fix(profiling): fix data race when accessing span for thread #11167
+11
−7
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.
The ThreadSpanLinks singleton holds the active span (if one exists) for
a given thread ID. The
get_active_span_from_thread_id
member functionreturns a pointer to the active span for a thread. The
link_span
memberfunction sets the active span for a thread.
get_active_span_from_thread_id
accesses the map of spans under a mutex, but returns the pointer after
releasing the mutex, meaning
link_span
can modify the members of theSpan while the caller of
get_active_span_from_thread_id
is reading them.Fix this by returning a copy of the
Span
. Use astd::optional
to wrapthe return value of
get_active_span_from_thread_id
, rather thanreturning a pointer. We want to tell whether or not there actually was a
span associated with the thread, but returning a pointer would require
us to heap allocate the copy of the Span.
TODO - regression test?
TODO - can we use C++17?
Checklist
Reviewer Checklist