Skip to content

Commit fedbb53

Browse files
committed
fix(client): route span-activation detach through safe helper
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.
1 parent cd34845 commit fedbb53

2 files changed

Lines changed: 99 additions & 37 deletions

File tree

‎langfuse/_client/client.py‎

Lines changed: 58 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1339,12 +1339,20 @@ def _start_as_current_otel_span_with_processed_media(
13391339
cost_details: Optional[Dict[str, float]] = None,
13401340
prompt: Optional[PromptClient] = None,
13411341
) -> Any:
1342-
with self._otel_tracer.start_as_current_span(
1343-
name=name,
1344-
end_on_exit=end_on_exit if end_on_exit is not None else True,
1345-
) as otel_span:
1346-
baggage_token = None
1342+
# We manage span-context activation ourselves instead of using
1343+
# otel_tracer.start_as_current_span so that the context detach runs
1344+
# through _detach_context_token_safely. OTel's own detach is unguarded
1345+
# and logs "Failed to detach context" whenever __enter__ and __exit__
1346+
# straddle different async contexts (e.g. end_on_exit=False with a
1347+
# manual close), even though the observation is already complete.
1348+
otel_span = self._otel_tracer.start_span(name=name)
1349+
end_span = end_on_exit if end_on_exit is not None else True
1350+
span_context_token = otel_context_api.attach(
1351+
otel_trace_api.set_span_in_context(otel_span)
1352+
)
1353+
baggage_token = None
13471354

1355+
try:
13481356
if otel_span.is_recording():
13491357
context_with_app_root_claim = _set_langfuse_trace_id_in_baggage(
13501358
trace_id=self._get_otel_trace_id(otel_span),
@@ -1356,41 +1364,54 @@ def _start_as_current_otel_span_with_processed_media(
13561364
as_type or "generation"
13571365
) # default was "generation"
13581366

1359-
try:
1360-
common_args = {
1361-
"otel_span": otel_span,
1362-
"langfuse_client": self,
1363-
"environment": self._environment,
1364-
"release": self._release,
1365-
"input": input,
1366-
"output": output,
1367-
"metadata": metadata,
1368-
"version": version,
1369-
"level": level,
1370-
"status_message": status_message,
1371-
}
1367+
common_args = {
1368+
"otel_span": otel_span,
1369+
"langfuse_client": self,
1370+
"environment": self._environment,
1371+
"release": self._release,
1372+
"input": input,
1373+
"output": output,
1374+
"metadata": metadata,
1375+
"version": version,
1376+
"level": level,
1377+
"status_message": status_message,
1378+
}
13721379

1373-
if span_class in [
1374-
LangfuseGeneration,
1375-
LangfuseEmbedding,
1376-
]:
1377-
common_args.update(
1378-
{
1379-
"completion_start_time": completion_start_time,
1380-
"model": model,
1381-
"model_parameters": model_parameters,
1382-
"usage_details": usage_details,
1383-
"cost_details": cost_details,
1384-
"prompt": prompt,
1385-
}
1386-
)
1387-
# For span-like types (span, agent, tool, chain, retriever, evaluator, guardrail), no generation properties needed
1380+
if span_class in [
1381+
LangfuseGeneration,
1382+
LangfuseEmbedding,
1383+
]:
1384+
common_args.update(
1385+
{
1386+
"completion_start_time": completion_start_time,
1387+
"model": model,
1388+
"model_parameters": model_parameters,
1389+
"usage_details": usage_details,
1390+
"cost_details": cost_details,
1391+
"prompt": prompt,
1392+
}
1393+
)
1394+
# For span-like types (span, agent, tool, chain, retriever, evaluator, guardrail), no generation properties needed
13881395

1389-
yield span_class(**common_args) # type: ignore[arg-type]
1396+
yield span_class(**common_args) # type: ignore[arg-type]
13901397

1391-
finally:
1392-
if baggage_token is not None:
1393-
_detach_context_token_safely(baggage_token)
1398+
except Exception as exc:
1399+
if otel_span.is_recording():
1400+
otel_span.record_exception(exc)
1401+
otel_span.set_status(
1402+
otel_trace_api.Status(
1403+
status_code=otel_trace_api.StatusCode.ERROR,
1404+
description=f"{type(exc).__name__}: {exc}",
1405+
)
1406+
)
1407+
raise
1408+
1409+
finally:
1410+
if baggage_token is not None:
1411+
_detach_context_token_safely(baggage_token)
1412+
_detach_context_token_safely(span_context_token)
1413+
if end_span:
1414+
otel_span.end()
13941415

13951416
def _get_current_otel_span(self) -> Optional[otel_trace_api.Span]:
13961417
current_span = otel_trace_api.get_current_span()

‎tests/unit/test_context_detach.py‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
"""Regression tests for cross-context span detach (langfuse/langfuse#13590).
2+
3+
`start_as_current_observation(end_on_exit=False)` lets `__enter__` and `__exit__`
4+
run in different execution contexts (a manual close on another asyncio task). The
5+
OpenTelemetry span-activation token is then detached in a context it was not
6+
created in, and OTel's own `context.detach` logs `Failed to detach context` on
7+
every such close. The Langfuse span-activation detach must go through
8+
`_detach_context_token_safely`, which swallows that expected mismatch.
9+
"""
10+
11+
import asyncio
12+
import logging
13+
14+
from langfuse import Langfuse
15+
16+
17+
def _enter_and_close_across_contexts(client: Langfuse) -> None:
18+
# Attach happens here, in the current (root) context.
19+
observation = client.start_as_current_observation(
20+
name="cross-context-span", end_on_exit=False
21+
)
22+
observation.__enter__()
23+
24+
async def _close_on_other_task() -> None:
25+
# create_task copies the context, so __exit__ (and its detach) run in a
26+
# different context than the one __enter__ attached in.
27+
await asyncio.create_task(_do_exit())
28+
29+
async def _do_exit() -> None:
30+
observation.__exit__(None, None, None)
31+
32+
asyncio.run(_close_on_other_task())
33+
34+
35+
def test_cross_context_close_does_not_log_detach_error(
36+
langfuse_memory_client: Langfuse, caplog
37+
) -> None:
38+
with caplog.at_level(logging.ERROR, logger="opentelemetry.context"):
39+
_enter_and_close_across_contexts(langfuse_memory_client)
40+
41+
assert "Failed to detach context" not in caplog.text

0 commit comments

Comments
 (0)