chore(ddtrace/tracer): add test showing partial flush race condition #4213
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?
Demonstrated a map write race when using partial flush. Explanation (AI-generated) below:
In spancontext.go (lines 624, 627), the partial flush code path:
Here, finishedSpans[0].setMetric(...) and t.setTraceTags(finishedSpans[0]) call setMeta/setMetric on finishedSpans[0], which may be a different span than s. While s (the span passed to finishedOne) has its mutex held by the caller, finishedSpans[0] does not have its mutex held.
The flow is:
Note that setMeta and setMetric do not check the finished flag - they directly modify the maps. So even though the spans are marked finished, this is still a potential data race if any reader accesses the span data concurrently.
Motivation
Internal incident, see JIRA ticket. But this seems to be another bug, unrelated to the incident.
Reviewer's Checklist
✨ claude-4.5-opus-high wrote this test case - should probably be cleaned up before we merge this.
./scripts/lint.shlocally.Unsure? Have a question? Request a review!