Skip to content

fix(profiling): cover alternate asyncio task creation - #19422

Closed
taegyunkim wants to merge 1 commit into
taegyunkim/profiling-asyncio-span-inheritancefrom
taegyunkim/profiling-asyncio-creation-paths
Closed

taegyunkim wants to merge 1 commit into
taegyunkim/profiling-asyncio-span-inheritancefrom
taegyunkim/profiling-asyncio-creation-paths

Conversation

@taegyunkim

@taegyunkim taegyunkim commented Jul 30, 2026 •

Copy link
Copy Markdown
Contributor

Description

Stacked on #19421, following the core asyncio task attribution in #19346.

Covers asyncio task and loop execution paths that can bypass BaseEventLoop.create_task() or event-loop policy registration.

The change:

  • wraps public and private ensure_future paths, including the helper used by supported Python versions of gather();
  • publishes tasks returned by TaskGroup.create_task();
  • publishes tasks returned by the public asyncio.create_task() path;
  • captures task-factory state before each creation path and reuses the inherited-Context validation from fix(profiling): validate inherited asyncio span links #19421;
  • registers a BaseEventLoop for the duration of run_forever(), covering run_until_complete() and Runner(loop_factory=...) loops that were never installed through set_event_loop();
  • removes that runtime registration in finally, so stopped loops are not retained or later unwound from the wrong thread.

Duplicate publication through nested standard-library helpers is safe and idempotent. The task mapping is simply updated with the same copied metadata, and weak finalizer registration is deduplicated by task ID.

A task created entirely inside third-party native code can still be unobserved. Such a task remains unattributed and never falls back to the event-loop thread link.

Testing

Added regressions for:

  • inherited endpoint attribution for uvloop tasks created by raw coroutine arguments to asyncio.gather();
  • a loop executed with new_event_loop().run_until_complete() without calling set_event_loop().

Python 3.13 uvloop environment:

uvloop gather and explicit/custom empty Context: 2 passed, 403 deselected
unregistered run_until_complete loop: 1 passed, 404 deselected

Also passed scripts/lint, git diff --check, and targeted formatting. scripts/run-benchmarks --list found no benchmark suite matching _asyncio.py.

Risks

  • The additional wrappers run only when native stack profiling is enabled. Publication is best effort and cannot make task creation or loop execution fail.
  • run_forever() adds two loop-registration calls per invocation, not per event-loop iteration.
  • Unsupported native-only creation paths lose attribution instead of borrowing another task's or the thread's span.

Additional Notes

No separate release note is needed because this extends the customer-facing asyncio fix documented in #19346. Add the changelog/no-changelog label.

@taegyunkim taegyunkim added the changelog/no-changelog A changelog entry is not required for this PR. label Jul 30, 2026
@cit-pr-commenter-54b7da

Copy link
Copy Markdown

Codeowners resolved as

ddtrace/profiling/_asyncio.py                                           @DataDog/profiling-python
tests/profiling/collector/test_stack_asyncio_endpoint_labels.py         @DataDog/profiling-python

@cit-pr-commenter-54b7da

Copy link
Copy Markdown

Circular import analysis

⚠️ Existing circular imports

There are 3 circular imports that already exist on the base branch and have not been changed by this PR.

ddtrace.trace -> ddtrace._trace.tracer -> ddtrace.internal.debug -> ddtrace.trace
ddtrace -> ddtrace.trace -> ddtrace._trace.tracer -> ddtrace.internal.debug -> ddtrace
ddtrace -> ddtrace.trace -> ddtrace._trace.tracer -> ddtrace.internal.debug -> ddtrace.internal.runtime.runtime_metrics -> ddtrace

@taegyunkim taegyunkim added the Profiling Continous Profling label Jul 30, 2026
@datadog-datadog-prod-us1-2

datadog-datadog-prod-us1-2 Bot commented Jul 30, 2026 •

Copy link
Copy Markdown
Contributor

Pipelines  Tests

✨ Unblock PR with BitsAI

⚠️ Warnings

🚦 13 Pipeline jobs failed

DataDog/apm-reliability/dd-trace-py | build linux serverless: [amd64, cp315-cp315, v113741238-d2b8243-manylinux2014_x86_64, 1]   View in Datadog   GitLab

DataDog/apm-reliability/dd-trace-py | build linux serverless: [amd64, cp315-cp315, v126532274-233089d-musllinux_1_2_x86_64, 1]   View in Datadog   GitLab

DataDog/apm-reliability/dd-trace-py | build linux serverless: [arm64, cp315-cp315, v113741357-d2b8243-manylinux2014_aarch64, 1]   View in Datadog   GitLab

View all 13 failed jobs.

ℹ️ Info

No other issues found (see more)

🧪 All tests passed
❄️ No new flaky tests detected

Useful? React with 👍 / 👎

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: df4912d | Docs | Datadog PR Page | Give us feedback!

@taegyunkim
taegyunkim force-pushed the taegyunkim/profiling-asyncio-span-inheritance branch from 02909e3 to 8dfd99e Compare July 30, 2026 22:08
@taegyunkim
taegyunkim force-pushed the taegyunkim/profiling-asyncio-creation-paths branch from 4585594 to df4912d Compare July 30, 2026 22:09
@taegyunkim

Copy link
Copy Markdown
Contributor Author

Consolidated into #19346 so the asyncio attribution change can be reviewed and landed as one complete lifecycle unit.

@taegyunkim taegyunkim closed this Aug 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

changelog/no-changelog A changelog entry is not required for this PR. Profiling Continous Profling

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant