-
Notifications
You must be signed in to change notification settings - Fork 369
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
Fix object_id usage as thread local key #2096
Conversation
I tried spending a bit of time thinking about this. There's quite a lot of abstraction layers here, but here's my current understanding:
Flattening a bit down the layers, it appears to me that the intention here is to effectively have a unique key for each instance of But since this is a unique key per tracer instance, and we don't expect many tracer instances to exist in a given application -- because if we do, we need to start thinking about cleaning up leftover contexts because otherwise they will lead to a memory leak -- I don't think we need a cheaper (performance-wise) way of doing this, since the common situation is that we'll do this one or at most a handful of times in practice. In terms of easier way, I don't think there is: https://bugs.ruby-lang.org/issues/12607 is actually what we'd want (atomic integer) but hasn't been accepted yet. |
Also, this is not in the part of the code you changed, but is this a potential bug? dd-trace-rb/lib/datadog/tracing/context_provider.rb Lines 23 to 34 in 0656198
In particular, when key is another thread, it seems weird to then say "whatever trace that other thread was running, I'm also running it now". Should we only run the |
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.
Seems reasonable to me!
Co-authored-by: Ivo Anjo <ivo.anjo@datadoghq.com>
This seems reasonable. I'll add a comment for this. |
Codecov Report
@@ Coverage Diff @@
## master #2096 +/- ##
=======================================
Coverage 97.50% 97.50%
=======================================
Files 1033 1033
Lines 53260 53276 +16
=======================================
+ Hits 51933 51949 +16
Misses 1327 1327
📣 Codecov can now indicate which changes are the most critical in Pull Requests. Learn more |
I stumbled upon this flaky error: https://app.circleci.com/pipelines/github/DataDog/dd-trace-rb/6331/workflows/02b56e62-53d7-4d50-ac12-f3e84194bf8f/jobs/234310/parallel-runs/1
It happens because we use
object_id
as a thread local key andobject_id
can be reused after garbage collection, causing collision between a recently garbage collected instance ofFiberLocalContext
and a new one.This PR addresses this issue in two places where we use
object_id
as a thread local key by creating a thread-safe counter for each of the object instances.If there's an easier/cheaper way to get a unique identifier please let me know!