[PROF-4756] Fix profiler not restarting on Process.daemon
#2150
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.
What does this PR do?:
Extend the
Forking
monkey patch to also cover theProcess.daemon
API (that is another way of forking a Ruby process).Motivation:
When the Ruby VM
fork
s, only the thread that calledfork
survives in the child process.Thus, we have the
Forking
monkey patch which allows us to run callbacks in the child process, including restarting the profiler.But there's a less known API in Ruby that also makes use of forking --
Process.daemon
. One example of using this API is when callingrails server -d
; that-d
makes Rails call this API.Because we were missing monkey patching
Process.daemon
, the profiler would not restart onProcess.daemon
. A customer using exactlyrails server -d
ran into this issue.Additional Notes:
This PR is on top of #2149 just to avoid any conflicts because of close by lines being removed, but both PRs are entirely independent.
How to test the change?
Easy one-liner to validate that the profiler restarts after becoming a daemon:
The daemon mode seems... broken(?) in Rails 7. With puma, no webserver would be started, and with webrick, a process would stay behind but not reply to any HTTP requests.
Nevertheless, I believe this fix should be enough (the customer reported this a long ago, this was a low priority fix).