Repository navigation
Don't record GeneratorExit as an error when a generator is closed early - #2785
Merged
alexmojaki merged 2 commits intoNov 12, 2026
Merged
Conversation
Contributor
Coverage reportClick to see where and how coverage changed
This report was generated by python-coverage-comment-action |
||||||||||||||||||||||||||||||
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.
When a generator that has a span open (either
with logfire.span(...)inside it, or@logfire.instrument(allow_generator=True)) is closed before it finishes, for example the caller doesbreak, or a streaming response's client disconnects, Python throwsGeneratorExitinto it. Right now the span records that as an exception and gets ERROR status, so a normal early stop shows up as a failed span.OpenTelemetry's
use_spanonly recordsExceptionsubclasses for this exact reason (open-telemetry/opentelemetry-python#4484). This change skipsGeneratorExitinLogfireSpan.__exit__and in the wrapped SDK span's__exit__. OtherBaseExceptions likeKeyboardInterruptare still recorded.Repro:
Tests: added
test_generator_closed_early_is_not_an_error(instrumented and manual span), and updated theGeneratorExit+ propagate case oftest_instrument_asynccontextmanager_exceptionto expect UNSET.tests/test_logfire.pyandtests/test_metrics.pypass locally.I used an AI assistant while working on this; I reproduced the issue, reviewed the change and ran the tests myself.