dd-trace-py
dd-trace-py copied to clipboard
fix(tracing): preserve generator return values in @tracer.wrap() decorator
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
- [x] 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
- [x] 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
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
Bootstrap import analysis
Comparison of import times between this PR and base.
Summary
The average import time from this PR is: 278 ± 3 ms.
The average import time from base is: 280 ± 3 ms.
The import time difference between this PR and base is: -2.4 ± 0.1 ms.
Import time breakdown
The following import paths have shrunk:
ddtrace.auto
1.997 ms
(0.72%)
ddtrace.bootstrap.sitecustomize
1.323 ms
(0.48%)
ddtrace.bootstrap.preload
1.323 ms
(0.48%)
ddtrace.internal.remoteconfig.client
0.648 ms
(0.23%)
ddtrace
0.675 ms
(0.24%)
ddtrace.internal._unpatched
0.030 ms
(0.01%)
json
0.030 ms
(0.01%)
json.decoder
0.030 ms
(0.01%)
re
0.030 ms
(0.01%)
enum
0.030 ms
(0.01%)
types
0.030 ms
(0.01%)
Benchmarks
Benchmark execution time: 2025-06-26 20:59:04
Comparing candidate commit c05caa44e51b08706d7b3b8471f4cc980d657a91 in PR branch brettlangdon/fix.generator.return with baseline commit 5fdd9c23c8f9886d201335b8b2ac141fc95bc517 in branch main.
Found 0 performance improvements and 0 performance regressions! Performance is the same for 548 metrics, 2 unstable metrics.