Permalink
Browse files

Set close_on_exec=true on file descriptors >= 3 when calling Process.…

…spawn
  • Loading branch information...
1 parent d93d457 commit d9aa645b2fc7d1a036efc7dbad10e1a197bfa2ad Lewis Marshall committed Sep 24, 2012
View
@@ -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:
@@ -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
@@ -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
@@ -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
@@ -1,2 +0,0 @@
-fails:Kernel#spawn closes file descriptors >= 3 in the child process
-fails:Kernel.spawn closes file descriptors >= 3 in the child process
@@ -1 +0,0 @@
-fails:Process.spawn closes file descriptors >= 3 in the child process

0 comments on commit d9aa645

Please sign in to comment.