Skip to content

fix(profiling): sample an asyncio eager task's first step once - #21744

Closed
taegyunkim wants to merge 9 commits into
mainfrom
taegyunkim/profiling-eager-task-single-sample
Closed

taegyunkim wants to merge 9 commits into
mainfrom
taegyunkim/profiling-eager-task-single-sample

Conversation

@taegyunkim

@taegyunkim taegyunkim commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Description

JIRA: PROF-16348

An eager task (Python 3.12+) runs its first step inside create_task, while the task that called it is still running. The stack sampler saw both as on CPU and gave each a full copy of the sample, so that time was counted twice. The eager task's copy also had a wrong stack.

When several tasks on a thread are running, the sampler now finds each one's coroutine frame on the thread stack and emits a single sample. That sample has the outermost task's stack and the innermost task's labels. The sampler reads the thread without stopping it, so a step can start or end while it reads. Tasks are placed using the frames read for the stack, and at most one running task gets a sample.

Testing

New test_asyncio_eager_tasks.py. One test checks an eager child and an eager grandchild for one sample per tick, the right task and the full stack. It lets the step's last sample carry a creator's label, the edge case under Risks. Another runs 10,000 short eager steps and checks one sample per tick at step edges. Both fail without the fix. tests/profiling passed on Python 3.9, 3.12, 3.13 and 3.14 (Linux aarch64, scripts/run-tests) before the last test change; CI covers that change. Native gtests are left to CI.

Risks

At a step's start or end, a sample can be labeled with the creating task instead of the eager task. Its time is still counted once.

Additional Notes

None.

@cit-pr-commenter-54b7da

cit-pr-commenter-54b7da Bot commented Nov 12, 2026 •

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/stacks.h         @DataDog/profiling-python
ddtrace/internal/datadog/profiling/stack/echion/echion/threads.h        @DataDog/profiling-python
ddtrace/internal/datadog/profiling/stack/src/echion/stacks.cc           @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

cit-pr-commenter-54b7da Bot commented Nov 12, 2026 •

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.bedrock_agents -×-> ddtrace.trace  (product:llmobs -> product:tracing, score=101)
ddtrace.llmobs._integrations.base -×-> 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 12, 2026 •

Copy link
Copy Markdown

Benchmarks

Benchmark execution time: 2026-11-23 21:39:19

Comparing candidate commit a54f185 in PR branch taegyunkim/profiling-eager-task-single-sample with baseline commit cbf6506 in branch main.

📊 Benchmarking dashboard

Found 0 performance improvements and 0 performance regressions! Performance is the same for 607 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 [-676.608ns; +751.891ns] or [-6.274%; +6.972%]

scenario:coreapiscenario-core_dispatch_1_listener

  • unstable execution_time [-35.054ns; +39.229ns] or [-5.444%; +6.092%]

scenario:coreapiscenario-core_dispatch_50_listeners

  • unstable execution_time [-2171.092ns; +1618.065ns] or [-10.647%; +7.935%]

scenario:coreapiscenario-core_dispatch_exception_listeners

  • unstable execution_time [-1962.414ns; +1557.219ns] or [-10.036%; +7.964%]

scenario:coreapiscenario-core_dispatch_listeners

  • unstable execution_time [-373.454ns; +353.983ns] or [-8.531%; +8.086%]

scenario:coreapiscenario-core_dispatch_no_args_listeners

  • unstable execution_time [-234.431ns; +216.596ns] or [-8.482%; +7.837%]

scenario:coreapiscenario-core_dispatch_with_results_1_listener

  • unstable execution_time [-87.368ns; +87.715ns] or [-6.309%; +6.334%]

scenario:coreapiscenario-core_dispatch_with_results_50_listeners

  • unstable execution_time [-4852.052ns; +4160.277ns] or [-9.901%; +8.489%]

scenario:coreapiscenario-core_dispatch_with_results_listeners

  • unstable execution_time [-1010.259ns; +790.140ns] or [-9.737%; +7.615%]

scenario:packagesupdateimporteddependencies-import_many_stdlib_cached

  • unstable execution_time [-56199.619ns; +55684.495ns] or [-9.637%; +9.548%]

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

Base automatically changed from taegyunkim/profiling-eager-task-span-test to main November 16, 2026 16:06
@taegyunkim
taegyunkim force-pushed the taegyunkim/profiling-eager-task-single-sample branch from 68fa2fa to e99e72d Compare November 16, 2026 16:44
@datadog-datadog-us1-prod

datadog-datadog-us1-prod Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

Pipelines  Tests

✨ Unblock PR with BitsAI

❌ Errors

Your PR has failed checks. Please review the issues below and take necessary action before merging.

🚦 1 Pipeline job failed

DataDog/apm-reliability/dd-trace-py | clang-tidy profiling — 🔧 Needs a code fix, caused by this PR

View more details · View in GitLab

⚠️ Warnings

❄️ 1 New flaky test detected

test_asyncio_eager_task_first_step_is_sampled_once[grandchild][py3.15] from test_asyncio_eager_tasks.py
Expected status 0, got 1.
=== Captured STDOUT ===
b''=== End of captured STDOUT ===
=== Captured STDERR ===
b'Traceback (most recent call last):\n  File "tests/profiling/collector/test_asyncio_eager_tasks.py", line 64, in <module>\n    assert all(pprof_utils.get_num_label(profile, sample, "task id") == task_ids[worker] for sample in samples)\n           ~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\nAssertionError\n'=== End of captured STDERR ===

View in Flaky Test Management

ℹ️ Info

No other issues found (see more)

🧪 All tests passed

Useful? React with 👍 / 👎

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: a54f185 | Docs | View more details | Give us feedback!

…'s label

The sampler reads the stack before the tasks. If the step ends in between, that tick's sample keeps the step's stack but is labelled with a creator. Accept that on the last sample instead of stopping the profiler from inside the eager task.
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