fix(client): route span-activation detach through safe helper - #1807
Open
codechrl wants to merge 3 commits into
Open
fix(client): route span-activation detach through safe helper#1807codechrl wants to merge 3 commits into
codechrl wants to merge 3 commits into
Conversation
start_as_current_observation(end_on_exit=False) closed from a different async context detached the OTel span-activation token in a context it was not created in, so OTel logged 'Failed to detach context' (ValueError: Token was created in a different Context) on every close. Manage span activation directly and detach through the existing _detach_context_token_safely helper (already used for the baggage token), ending the span explicitly and preserving exception recording. Fixes langfuse/langfuse#13590.
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.
Bug
When
start_as_current_observation(end_on_exit=False)is closed from a different execution context than the one it opened in (normal in async code where the manual close runs on another task), the span-activation context token is detached in a context it was not created in._start_as_current_otel_span_with_processed_mediarelied on OTel'sstart_as_current_span, whose internalcontext.detachis unguarded, so OTel logsFailed to detach context(ValueError: Token was created in a different Context) on every such close. Langfuse only guarded its own baggage token.Fixes langfuse/langfuse#13590.
Fix
Activate the span directly (
start_span+context.attach) and detach the span-activation token through the existing_detach_context_token_safelyhelper, which was built for exactly this case and already guards the baggage token. The span is ended explicitly whenend_on_exitis set. Exception handling matches OTel'sstart_as_current_spandefaults (record_exception+ error status), so error observations are unchanged.Verification
New unit test
tests/unit/test_context_detach.pyopens an observation, closes it on a copied asyncio context, and asserts no detach error is logged. Fails onmain, passes with this change. Existingtests/unitotel/propagation/app-root suites stay green (221 passed, 2 skipped).Greptile Summary
The PR replaces OpenTelemetry’s unguarded current-span context manager with explicit span activation and safe token detachment, preserving exception recording and conditional span ending.
start_spanand activates them through the OTel context API.Confidence Score: 4/5
The implementation appears safe to merge, with a non-blocking test-isolation issue that should be addressed to prevent ambient span leakage between tests.
The production lifecycle change preserves the prior span behavior while safely handling cross-context detachment; only the regression test leaves stale ambient context after it returns.
Files Needing Attention: tests/unit/test_context_detach.py
Sequence Diagram
sequenceDiagram participant Caller participant Langfuse participant Context as OTel Context participant Span as OTel Span Caller->>Langfuse: Enter current observation Langfuse->>Span: start_span() Langfuse->>Context: attach(span context) Langfuse-->>Caller: observation wrapper Caller->>Langfuse: Exit from copied async context Langfuse->>Context: safely detach baggage token Langfuse->>Context: safely detach span token Langfuse->>Span: end() when end_on_exit Note over Context: Opening context remains unchanged when exit occurs in a copyPrompt To Fix All With AI
Reviews (1): Last reviewed commit: "fix(client): route span-activation detac..." | Re-trigger Greptile
Context used: