-
Notifications
You must be signed in to change notification settings - Fork 375
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
[PROF-10241] Use Process._fork hook in at_fork
monkey patch on Ruby 3.1+
#3830
Merged
ivoanjo
merged 3 commits into
master
from
ivoanjo/prof-10241-use-process-fork-modern-rubies
Aug 8, 2024
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,15 +3,8 @@ | |
module Datadog | ||
module Core | ||
module Utils | ||
# Monkey patches `Kernel#fork` and similar functions, adding a `Kernel#datadog_at_fork` callback mechanism which | ||
# Monkey patches `Kernel#fork` and similar functions, adding a `Process#datadog_at_fork` callback mechanism which | ||
# is used to restart observability after the VM forks (e.g. in multiprocess Ruby apps). | ||
# | ||
# TODO: Use `Process._fork` on Ruby 3.1+, see | ||
# https://github.com/ruby/ruby/pull/5017 and https://bugs.ruby-lang.org/issues/17795 | ||
# | ||
# Known limitations: Does not handle `BasicObject`s that include `Kernel` directly; e.g. | ||
# `Class.new(BasicObject) { include(::Kernel); def call; fork { }; end }.new.call`. | ||
# This will be fixed once we move to hooking into `Process._fork` | ||
module AtForkMonkeyPatch | ||
def self.supported? | ||
Process.respond_to?(:fork) | ||
|
@@ -20,20 +13,22 @@ def self.supported? | |
def self.apply! | ||
return false unless supported? | ||
|
||
[ | ||
::Process.singleton_class, # Process.fork | ||
::Kernel.singleton_class, # Kernel.fork | ||
::Object, # fork without explicit receiver (it's defined as a method in ::Kernel) | ||
# Note: Modifying Object as we do here is irreversible. During tests, this | ||
# change will stick around even if we otherwise stub `Process` and `Kernel` | ||
].each { |target| target.prepend(KernelMonkeyPatch) } | ||
if RUBY_VERSION < '3.1' | ||
[ | ||
::Process.singleton_class, # Process.fork | ||
::Kernel.singleton_class, # Kernel.fork | ||
::Object, # fork without explicit receiver (it's defined as a method in ::Kernel) | ||
# Note: Modifying Object as we do here is irreversible. During tests, this | ||
# change will stick around even if we otherwise stub `Process` and `Kernel` | ||
].each { |target| target.prepend(KernelMonkeyPatch) } | ||
end | ||
|
||
::Process.singleton_class.prepend(ProcessDaemonMonkeyPatch) | ||
|
||
true | ||
end | ||
|
||
# Adds `Kernel#datadog_at_fork` behavior; see parent module for details. | ||
# Adds `datadog_at_fork` behavior; see parent module for details. | ||
module KernelMonkeyPatch | ||
def fork | ||
# If a block is provided, it must be wrapped to trigger callbacks. | ||
|
@@ -60,17 +55,6 @@ def fork | |
result | ||
end | ||
|
||
# NOTE: You probably want to wrap any calls to datadog_at_fork with a OnlyOnce so as to not re-register | ||
# the same block/behavior more than once. | ||
def datadog_at_fork(stage, &block) | ||
raise ArgumentError, 'Bad \'stage\' for ::datadog_at_fork' unless stage == :child | ||
|
||
datadog_at_fork_blocks[stage] ||= [] | ||
datadog_at_fork_blocks[stage] << block | ||
|
||
nil | ||
end | ||
|
||
module_function | ||
|
||
def datadog_at_fork_blocks | ||
|
@@ -80,11 +64,23 @@ def datadog_at_fork_blocks | |
end | ||
end | ||
|
||
# A call to Process.daemon ( https://rubyapi.org/3.1/o/process#method-c-daemon ) forks the current process and | ||
# keeps executing code in the child process, killing off the parent, thus effectively replacing it. | ||
# | ||
# This monkey patch makes the `Kernel#datadog_at_fork` mechanism defined above also work in this situation. | ||
# Adds `datadog_at_fork` behavior; see parent module for details. | ||
module ProcessDaemonMonkeyPatch | ||
# Hook provided by Ruby 3.1+ for observability libraries that want to know about fork, see | ||
# https://github.com/ruby/ruby/pull/5017 and https://bugs.ruby-lang.org/issues/17795 | ||
def _fork | ||
datadog_at_fork_blocks = Datadog::Core::Utils::AtForkMonkeyPatch::KernelMonkeyPatch.datadog_at_fork_blocks | ||
|
||
pid = super | ||
|
||
datadog_at_fork_blocks[:child].each(&:call) if datadog_at_fork_blocks.key?(:child) && pid == 0 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would check There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in 9cf5444 |
||
|
||
pid | ||
end | ||
|
||
# A call to Process.daemon ( https://rubyapi.org/3.1/o/process#method-c-daemon ) forks the current process and | ||
# keeps executing code in the child process, killing off the parent, thus effectively replacing it. | ||
# This is not covered by `_fork` and thus we have some extra code for it. | ||
def daemon(*args) | ||
datadog_at_fork_blocks = Datadog::Core::Utils::AtForkMonkeyPatch::KernelMonkeyPatch.datadog_at_fork_blocks | ||
|
||
|
@@ -94,6 +90,23 @@ def daemon(*args) | |
|
||
result | ||
end | ||
|
||
# NOTE: You probably want to wrap any calls to datadog_at_fork with a OnlyOnce so as to not re-register | ||
# the same block/behavior more than once. | ||
def datadog_at_fork(stage, &block) | ||
ProcessDaemonMonkeyPatch.datadog_at_fork(stage, &block) | ||
end | ||
|
||
# Also allow calling without going through Process for tests | ||
def self.datadog_at_fork(stage, &block) | ||
raise ArgumentError, 'Bad \'stage\' for ::datadog_at_fork' unless stage == :child | ||
|
||
datadog_at_fork_blocks = Datadog::Core::Utils::AtForkMonkeyPatch::KernelMonkeyPatch.datadog_at_fork_blocks | ||
datadog_at_fork_blocks[stage] ||= [] | ||
datadog_at_fork_blocks[stage] << block | ||
|
||
nil | ||
end | ||
end | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should the module now be called just
ProcessMoneyPatch
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Process 💰 Patch 🤣
Fixed in 0c719dd