Repository navigation
Python: close loop inner stream on early exit - #9331
Merged
Eduard van Valkenburg (eavanvalkenburg) merged 1 commit intoOct 15, 2026
Merged
Eduard van Valkenburg (eavanvalkenburg) merged 1 commit into
Eduard van Valkenburg (eavanvalkenburg) merged 1 commit into
Conversation
Signed-off-by: CorgiBoyG <111257566+CorgiBoyG@users.noreply.github.com>
RongJie G (CorgiBoyG)
requested review from
SergeyMenshykh,
Tao Chen (TaoChenOSU),
Eduard van Valkenburg (eavanvalkenburg),
Giles Odigwe (giles17),
Jose Alvarez (jpalvarezl),
Evan Mattson (moonbox3),
Roger Barreto (rogerbarreto),
sophia-ramsey and
westey (westey-m)
as code owners
October 13, 2026 09:01
RongJie G (CorgiBoyG)
temporarily deployed
to
github-app-auth
October 13, 2026 09:01 — with
GitHub Actions
Inactive
RongJie G (CorgiBoyG)
temporarily deployed
to
github-app-auth
October 13, 2026 09:01 — with
GitHub Actions
Inactive
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
The context-manager change correctly propagates cleanup and is comprehensively covered by focused regression tests.
0 open findings
What changed in this PR
Ensures streaming agent loops deterministically release active provider streams on completion, early closure, or cancellation.
Changes:
- Manages each inner
ResponseStreamwith its async context protocol. - Adds regression coverage for all relevant termination paths.
| File | Description |
|---|---|
python/packages/core/agent_framework/_harness/_loop.py |
Closes each active inner stream deterministically. |
python/packages/core/tests/core/test_harness_loop.py |
Tests full consumption, early close, and cancellation cleanup. |
🧠 Review effort: Balanced
💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.
Eduard van Valkenburg (eavanvalkenburg)
approved these changes
Oct 15, 2026
7 tasks
This branch was previously deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
ResponseStreamas an async context managerRoot cause
AgentLoopMiddlewareforwarded updates through an async-generator layer withasync for, but did not own the inner stream through its async context protocol. Closing the outer stream therefore finalized the loop generator without propagatingclose()to the active innerResponseStream, leaving provider cleanup hooks pending until garbage collection.Using the existing
ResponseStreamasync context protocol keeps normal finalization unchanged and guarantees that early exits release the active inner stream.Fixes #9328
Validation
python -m pytest -m "not integration" -n auto --dist worksteal tests -qpython -m pytest tests/core/test_harness_loop.py -qruff check agent_framework/_harness/_loop.py tests/core/test_harness_loop.pyruff format --check agent_framework/_harness/_loop.py tests/core/test_harness_loop.pygit diff --check