Skip to content

Commit 7e2468c

Browse files
committed
Python: fix(core): close ResponseStream when the consumer is cancelled
__anext__ only caught Exception, so a CancelledError surfacing in an async map/flat_map transform, a transform hook, or a gate after the provider yielded bypassed the error path entirely and left the provider stream suspended in its async with until GC. Catch BaseException instead, run the same error-scoped cleanup and close, and re-raise. Regressions on both sides: real task cancellation mid-iteration, a hook and an async map transform raising CancelledError after a yielded update in core, and SDK stream __aexit__ firing on cancellation in the openai package. Each new test fails if the catch is narrowed back to Exception.
1 parent 8f16e57 commit 7e2468c

3 files changed

Lines changed: 116 additions & 6 deletions

File tree

‎python/packages/core/agent_framework/_types.py‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4011,16 +4011,20 @@ async def __anext__(self) -> UpdateT:
40114011
except StopAsyncIteration:
40124012
await self._finish_consumption()
40134013
raise
4014-
except Exception as exc:
4015-
# Run the error-scoped cleanup hooks first: they read
4016-
# self._stream_error, and close() would consume the one-shot
4017-
# cleanup run without it. close() still runs in finally so the
4018-
# provider stream is released even when a hook raises.
4014+
except BaseException as exc:
4015+
# CancelledError must reach close() too: a cancel landing in an
4016+
# async map/flat_map transform, hook, or gate otherwise leaves the
4017+
# provider stream suspended until GC. Hooks run first because they
4018+
# read self._stream_error, and close() would consume the one-shot
4019+
# cleanup run without it. close() stays in finally so the provider
4020+
# stream is released even when a hook raises; the original
4021+
# exception always re-raises.
40194022
try:
40204023
await self._handle_stream_error(exc)
40214024
finally:
40224025
await self.close()
40234026
raise
4027+
40244028
async def close(self) -> None:
40254029
"""Close the active iterator and run cleanup hooks.
40264030

‎python/packages/core/tests/core/test_types.py‎

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4529,7 +4529,8 @@ def failing_hook(update: ChatResponseUpdate) -> ChatResponseUpdate:
45294529
raise RuntimeError("hook blew up")
45304530

45314531
stream: ResponseStream[ChatResponseUpdate, Sequence[ChatResponseUpdate]] = ResponseStream(
4532-
updates(), transform_hooks=[failing_hook] # ty: ignore[invalid-argument-type]
4532+
updates(),
4533+
transform_hooks=[failing_hook], # ty: ignore[invalid-argument-type]
45334534
)
45344535

45354536
with pytest.raises(RuntimeError, match="hook blew up"):
@@ -4538,6 +4539,86 @@ def failing_hook(update: ChatResponseUpdate) -> ChatResponseUpdate:
45384539

45394540
assert events == ["iterator"]
45404541

4542+
async def test_cancellation_closes_iterator(self) -> None:
4543+
"""Cancelling the consumer mid-iteration releases the suspended iterator."""
4544+
events: list[str] = []
4545+
provider_suspended = asyncio.Event()
4546+
4547+
async def updates() -> AsyncIterable[ChatResponseUpdate]:
4548+
try:
4549+
yield ChatResponseUpdate(contents=[Content.from_text("first")], role="assistant")
4550+
provider_suspended.set()
4551+
await asyncio.sleep(60)
4552+
yield ChatResponseUpdate(contents=[Content.from_text("second")], role="assistant")
4553+
finally:
4554+
events.append("iterator")
4555+
4556+
stream: ResponseStream[ChatResponseUpdate, Sequence[ChatResponseUpdate]] = ResponseStream(
4557+
updates(), cleanup_hooks=[lambda: events.append("cleanup")]
4558+
)
4559+
4560+
async def consume() -> None:
4561+
async for _ in stream:
4562+
pass
4563+
4564+
task = asyncio.create_task(consume())
4565+
await provider_suspended.wait()
4566+
task.cancel()
4567+
with pytest.raises(asyncio.CancelledError):
4568+
await task
4569+
4570+
assert events == ["iterator", "cleanup"]
4571+
4572+
async def test_cancelled_error_from_hook_closes_iterator(self) -> None:
4573+
"""A hook raising CancelledError after a yielded update still closes the iterator."""
4574+
events: list[str] = []
4575+
4576+
async def updates() -> AsyncIterable[ChatResponseUpdate]:
4577+
try:
4578+
yield ChatResponseUpdate(contents=[Content.from_text("first")], role="assistant")
4579+
yield ChatResponseUpdate(contents=[Content.from_text("second")], role="assistant")
4580+
finally:
4581+
events.append("iterator")
4582+
4583+
async def cancelling_hook(update: ChatResponseUpdate) -> ChatResponseUpdate:
4584+
raise asyncio.CancelledError
4585+
4586+
stream: ResponseStream[ChatResponseUpdate, Sequence[ChatResponseUpdate]] = ResponseStream(
4587+
updates(),
4588+
transform_hooks=[cancelling_hook], # ty: ignore[invalid-argument-type]
4589+
cleanup_hooks=[lambda: events.append("cleanup")],
4590+
)
4591+
4592+
with pytest.raises(asyncio.CancelledError):
4593+
async for _ in stream:
4594+
pass
4595+
4596+
# Error path runs cleanup before close() releases the iterator.
4597+
assert events == ["cleanup", "iterator"]
4598+
4599+
async def test_cancelled_error_from_map_transform_closes_inner_stream(self) -> None:
4600+
"""A cancelled async map transform releases the wrapped provider iterator."""
4601+
events: list[str] = []
4602+
4603+
async def updates() -> AsyncIterable[ChatResponseUpdate]:
4604+
try:
4605+
yield ChatResponseUpdate(contents=[Content.from_text("first")], role="assistant")
4606+
yield ChatResponseUpdate(contents=[Content.from_text("second")], role="assistant")
4607+
finally:
4608+
events.append("iterator")
4609+
4610+
async def cancelling_transform(update: ChatResponseUpdate) -> ChatResponseUpdate:
4611+
raise asyncio.CancelledError
4612+
4613+
inner: ResponseStream[ChatResponseUpdate, Sequence[ChatResponseUpdate]] = ResponseStream(updates())
4614+
outer = inner.map(cancelling_transform, _combine_updates)
4615+
4616+
with pytest.raises(asyncio.CancelledError):
4617+
async for _ in outer:
4618+
pass
4619+
4620+
assert events == ["iterator"]
4621+
45414622
async def test_cleanup_hook_called_after_iteration(self) -> None:
45424623
"""Cleanup hook is called after iteration completes."""
45434624
cleanup_called = {"value": False}

‎python/packages/openai/tests/openai/test_openai_chat_completion_client.py‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Copyright (c) Microsoft. All rights reserved.
22

3+
import asyncio
34
import inspect
45
import json
56
import os
@@ -2248,6 +2249,30 @@ def failing_hook(update: Any) -> Any:
22482249
assert sdk_stream.closed
22492250

22502251

2252+
async def test_streaming_closes_provider_stream_on_cancellation(
2253+
openai_unit_test_env: dict[str, str],
2254+
) -> None:
2255+
"""Cancellation after a yielded update must close the SDK stream too (#8762)."""
2256+
client = OpenAIChatCompletionClient()
2257+
sdk_stream = _FakeAsyncStream([_make_content_chunk("hello"), _make_content_chunk("world")])
2258+
2259+
async def create(**kwargs: Any) -> Any:
2260+
return sdk_stream
2261+
2262+
async def cancelling_hook(update: Any) -> Any:
2263+
raise asyncio.CancelledError
2264+
2265+
with patch.object(client.client.chat.completions, "create", side_effect=create):
2266+
stream = client._inner_get_response(messages=[Message(role="user", contents=["test"])], stream=True, options={})
2267+
assert isinstance(stream, ResponseStream)
2268+
stream._transform_hooks.append(cancelling_hook)
2269+
with pytest.raises(asyncio.CancelledError):
2270+
async for _ in stream:
2271+
pass
2272+
2273+
assert sdk_stream.closed
2274+
2275+
22512276
async def test_streaming_closes_provider_stream_on_completion(
22522277
openai_unit_test_env: dict[str, str],
22532278
) -> None:

0 commit comments

Comments
 (0)