Skip to content

Commit 7dd98a5

Browse files
fix(observe): preserve asyncio.timeout awareness in async generator wrapper
The _ContextPreservedAsyncGeneratorWrapper was creating a fresh asyncio.Task for every __anext__ call (via asyncio.create_task( context=...)). This changed the task identity between generator iterations, which silently broke asyncio.timeout / asyncio.timeout_at because those context managers bind to asyncio.current_task() at enter time and the bound task is already done by the next iteration. Replace the asyncio.create_task wrapping with a token-based approach that sets preserved contextvars directly on the current task. Context vars are task-local so they survive generator suspension points without changing the task identity. The same pattern is applied to aclose() for consistency. Closes langfuse/langfuse#13349
1 parent 58aee87 commit 7dd98a5

2 files changed

Lines changed: 73 additions & 42 deletions

File tree

‎langfuse/_client/observe.py‎

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -694,19 +694,24 @@ async def aclose(self) -> None:
694694
if self._span_ended:
695695
return
696696

697+
# Apply preserved context to current task without creating a new
698+
# task, so that asyncio.timeout / asyncio.current_task() bindings
699+
# remain intact (see langfuse/langfuse#13349).
700+
tokens: list[tuple[contextvars.ContextVar[Any], contextvars.Token[Any]]] = []
701+
for var in list(self.context.keys()):
702+
val = self.context.get(var)
703+
tokens.append((var, var.set(val)))
697704
try:
698705
try:
699-
await asyncio.create_task(
700-
self.generator.aclose(),
701-
context=self.context,
702-
) # type: ignore
703-
except TypeError:
704-
await self.context.run(asyncio.create_task, self.generator.aclose())
705-
except (Exception, asyncio.CancelledError) as error:
706-
self._finalize_with_error(error)
707-
raise
708-
else:
709-
self._finalize()
706+
await self.generator.aclose()
707+
except (Exception, asyncio.CancelledError) as error:
708+
self._finalize_with_error(error)
709+
raise
710+
else:
711+
self._finalize()
712+
finally:
713+
for var, token in reversed(tokens):
714+
var.reset(token)
710715

711716
async def close(self) -> None:
712717
await self.aclose()
@@ -716,19 +721,18 @@ def __del__(self) -> None:
716721

717722
async def __anext__(self) -> Any:
718723
try:
719-
# Run the generator's __anext__ in the preserved context
724+
# Apply preserved context to current task without creating a new
725+
# task, so that asyncio.timeout / asyncio.current_task() bindings
726+
# remain intact (see langfuse/langfuse#13349).
727+
tokens: list[tuple[contextvars.ContextVar[Any], contextvars.Token[Any]]] = []
728+
for var in list(self.context.keys()):
729+
val = self.context.get(var)
730+
tokens.append((var, var.set(val)))
720731
try:
721-
# Python 3.11+ approach with explicit task context
722-
item = await asyncio.create_task(
723-
self.generator.__anext__(), # type: ignore
724-
context=self.context,
725-
) # type: ignore
726-
except TypeError:
727-
# Python 3.10 fallback - create the task inside the preserved context.
728-
item = await self.context.run(
729-
asyncio.create_task,
730-
self.generator.__anext__(), # type: ignore
731-
)
732+
item = await self.generator.__anext__() # type: ignore
733+
finally:
734+
for var, token in reversed(tokens):
735+
var.reset(token)
732736

733737
if self.capture_output:
734738
self.items.append(item)

‎tests/unit/test_observe.py‎

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -271,27 +271,53 @@ async def generator() -> AsyncGenerator[str, None]:
271271

272272

273273
@pytest.mark.asyncio
274-
async def test_async_generator_wrapper_fallback_preserves_context(
275-
monkeypatch: pytest.MonkeyPatch,
276-
) -> None:
274+
async def test_async_generator_wrapper_preserves_asyncio_timeout() -> None:
275+
"""__anext__ must not create new tasks, otherwise asyncio.timeout
276+
loses track of the task it should cancel (langfuse/langfuse#13349)."""
277277
marker = contextvars.ContextVar("marker", default="ambient")
278278
seen: list[str] = []
279-
original_create_task = asyncio.create_task
280279

281-
def create_task_with_type_error(*args: Any, **kwargs: Any) -> asyncio.Task[Any]:
282-
if "context" in kwargs:
283-
raise TypeError("context argument unsupported")
280+
async def generator() -> AsyncGenerator[str, None]:
281+
for i in range(3):
282+
seen.append(marker.get())
283+
yield f"item_{i}"
284+
await asyncio.sleep(0)
285+
286+
span = SpanRecorder()
287+
context = contextvars.copy_context()
288+
context.run(marker.set, "preserved")
289+
wrapper = _ContextPreservedAsyncGeneratorWrapper(
290+
generator(),
291+
context,
292+
cast(Any, span),
293+
False,
294+
None,
295+
)
296+
297+
# asyncio.timeout should work — the generator completes before the deadline
298+
async with asyncio.timeout(1.0):
299+
items = []
300+
async for item in wrapper:
301+
items.append(item)
284302

285-
return original_create_task(*args, **kwargs)
303+
assert items == ["item_0", "item_1", "item_2"]
304+
assert seen == ["preserved", "preserved", "preserved"]
305+
assert span.ended == 1
286306

287-
monkeypatch.setattr(asyncio, "create_task", create_task_with_type_error)
307+
308+
@pytest.mark.asyncio
309+
async def test_async_generator_wrapper_respects_asyncio_timeout() -> None:
310+
"""asyncio.timeout must be able to cancel a hanging generator decorated
311+
with @observe (langfuse/langfuse#13349). Without the fix, the timeout
312+
becomes a no-op because each __anext__ creates a fresh task."""
313+
marker = contextvars.ContextVar("marker", default="ambient")
314+
seen: list[str] = []
288315

289316
async def generator() -> AsyncGenerator[str, None]:
290-
try:
291-
yield marker.get()
292-
yield "item_1"
293-
finally:
317+
for i in range(10):
294318
seen.append(marker.get())
319+
yield f"item_{i}"
320+
await asyncio.sleep(0.1)
295321

296322
span = SpanRecorder()
297323
context = contextvars.copy_context()
@@ -304,13 +330,14 @@ async def generator() -> AsyncGenerator[str, None]:
304330
None,
305331
)
306332

307-
assert await wrapper.__anext__() == "preserved"
308-
marker.set("ambient-now")
333+
with pytest.raises(asyncio.TimeoutError):
334+
async with asyncio.timeout(0.05):
335+
async for _item in wrapper:
336+
pass
309337

310-
await wrapper.aclose()
311-
312-
assert seen == ["preserved"]
313-
assert span.ended == 1
338+
# Should have yielded at least once before the timeout fired
339+
assert len(seen) >= 1
340+
assert all(v == "preserved" for v in seen)
314341

315342

316343
@pytest.mark.asyncio

0 commit comments

Comments
 (0)