Skip to content
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

Mark ddtrace threads as fork-safe #3279

Merged
merged 3 commits into from
Nov 27, 2023
Merged

Mark ddtrace threads as fork-safe #3279

merged 3 commits into from
Nov 27, 2023

Conversation

ivoanjo
Copy link
Member

@ivoanjo ivoanjo commented Nov 23, 2023

What does this PR do?

This PR applies to all relevant ddtrace threads a pattern pioneered by rails: set the :fork_safe thread variable to indicate that it's OK if ddtrace threads are started before a Ruby app calls fork.

Motivation:

This PR fixes the issue reported in #3203. The puma webserver in clustering mode has a check where it will warn when it detects that threads were started before the webserver has fork'd the child processes, see:

https://github.com/puma/puma/blob/32e011ab9e029c757823efb068358ed255fb7ef4/lib/puma/cluster.rb#L353-L359

This is not a problem for ddtrace (as discussed in #3203), but the warning is annoying.

While looking into the issue again today, I spotted that actually there's a way to tell puma that a thread is fine and is it's not a problem if it's started before the webserver has fork'd: puma checks if the thread has a :fork_safe thread-variable set to true.

Additional Notes:

We have a bunch of places in ddtrace where we create threads... I briefly played with the idea of creating a superclass of all ddtrace Threads, but decided to not get into a bigger refactoring for such a small issue. Maybe next time...?

How to test the change?

Test coverage included. Furthermore, you can try running a Ruby webapp in puma before/after the change to confirm the warning is gone.

For Datadog employees:

  • If this PR touches code that signs or publishes builds or packages, or handles
    credentials of any kind, I've requested a review from @DataDog/security-design-and-guidance.
  • This PR doesn't touch any of that.

Fixes #3203

**What does this PR do?**

This PR applies to all relevant ddtrace threads a pattern pioneered by
rails: set the `:fork_safe` thread variable to indicate that it's OK
if ddtrace threads are started before a Ruby app calls fork.

**Motivation:**

This PR fixes the issue reported in #3203. The puma webserver in
clustering mode has a check where it will warn when it detects that
threads were started before the webserver has fork'd the child
processes, see:

https://github.com/puma/puma/blob/32e011ab9e029c757823efb068358ed255fb7ef4/lib/puma/cluster.rb#L353-L359

This is not a problem for ddtrace (as discussed in #3203), but the
warning is annoying.

While looking into the issue again today, I spotted that actually
there's a way to tell puma that a thread is fine and is it's not a
problem if it's started before the webserver has fork'd: puma
checks if the thread has a `:fork_safe` thread-variable set to `true`.

**Additional Notes:**

We have a bunch of places in ddtrace where we create threads...
I briefly played with the idea of creating a superclass of all
ddtrace Threads, but decided to not get into a bigger refactoring
for such a small issue. Maybe next time...?

**How to test the change?**

Test coverage included. Furthermore, you can try running
a Ruby webapp in puma before/after the change to confirm the
warning is gone.

Fixes #3203
@ivoanjo ivoanjo requested review from a team as code owners November 23, 2023 14:19
@github-actions github-actions bot added core Involves Datadog core libraries profiling Involves Datadog profiling tracing labels Nov 23, 2023
@ivoanjo ivoanjo mentioned this pull request Nov 23, 2023
@codecov-commenter
Copy link

codecov-commenter commented Nov 23, 2023

Codecov Report

Attention: 2 lines in your changes are missing coverage. Please review.

Comparison is base (b4c9019) 98.23% compared to head (e430e78) 98.22%.
Report is 1 commits behind head on master.

Files Patch % Lines
lib/datadog/core/configuration.rb 50.00% 1 Missing ⚠️
spec/datadog/core/configuration_spec.rb 75.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #3279      +/-   ##
==========================================
- Coverage   98.23%   98.22%   -0.01%     
==========================================
  Files        1253     1253              
  Lines       72393    72418      +25     
  Branches     3393     3397       +4     
==========================================
+ Hits        71112    71133      +21     
- Misses       1281     1285       +4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Member

@marcotc marcotc left a comment

Choose a reason for hiding this comment

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

Could you drop a link to the relevant puma code section somewhere in our code base in a comment?

@ivoanjo
Copy link
Member Author

ivoanjo commented Nov 24, 2023

Could you drop a link to the relevant puma code section somewhere in our code base in a comment?

Done in e430e78 :)

@ivoanjo ivoanjo merged commit 29c8d5f into master Nov 27, 2023
218 checks passed
@ivoanjo ivoanjo deleted the ivoanjo/fix-puma-warning branch November 27, 2023 11:47
@github-actions github-actions bot added this to the 1.18.0 milestone Nov 27, 2023
@TonyCTHsu TonyCTHsu mentioned this pull request Dec 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Involves Datadog core libraries profiling Involves Datadog profiling tracing
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Puma warning log
3 participants