Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Close open file descriptors when calling Process.spawn #1904

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions kernel/common/io.rb
Original file line number Diff line number Diff line change
Expand Up @@ -454,13 +454,27 @@ def self.setup(io, fd, mode=nil, sync=false)
end
end

update_max_open_fd(fd)

io.descriptor = fd
io.mode = mode || cur_mode
io.sync = !!sync
io.sync ||= STDOUT.fileno == fd if STDOUT.respond_to?(:fileno)
io.sync ||= STDERR.fileno == fd if STDERR.respond_to?(:fileno)
end

@max_open_fd = 2

def self.update_max_open_fd(*fds)
fds.each do |fd|
@max_open_fd = fd if fd > @max_open_fd
end
end

def self.max_open_fd
@max_open_fd
end

##
# Obtains a new duplicate descriptor for the current one.
def initialize_copy(original) # :nodoc:
Expand Down
2 changes: 2 additions & 0 deletions kernel/common/io18.rb
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,11 @@ def self.pipe

begin
connect_pipe(lhs, rhs)
::IO.update_max_open_fd(lhs.fileno, rhs.fileno)
rescue Errno::EMFILE
GC.run(true)
connect_pipe(lhs, rhs)
::IO.update_max_open_fd(lhs.fileno, rhs.fileno)
end

lhs.sync = true
Expand Down
2 changes: 2 additions & 0 deletions kernel/common/io19.rb
Original file line number Diff line number Diff line change
Expand Up @@ -673,9 +673,11 @@ def self.pipe(external_encoding=nil, internal_encoding=nil)

begin
connect_pipe(lhs, rhs)
::IO.update_max_open_fd(lhs.fileno, rhs.fileno)
rescue Errno::EMFILE
GC.run(true)
connect_pipe(lhs, rhs)
::IO.update_max_open_fd(lhs.fileno, rhs.fileno)
end

external_encoding ||= Encoding.default_external
Expand Down
9 changes: 9 additions & 0 deletions kernel/common/process19.rb
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,15 @@ def self.exec(*args)
def self.spawn(*args)
env, prog, argv, redirects, options = Rubinius::Spawn.extract_arguments(*args)

unless options[:close_others] == false
3.upto(IO.max_open_fd).each do |fd|
begin
IO.for_fd(fd, :autoclose => false).close_on_exec = true
rescue Errno::EBADF
end
end
end

IO.pipe do |read, write|
pid = Process.fork do
read.close
Expand Down
1 change: 0 additions & 1 deletion lib/19/open3.rb
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,6 @@ def popen2e(*cmd, &block)
module_function :popen2e

def popen_run(cmd, opts, child_io, parent_io) # :nodoc:
parent_io.each {|io| io.close_on_exec = true }
pid = spawn(*cmd, opts)
wait_thr = Process.detach(pid)
child_io.each {|io| io.close }
Expand Down
2 changes: 0 additions & 2 deletions spec/tags/19/ruby/core/kernel/spawn_tags.txt

This file was deleted.

1 change: 0 additions & 1 deletion spec/tags/19/ruby/core/process/spawn_tags.txt

This file was deleted.