Skip to content

fix(profiling): sample an asyncio eager task's first step once, as the eager task - #22048

Draft
taegyunkim wants to merge 1 commit into
mainfrom
taegyunkim/profiling-eager-step-own-sample
Draft

taegyunkim wants to merge 1 commit into
mainfrom
taegyunkim/profiling-eager-step-own-sample

Conversation

@taegyunkim

@taegyunkim taegyunkim commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Description

JIRA: PROF-16348. Replaces #21744.

An eager task (Python 3.12 and 3.13) runs its first step inside create_task, while its creator is still running. The stack sampler saw both tasks as on CPU and emitted a sample for each, so the step was counted twice. The eager task's copy also repeated its own frame and lost the creator's frames.

The sampler now gives the sample to the running task found in asyncio.tasks._eager_tasks and drops the other running tasks. Its stack is the whole thread stack below Handle._run, which already holds the creator's frames, so nothing else is added. With nested eager steps, the innermost eager task owns the sample.

The eager task owns the sample, not its creator, so that the step carries the eager task's span once task samples read their own task's Context.

Customer impact

Eager steps are reported once, under the eager task, with the full stack.

Testing

test_asyncio_eager_tasks.py checks one sample per tick, the eager task's id and name, and the stack, for an eager child and an eager grandchild. It fails on main and when the creator keeps the sample. With test_stack_asyncio_endpoint_labels.py, it passes on CPython 3.12.15 and 3.13.16 (Linux aarch64, scripts/run-tests). On 3.14.8 the new tests are strict xfails: native tasks never enter _eager_tasks.

Risks

The sampler reads the thread while it runs, so one sample at a step's start or end can carry the other task's labels. It is still counted once.

Additional Notes

None.

@cit-pr-commenter-54b7da

Copy link
Copy Markdown

Codeowners resolved as

Resolved from the full PR diff against main using the target branch CODEOWNERS file.
CODEOWNERS team requests not listed below are not required by the current file set.

ddtrace/internal/datadog/profiling/stack/echion/echion/tasks.h          @DataDog/profiling-python
ddtrace/internal/datadog/profiling/stack/echion/echion/threads.h        @DataDog/profiling-python
ddtrace/internal/datadog/profiling/stack/src/echion/threads.cc          @DataDog/profiling-python
releasenotes/notes/profiling-eager-task-first-step-bd4bc205ec068a5f.yaml  @DataDog/apm-python
tests/profiling/collector/test_asyncio_eager_tasks.py                   @DataDog/profiling-python

@cit-pr-commenter-54b7da

Copy link
Copy Markdown

Dependency direction analysis

⚠️ Existing dependency direction violations

There are 22 dependency direction violations that already exist on the base branch and have not been changed by this PR.

Show existing violations (showing 5 of 22 highest severity)
ddtrace.internal.tracemethods -×-> ddtrace.trace  (internal-core -> product:tracing, score=103)
ddtrace.llmobs._integrations.base -×-> ddtrace.trace  (product:llmobs -> product:tracing, score=101)
ddtrace.llmobs._integrations.bedrock_agents -×-> ddtrace.trace  (product:llmobs -> product:tracing, score=101)
ddtrace.internal.span_bus -×-> ddtrace._trace.span  (internal-core -> product:tracing, score=35)
ddtrace.llmobs._llmobs -×-> ddtrace._trace.span  (product:llmobs -> product:tracing, score=33)

To see all violations, download the layers-base.json and layers-pr.json artifacts from this CI job and run:

uv run --script scripts/import-analysis/layers.py compare layers-base.json layers-pr.json

@pr-commenter

pr-commenter Bot commented Nov 24, 2026

Copy link
Copy Markdown

Benchmarks

Benchmark execution time: 2026-11-24 00:51:32

Comparing candidate commit 3b38d81 in PR branch taegyunkim/profiling-eager-step-own-sample with baseline commit 2cc4b9c in branch main.

📊 Benchmarking dashboard

Found 0 performance improvements and 0 performance regressions! Performance is the same for 604 metrics, 10 unstable metrics, 1 flaky benchmarks without significant changes.

Explanation

This is an A/B test comparing a candidate commit's performance against that of a baseline commit. Performance changes are noted in the tables below as:

  • 🟩 = significantly better candidate vs. baseline
  • 🟥 = significantly worse candidate vs. baseline

We compute a confidence interval (CI) over the relative difference of means between metrics from the candidate and baseline commits, considering the baseline as the reference.

If the CI is entirely outside the configured SIGNIFICANT_IMPACT_THRESHOLD (or the deprecated UNCONFIDENCE_THRESHOLD), the change is considered significant.

Feel free to reach out to #apm-benchmarking-platform on Slack if you have any questions.

More details about the CI and significant changes

You can imagine this CI as a range of values that is likely to contain the true difference of means between the candidate and baseline commits.

CIs of the difference of means are often centered around 0%, because often changes are not that big:

---------------------------------(------|---^--------)-------------------------------->
                              -0.6%    0%  0.3%     +1.2%
                                 |          |        |
         lower bound of the CI --'          |        |
sample mean (center of the CI) -------------'        |
         upper bound of the CI ----------------------'

As described above, a change is considered significant if the CI is entirely outside the configured SIGNIFICANT_IMPACT_THRESHOLD (or the deprecated UNCONFIDENCE_THRESHOLD).

For instance, for an execution time metric, this confidence interval indicates a significantly worse performance:

----------------------------------------|---------|---(---------^---------)---------->
                                       0%        1%  1.3%      2.2%      3.1%
                                                  |   |         |         |
       significant impact threshold --------------'   |         |         |
                      lower bound of CI --------------'         |         |
       sample mean (center of the CI) --------------------------'         |
                      upper bound of CI ----------------------------------'

Unstable benchmarks

These benchmarks have a confidence interval too wide to call a change; treat them as noise rather than signal.

scenario:coreapiscenario-context_with_data_listeners

  • unstable execution_time [-766.074ns; +674.885ns] or [-7.075%; +6.233%]

scenario:coreapiscenario-core_dispatch_1_listener

  • unstable execution_time [-35.679ns; +38.887ns] or [-5.532%; +6.030%]

scenario:coreapiscenario-core_dispatch_50_listeners

  • unstable execution_time [-2045.748ns; +1673.394ns] or [-10.053%; +8.223%]

scenario:coreapiscenario-core_dispatch_exception_listeners

  • unstable execution_time [-1696.473ns; +1826.737ns] or [-8.751%; +9.423%]

scenario:coreapiscenario-core_dispatch_listeners

  • unstable execution_time [-403.005ns; +335.731ns] or [-9.195%; +7.660%]

scenario:coreapiscenario-core_dispatch_no_args_listeners

  • unstable execution_time [-224.469ns; +223.030ns] or [-8.115%; +8.063%]

scenario:coreapiscenario-core_dispatch_with_results_1_listener

  • unstable execution_time [-87.583ns; +86.383ns] or [-6.286%; +6.200%]

scenario:coreapiscenario-core_dispatch_with_results_50_listeners

  • unstable execution_time [-4216.381ns; +4804.330ns] or [-8.649%; +9.855%]

scenario:coreapiscenario-core_dispatch_with_results_listeners

  • unstable execution_time [-947.112ns; +890.562ns] or [-9.114%; +8.570%]

scenario:packagesupdateimporteddependencies-import_many_stdlib_cached

  • unstable execution_time [-63.685µs; +47.833µs] or [-10.941%; +8.218%]

Known flaky benchmarks

These benchmarks are marked as flaky and will not trigger a failure. Modify FLAKY_BENCHMARKS_REGEX to control which benchmarks are marked as flaky.

Known flaky benchmarks without significant changes:

  • scenario:packagespackageforrootmodulemapping-cache_off

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant