Repository navigation
Introduce a callback for forked children - #37312
Conversation
There was a problem hiding this comment.
This leaves Kernel.fork and Kernel#fork untouched... maybe we should cover those too. We can't help anyone who's already included Kernel, but it would at least catch anyone who includes it after we're hooked.
There was a problem hiding this comment.
We can't help anyone who's already included Kernel
Actually we could if we monkey patched Kernel using an alias method chain.
However I don't think Kernel#fork is a problem because Kernel is already included in Object. So the only way to bypass this patch would be to include Kernel in a BasicObject subclass, which really is a stretch IMO.
Good point about Kernel.fork though, I'll fix it.
There was a problem hiding this comment.
Including Kernel in a BasicObject is exactly what SimpleDelegator from the stdlib does. But it goes on to remove all the private instance methods of Kernel (which includes fork) to ensure messages pass through to the delegated object, so it's not a problem in the end.
https://github.com/ruby/ruby/blob/01e75b6be9987bf9ce0793643470e47a32871c38/lib/delegate.rb#L39-L40
4784f51 to
02046a0
Compare
We hook into Kernel#fork and Process#fork, when they are invoked we can trigger all the registered callbacks. There is also a `check!` method that can be called to very cheaply detect if the process was forked.
02046a0 to
78b9580
Compare
There was a problem hiding this comment.
Including Kernel in a BasicObject is exactly what SimpleDelegator from the stdlib does. But it goes on to remove all the private instance methods of Kernel (which includes fork) to ensure messages pass through to the delegated object, so it's not a problem in the end.
https://github.com/ruby/ruby/blob/01e75b6be9987bf9ce0793643470e47a32871c38/lib/delegate.rb#L39-L40
| module ActiveSupport | ||
| module ForkTracker # :nodoc: | ||
| module CoreExt | ||
| def fork(*) |
There was a problem hiding this comment.
It should be made private, otherwise it makes fork public for all Object instances.
We hook into Kernel#fork and Process#fork, when they are invoked
we can trigger all the registered callbacks.
There is also a
check!method that can be called to very cheaplydetect if the process was forked.
Context
This originates from #37296. In ActiveRecord we have to detect when a fork happened, so that we can safely discard the database connections inherited from the parent.
Currently it's done in a relatively ad hoc way inside Active Record, but extracting the forking detection logic can really improve and simplify Active Record connection management code, see: 9748425
How it works
We hook into
Kernel#forkandProcess.forkso that we are immediately aware that the process was forked, and able to do all the necessary cleanup right away.However just in case the fork was done inside a c-extension, there's a
check!method which is recommended to be called before using database connections. And that any forking code bypassing ruby'sforkmethod should invoke.@matthewd @rafaelfranca