Feature #19560
openIO#close_on_fork= and IO#close_on_fork?
Description
Context¶
Forking setups are extremely common in the Ruby ecosystem, as they remain the primary way to get parallelism with MRI.
Generally speaking it works very well, however there are two main issues library authors and application owners need to be careful of:
- Restarting threads
- Closing inherited connections and other file descriptors.
I believe we could make the second one much easier.
O_CLOFORK¶
A couple years ago, a new flag was added to the POSIX spec: O_CLOFORK
. Similar to O_CLOEXEC
, this file descriptor flag make it so the file descriptor is automatically closed upon forking.
Unfortunately its support is relatively limited for now. It's supported on macOS and some relatively exotic unixes, but not in Linux nor most BSDs.
The feature was discussed on Linux mailing list, but it seem to have encountered some strong opposition, so it's unclear if we can hope for it to be added.
That said, I don't think it would be too hard for Ruby to shim this feature by closing all IOs with close_on_fork?
right after fork.
Ruby shim¶
This can be implemented as a Ruby shim starting in Ruby 3.1 using the Process._fork
callback
class IO
def close_on_fork=(enabled)
if enabled
::CloseIOOnFork::IOS[self] = true
end
@close_on_fork = enabled
end
def close_on_fork?
@close_on_fork
end
end
module CloseIOOnFork
IOS = ObjectSpace::WeakMap.new
def _fork
pid = super
if pid == 0 # child
::CloseIOOnFork::IOS.each_key do |io|
io.close if io.close_on_fork?
end
end
pid
end
end
Process.singleton_class.prepend(CloseIOOnFork)
rd, rw = IO.pipe
rw.close_on_fork = true
pid = fork do
p rw.closed? # => true
end
Process.wait(pid)
Usage¶
With such feature, many network client would mostly just need to set this flag on their sockets, and just properly handle unexpectedly closed connections, which most already do.