Repository navigation
fix(profiling): clear finished physical span links - #19529
gh-worker-dd-mergequeue-cf854d[bot] merged 12 commits into
Conversation
🎉 All green!🧪 All tests passed 🔗 Commit SHA: c5b6e97 | Docs | Datadog PR Page | Give us feedback! |
971fd10 to
82b9518
Compare
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 82b9518ce8
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
82b9518 to
6f4f30f
Compare
Codeowners resolved asResolved from the full PR diff against No remaining files require a CODEOWNERS review. |
Circular import analysis
|
4899eaa to
941677e
Compare
Dependency direction analysis
|
|
@KowalskiThomas Thanks for reviewing. I have addressed all your comments. Please take another look |
|
@codex review |
|
Codex Review: Didn't find any major issues. Hooray! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
There was a problem hiding this comment.
Pull request overview
Adds physical-thread span-link cleanup to prevent stale profiling attribution after spans finish.
Changes:
- Adds reverse span-to-thread indexing and finish-time invalidation.
- Registers activation and span-finish lifecycle listeners.
- Adds native/integration tests, profile helpers, and a release note.
Reviewed changes
Copilot reviewed 12 out of 12 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
ddtrace/profiling/collector/stack.py |
Manages span-link listeners. |
ddtrace/internal/datadog/profiling/stack/__init__.py |
Handles deactivation and finished spans. |
ddtrace/internal/datadog/profiling/stack/__init__.pyi |
Types the finish callback. |
ddtrace/internal/datadog/profiling/stack/_stack.pyi |
Types native unlinking. |
ddtrace/internal/datadog/profiling/stack/include/thread_span_links.hpp |
Defines reverse-index storage. |
ddtrace/internal/datadog/profiling/stack/src/thread_span_links.cpp |
Implements atomic link cleanup. |
ddtrace/internal/datadog/profiling/stack/src/stack.cpp |
Exposes native finish invalidation. |
ddtrace/internal/datadog/profiling/stack/test/test_thread_span_links.cpp |
Tests reverse invalidation and relinking. |
tests/profiling/collector/test_stack_span_links.py |
Tests context deactivation. |
tests/profiling/collector/test_stack_endpoint_labels.py |
Tests attribution lifecycle scenarios. |
tests/profiling/collector/pprof_utils.py |
Adds profile inspection helpers. |
releasenotes/notes/profiling-clear-finished-endpoint-e75147a1799ab41a.yaml |
Documents the customer-facing fix. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
KowalskiThomas
left a comment
There was a problem hiding this comment.
Makes sense to me -- one last suggestion but it's really minor.
I do believe this should be tested in staging if it hasn't though; leaving as comment (and not approval) until I have confirmation on that!
@KowalskiThomas OK I sent it to
|
KowalskiThomas
left a comment
There was a problem hiding this comment.
Staging seems to look good, thanks for validating.
Description
This is the first PR in the span-attribution stack. It fixes stale endpoint and span attribution for physical thread stacks before the later PRs introduce logical asyncio and gevent targets.
Problem
The stack profiler samples threads independently of application execution, so it keeps a native per-thread copy of the span metadata needed to label profile samples. That copy must follow the lifetime of the tracing context. Otherwise, work that runs after a span finishes can still be labeled with the finished span ID, local-root span ID, and endpoint.
Context activation events are necessary, but they are not sufficient to keep these copies accurate. Consider this sequence:
Finishing the span does not produce an activation event on the worker. Without a separate finish-time cleanup path, its copied link remains stale and subsequent samples are incorrectly attributed to the finished trace.
A local root can also finish before one of its children. In that case the child is still valid and can continue producing profile samples, so cleanup for the root must not remove mappings whose current span is that unfinished child.
Solution
This PR makes the native
ThreadSpanLinksstore own both directions of each physical association under one mutex:It then uses two complementary lifecycle signals:
ddtrace.context_provider.activatehandles immediate state replacement on the current thread. Activating a span or propagatedContextpublishes its mapping, restoring a parent replaces it, and activatingNoneor aContextwithout a span ID clears it.trace.span_finishperforms global reverse invalidation for the exact span that finished. It removes every copied physical-thread mapping for that span, including mappings published on another thread that may never receive another activation. It deliberately preserves mappings for unfinished children when their local root finishes first.Updating the forward and reverse indexes atomically prevents the two views from diverging. Re-linking a thread removes its previous reverse-index entries first, so a later finish event for an older span cannot erase the thread's newer attribution.
Listener registration occurs only after the collector completes fallible initialization, and both listeners are removed during shutdown. Fork handling reconstructs the native mutex and inherited indexes without traversing potentially inconsistent parent state.
Review follow-up
Native lifecycle bookkeeping now records in-flight physical span publications before releasing the GIL. If a span finishes while publication is waiting on the native mutex, cleanup is deferred until the final publication completes. This preserves the GIL release introduced in #14852 to avoid thread-pool deadlocks.
Testing
Added coverage for:
Validation completed on the reordered physical-only PR:
359 passed, 35 skipped, 2 xfailedper mode1 passedin each mode309 passed, 85 skipped, 2 xfailedin both normal and fast-copy modes22 passedscripts/lint checksscripts/lint cformatscripts/lint profiling-native-checkmainat3efdcbe3afgit diff --checkRisks
Additional Notes
Before
After