Skip to content

Commit 21f29bb

Browse files
Wait for a pending observation read before cancellation
1 parent 70eaa3c commit 21f29bb

1 file changed

Lines changed: 8 additions & 3 deletions

File tree

‎tests/test_native_mcp_observation.py‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -216,9 +216,14 @@ async def policy(ctx: MCPResultContext[None], value: dict[str, Any]) -> dict[str
216216
await entered.wait()
217217
while not isinstance(await anext(reader), ToolStarted):
218218
pass
219-
blocked_read = asyncio.create_task(anext(reader))
220-
await asyncio.sleep(0)
221-
blocked_read.cancel()
219+
# Drain buffered observations before cancelling a pending read.
220+
while True:
221+
blocked_read = asyncio.create_task(anext(reader))
222+
await asyncio.sleep(0)
223+
if not blocked_read.done():
224+
break
225+
blocked_read.result()
226+
assert blocked_read.cancel()
222227
with pytest.raises(asyncio.CancelledError):
223228
await blocked_read
224229
assert not fast.done()

0 commit comments

Comments
 (0)