Skip to content

Python: AgentLoopMiddleware does not close the active inner stream when streaming is stopped early #9328

Closed

Description

@CorgiBoyG

Describe the bug

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

  1. Use a chat client whose streaming ResponseStream has a cleanup hook.
  2. Run an Agent with AgentLoopMiddleware and stream=True.
  3. Consume one update and call await stream.close() before the provider stream is exhausted.
  4. Observe that the provider stream cleanup hook has not run.

A minimal regression assertion is:

stream = agent.run("start", stream=True)
await anext(stream)
await stream.close()
assert cleanup_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)
  • Python 3.13.15
  • macOS

Activity

  1. CorgiBoyG commented on Oct 13, 2026

    @CorgiBoyG
    ContributorAuthor

    I can work on this. I have a deterministic failing regression test and will include coverage for early close, cancellation, and normal full consumption.

  2. added
    pythonUsage: [Issues, PRs], Target: Python
    triageUsage: [Issues], Target: All issues that still need to be triaged
    on Oct 13, 2026
  3. sunny-kavixo commented on Oct 13, 2026

    @sunny-kavixo

    I can work on this. I have a deterministic failing regression test...

  4. CorgiBoyG commented on Oct 13, 2026

    @CorgiBoyG
    ContributorAuthor

    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.

  5. CorgiBoyG commented on Oct 13, 2026

    @CorgiBoyG
    ContributorAuthor

    Opened #9331 with the fix and the regression coverage described above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

pythonUsage: [Issues, PRs], Target: Python

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions