Skip to content

fix(tracing): preserve generator return values in @tracer.wrap() decorator [backport 3.9] #13806

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 3.9
Choose a base branch
from

Conversation

github-actions[bot]
Copy link
Contributor

Backport 4b87ee8 from #13784 to 3.9.

The @tracer.wrap() decorator was not properly preserving return values from generator functions. When a wrapped generator function returned a value, the StopIteration exception's value attribute was None instead of the actual returned value.

This fix ensures that generator return values are properly captured and propagated through the StopIteration exception by storing the return value from 'yield from' and returning it after the tracing context exits.

Checklist

  • PR author has checked that all the criteria below are met
  • The PR description includes an overview of the change
  • The PR description articulates the motivation for the change
  • The change includes tests OR the PR description describes a testing strategy
  • The PR description notes risks associated with the change, if any
  • Newly-added code is easy to change
  • The change follows the library release note guidelines
  • The change includes or references documentation updates if necessary
  • Backport labels are set (if applicable)

Reviewer Checklist

  • Reviewer has checked that all the criteria below are met
  • Title is accurate
  • All changes are related to the pull request's stated goal
  • Avoids breaking API changes
  • Testing strategy adequately addresses listed risks
  • Newly-added code is easy to change
  • Release note makes sense to a user of the library
  • If necessary, author has acknowledged and discussed the performance implications of this PR as reported in the benchmarks PR comment
  • Backport labels are set in a manner that is consistent with the release branch maintenance policy

@github-actions github-actions bot requested review from a team as code owners June 27, 2025 17:26
@brettlangdon brettlangdon force-pushed the backport-13784-to-3.9 branch from e0435b5 to 46096df Compare June 27, 2025 17:28
@brettlangdon brettlangdon enabled auto-merge (squash) June 27, 2025 17:29
Copy link
Contributor Author

CODEOWNERS have been resolved as:

releasenotes/notes/fix-tracer-wrap-generator-return-value-fbaec09a9670e06b.yaml  @DataDog/apm-python
ddtrace/_trace/tracer.py                                                @DataDog/apm-sdk-api-python
tests/tracer/test_tracer.py                                             @DataDog/apm-sdk-api-python

Copy link
Contributor Author

Bootstrap import analysis

Comparison of import times between this PR and base.

Summary

The average import time from this PR is: 273 ± 4 ms.

The average import time from base is: 276 ± 4 ms.

The import time difference between this PR and base is: -2.6 ± 0.2 ms.

Import time breakdown

The following import paths have shrunk:

ddtrace.auto 2.019 ms (0.74%)
ddtrace.bootstrap.sitecustomize 1.341 ms (0.49%)
ddtrace.bootstrap.preload 1.341 ms (0.49%)
ddtrace.internal.remoteconfig.client 0.641 ms (0.23%)
ddtrace 0.678 ms (0.25%)
ddtrace.internal._unpatched 0.032 ms (0.01%)
json 0.032 ms (0.01%)
json.decoder 0.032 ms (0.01%)
re 0.032 ms (0.01%)
enum 0.032 ms (0.01%)
types 0.032 ms (0.01%)

@pr-commenter
Copy link

pr-commenter bot commented Jun 27, 2025

Benchmarks

Benchmark execution time: 2025-06-27 18:19:39

Comparing candidate commit 46096df in PR branch backport-13784-to-3.9 with baseline commit e0e451c in branch 3.9.

Found 0 performance improvements and 3 performance regressions! Performance is the same for 451 metrics, 6 unstable metrics.

scenario:iastaspects-strip_aspect

  • 🟥 execution_time [+1.306µs; +1.511µs] or [+10.138%; +11.727%]

scenario:iastaspects-translate_aspect

  • 🟥 execution_time [+246.515ns; +290.458ns] or [+7.540%; +8.884%]

scenario:iastaspectsospath-ospathbasename_aspect

  • 🟥 execution_time [+697.963ns; +816.205ns] or [+16.622%; +19.438%]

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