fix(observe): close abandoned generators when their span already ended - #1791
Open
DavidTraina wants to merge 3 commits into
Open
fix(observe): close abandoned generators when their span already ended#1791DavidTraina wants to merge 3 commits into
DavidTraina wants to merge 3 commits into
Conversation
11 tasks
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?
Once a wrapped generator's span has ended,
aclose()/close()return without closing the generator. But a span can end while the generator is still suspended mid-stream — for example, a client disconnect that cancels iteration before the generator resumes. The generator is then never closed: the GC finalizes it later, running itsfinallyblock under whatever request context happens to be active at that moment.In our production streaming service this fired many times a day: "Setting attribute on ended span" warnings from
finally-block span updates, running under other requests' contexts.Fix: always close the generator, even if the span already ended. Span behavior is unchanged. Also narrows a pre-existing
except TypeErrorthat could mistake an error raised by generator cleanup for the Python 3.10create_tasksignature fallback and silently swallow it.Alternative: #1792 builds on this and also preserves the generator's final span updates in the cancellation case. Pick whichever you prefer; happy to close the other.
Minimal repro (fails before, passes after)
Type of change
Verification
Checklist
code_review.md..env.templateif needed.