Skip to content

fix(tracer): keep the subsegment open for async generators and async context managers - #8956

Open
sumedh-aerram wants to merge 1 commit into
aws-powertools:developfrom
sumedh-aerram:fix/tracer-async-generators
Open

sumedh-aerram wants to merge 1 commit into
aws-powertools:developfrom
sumedh-aerram:fix/tracer-async-generators

Conversation

@sumedh-aerram

Copy link
Copy Markdown
Contributor

Issue number:

closes #8955

Summary

Changes

capture_method sent async generator functions down the sync path, so the subsegment closed at call time and errors raised while iterating were lost. It now keeps the subsegment open with in_subsegment_async across iteration, forwarding sent values, thrown exceptions and aclose to the generator. It handles functions built by @asynccontextmanager the same way the sync context-manager path does, and other wrappers around async generators keep the old path. The Tracer docs now mention async generators.

User experience

An async generator's subsegment now covers its work (52 ms instead of 0 ms in the issue's snippet) and records its exception.

Verification

Four new tests through Tracer(provider=...) fail on develop and pass with the fix, and a fifth keeps plain wrappers working. Tracer tests pass on Python 3.10 and 3.14. The full suite (4184 passed), ruff, mypy and ty pass. I checked cancellation, asend and aclose by hand. Not tested against the X-Ray service.

I used an AI assistant (Claude) to help write this change. I ran every check and reviewed the diff myself.


By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.

@sumedh-aerram
sumedh-aerram requested a review from a team as a code owner December 4, 2026 22:14
@sumedh-aerram
sumedh-aerram requested a review from svozza December 4, 2026 22:14
@boring-cyborg boring-cyborg Bot added documentation Improvements or additions to documentation tests tracer Tracer utility labels Dec 4, 2026
@powertools-for-aws-oss-automation powertools-for-aws-oss-automation Bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Dec 4, 2026
@sonarqubecloud

sonarqubecloud Bot commented Dec 4, 2026

Copy link
Copy Markdown

This branch has not been deployed

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

Labels

documentation Improvements or additions to documentation size/L Denotes a PR that changes 100-499 lines, ignoring generated files. tests tracer Tracer utility

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Tracer capture_method closes the subsegment immediately for async generators

1 participant