fix(instrumentation): guarantee span ends when Anthropic stream manager exit raises - #757
Open
Aftabbs wants to merge 1 commit into
Open
fix(instrumentation): guarantee span ends when Anthropic stream manager exit raises#757Aftabbs wants to merge 1 commit into
Aftabbs wants to merge 1 commit into
Conversation
…er exit raises If MessageStreamManager.__exit__ or AsyncMessageStreamManager.__aexit__ raised an exception, post_hook_exit_impl / post_hook_aexit_impl was never called, so span.end() was skipped and the OTel span leaked. Wrap the manager exit call in try/finally so cleanup always runs. Also annotate the bare except in the token-extraction block to make clear it is intentional — metadata is best-effort and the span ends unconditionally via the new finally path.
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.
Summary
When
MessageStreamManager.__exit__orAsyncMessageStreamManager.__aexit__raises an exception,post_hook_exit_impl/post_hook_aexit_implwas never called, sospan.end()was skipped and the OTel span leaked. Every subsequent request then ran under a stale unclosed parent span.Root Cause / Motivation
In both
WrappedMessageStreamManager.__exit__andWrappedAsyncMessageStreamManager.__aexit__, the telemetry cleanup was called sequentially after the underlying manager's exit:When the underlying stream manager raises (e.g., network timeout, server error during stream teardown), Python propagates that exception before
post_hook_exit_impl()is reached, leaving the OTel span permanently open.Changes
src/judgeval/instrumentation/llm/llm_anthropic/messages_stream.py_manager.__exit__and_manager.__aexit__calls intry/finallyso span cleanup always runs; added clarifying comment to the intentional bareexceptin token-extractionsrc/tests/instrumentation/llm/anthropic/test_messages_stream.pyTesting
test_messages.py)test_span_ends_even_when_manager_exit_raises— verifies span is exported even when__exit__raises (was asserting False before fix)test_span_ends_even_when_manager_aexit_raises— same for async pathtest_span_is_createdandtest_span_records_token_usagefor both wrappersImpact
Users who experience stream teardown errors (network issues, server-side errors during streaming) previously accumulated leaked OTel spans that were never closed. This caused traces to appear as perpetually-in-progress in Judgment and could inflate trace timing metrics.