fix(observe): defer span end when a cancellation never resumed the generator - #1792
Merged
hassiebp merged 5 commits intoJul 30, 2026
Conversation
11 tasks
Collaborator
|
Thanks for your contribution, @DavidTraina ! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
Alternative to #1791 — same bug, see there for details and a repro. Maintainers should pick one; I'll close the other.
Includes the same core fix:
aclose()/close()always close the wrapped generator, even if its span already ended, plus the sameexcept TypeErrornarrowing.On top of that: when a cancellation ends iteration before the generator ever resumed, don't end the span right away. Hold the error, close the generator first — so span updates in its
finallystill land — then end the span with the held error. The span outcome is the same ERROR/"CancelledError" as today; the difference is the generator's final updates are no longer dropped.Scope: async generators only,
CancelledErroronly, and only when the generator provably never resumed. Works on Python 3.10+ (inspect.getasyncgenstateis 3.12+, so it falls back to theag_frame/ag_runningattributes it reads).Type of change
Verification
Checklist
code_review.md..env.templateif needed.