You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Python: AgentLoopMiddleware does not close the active inner stream when streaming is stopped early #9328
When a streaming AgentLoopMiddleware run is closed before the active model stream is exhausted, the middleware closes its outer async generator but does not close the inner ResponseStream currently being consumed. As a result, the inner stream's cleanup hooks do not run deterministically. Resources owned by the provider stream, such as HTTP response bodies, telemetry scopes, or other transport cleanup, can remain live until garbage collection.
The non-loop streaming path and ResponseStream wrappers propagate close() to their active inner stream. The loop middleware inserts an async-generator layer (async for update in inner: yield update) without retaining and closing inner in its finally block, so that propagation stops at the loop boundary.
To reproduce
Use a chat client whose streaming ResponseStream has a cleanup hook.
Run an Agent with AgentLoopMiddleware and stream=True.
Consume one update and call await stream.close() before the provider stream is exhausted.
Observe that the provider stream cleanup hook has not run.
A minimal regression assertion is:
stream=agent.run("start", stream=True)
awaitanext(stream)
awaitstream.close()
assertcleanup_calls==1# currently 0
Expected behavior
Closing or cancelling the outer loop stream should close the active inner ResponseStream exactly once and run its cleanup immediately. Normal full consumption should remain unchanged.
Environment
Current main (b53d25ed1cd2f54ee5e5e5ed548bb2c5515022f3)
I can work on this. I have a deterministic failing regression test and will include coverage for early close, cancellation, and normal full consumption.
Thanks for the interest. I opened this issue and already have a working fix on a branch, with regression coverage for early close, cancellation, and normal full consumption, all green against the current main. I'll push the PR shortly and link it here.
Activity
CorgiBoyG commentedon Oct 13, 2026
I can work on this. I have a deterministic failing regression test and will include coverage for early close, cancellation, and normal full consumption.
sunny-kavixo commentedon Oct 13, 2026
I can work on this. I have a deterministic failing regression test...
CorgiBoyG commentedon Oct 13, 2026
Thanks for the interest. I opened this issue and already have a working fix on a branch, with regression coverage for early close, cancellation, and normal full consumption, all green against the current main. I'll push the PR shortly and link it here.
CorgiBoyG commentedon Oct 13, 2026
Opened #9331 with the fix and the regression coverage described above.