| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
Sorry, something went wrong.
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.
Sorry, something went wrong.
| Back | FazBrowse Home | New Git URL |
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_media relied on OTel's start_as_current_span, whose internal context.detach is unguarded, so OTel logs Failed 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_safely helper, which was built for exactly this case and already guards the baggage token. The span is ended explicitly when end_on_exit is set. Exception handling matches OTel's start_as_current_span defaults (record_exception + error status), so error observations are unchanged.
Verification
New unit test tests/unit/test_context_detach.py opens an observation, closes it on a copied asyncio context, and asserts no detach error is logged. Fails on main, passes with this change. Existing tests/unit otel/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.
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 copyReviews (1): Last reviewed commit: "fix(client): route span-activation detac..." | Re-trigger Greptile
Context used: