Skip to content

Introduce a callback for forked children - #37312

Merged
matthewd merged 1 commit into
rails:masterfrom
Shopify:on-fork-callback
Sep 30, 2019
Merged

matthewd merged 1 commit into
rails:masterfrom
Shopify:on-fork-callback

Conversation

@casperisfine

Copy link
Copy Markdown
Contributor

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.

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#fork and Process.fork so 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's fork method should invoke.

@matthewd @rafaelfranca

Comment thread activesupport/lib/active_support.rb Resolved

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment thread activesupport/test/fork_tracker_test.rb Resolved
Comment thread activesupport/test/fork_tracker_test.rb Resolved
@casperisfine
casperisfine force-pushed the on-fork-callback branch 2 times, most recently from 4784f51 to 02046a0 Compare September 30, 2019 09:18
@casperisfine

Copy link
Copy Markdown
Contributor Author

@matthewd I think I addressed all your feedback. I'll start rebasing #37296 on top of this PR.

Comment thread activesupport/lib/active_support.rb Resolved
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.
@matthewd
matthewd merged commit 9a62e95 into rails:master Sep 30, 2019

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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(*)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be made private, otherwise it makes fork public for all Object instances.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants