-
Notifications
You must be signed in to change notification settings - Fork 377
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[PROF-10241] Use Process._fork hook in
at_fork
monkey patch on Ruby…
… 3.1+ **What does this PR do?** This PR solves the long-standing "TODO" we had on the `at_fork` monkey patch where on Ruby 3.1+ there's a VM hook for instrumenting fork operations that avoids us needing to monkey patch `Kernel` and `Object`. To use the new hook we need to monkey patch `Process._fork` instead (see ruby/ruby#5017 and https://bugs.ruby-lang.org/issues/17795). Thus, I've added this monkey patch and disabled patching of `Kernel` and `Object` on Ruby 3.1+. **Motivation:** Avoid monkey patching core classes that no longer need to. **Additional Notes:** I'm not quite happy with either the AtForkMonkeyPatch or its specs, but I decided to not do a full revamp for now. **How to test the change?** This change includes test coverage. Here's a tiny test app that also exercises this feature: ```ruby puts RUBY_DESCRIPTION require 'datadog/core/utils/at_fork_monkey_patch' Datadog::Core::Utils::AtForkMonkeyPatch.apply! Process.datadog_at_fork(:child) { puts "Hello from child process: #{Process.pid} "} puts "Parent pid: #{Process.pid}" puts "fork { }" fork { } Process.wait puts "Kernel.fork { }" Kernel.fork { } Process.wait puts "Process.fork { }" Process.fork { } Process.wait puts "Foo.new.call" class Foo def call fork { } end def self.do_fork fork { } end end Foo.new.call Process.wait puts "Foo.do_fork" Foo.do_fork Process.wait puts "Fork from outside" Foo.new.send(:fork) { } Process.wait class BasicFoo < BasicObject include ::Kernel def call fork { } end def self.do_fork fork { } end end puts "BasicFoo.new.call" BasicFoo.new.call Process.wait puts "BasicObject:" Class.new(BasicObject) { include(::Kernel); def call; fork { }; end }.new.call Process.wait puts "BasicFork.do_fork" BasicFoo.do_fork Process.wait ``` P.s.: You may notice when you run this example on < 3.1 that `BasicFoo.new.call` and `BasicObject` are not instrumented! This was actually a known-gap from the old instrumentation.
- Loading branch information
Showing
2 changed files
with
121 additions
and
90 deletions.
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
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