Repository navigation
Conversation
…andler wrapper closes In rest_stream_error_handler, error_catching_iterator wraps response.body_iterator with a bare `async for item in original_iterator: yield item`. When a streaming consumer or SSE client disconnect calls `aclose()` on the wrapper (e.g., sse-starlette's EventSourceResponse disconnect path), GeneratorExit terminates the wrapper without closing `original_iterator`, delaying or skipping its cleanup until GC. Close `original_iterator` in a `finally` block (matching validate_request_params and validate in a2a.server.request_handlers).
🧪 Code Coverage (vs
|
| Base | PR | Delta | |
|---|---|---|---|
| src/a2a/utils/error_handlers.py | 96.97% | 97.00% | 🟢 +0.03% |
| Total | 94.27% | 94.27% | ⚪️ 0.00% |
Generated by coverage-comment.yml
mykytanetipa
self-requested a review
November 26, 2026 10:21
mykytanetipa
requested changes
Nov 29, 2026
Comment on lines
+258
to
+259
| finally: | ||
| await original_iterator.aclose() |
Collaborator
There was a problem hiding this comment.
To be consistent with the rest of the codebase (_plugin_hooks.py, base_client.py, default_request_handler_v2.py), please use contextlib.aclosing here instead of the explicit finally:
try:
async with aclosing(original_iterator) as events:
async for item in events:
yield item
except Exception as stream_error:
_log_error(stream_error)
raise stream_error
Contributor
Author
There was a problem hiding this comment.
Updated in ad3e6ec to use contextlib.aclosing(original_iterator), thanks!
This branch has not been 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.
Description
Explicitly closes the wrapped
original_iteratorinside afinallyblock inrest_stream_error_handler'serror_catching_iterator, ensuring underlying SSE async generators release resources promptly when a streaming response is closed early (e.g., on client disconnect).Root Cause
In
src/a2a/utils/error_handlers.py,rest_stream_error_handlerwrapsresponse.body_iteratorwhen it is an async generator (which is true forsse-starlette'sEventSourceResponseused by REST streaming endpoints):When a client disconnects mid-stream,
EventSourceResponse._stream_responsecallsawait self.body_iterator.aclose(). Becauseerror_catching_iteratoruses a bareasync forwithout closingoriginal_iteratorinfinally,GeneratorExitterminates the outer wrapper without callingawait original_iterator.aclose(). As documented insrc/a2a/server/request_handlers/request_handler.py(validate_request_params/validate), bareasync forleaves the inner async generator'sfinallycleanup unexecuted until garbage collection.Changes
src/a2a/utils/error_handlers.py: Addfinally: await original_iterator.aclose()toerror_catching_iteratorinrest_stream_error_handler.tests/utils/test_error_handlers.py:test_rest_stream_error_handler_closes_underlying_generator_on_aclose: verifies callingawait response.body_iterator.aclose()after reading one chunk closes the wrapped async generator immediately.test_rest_stream_error_handler_event_source_disconnect_closes_inner_generator: wraps a realcreate_event_source_response(...)stream with@rest_stream_error_handler, simulates an SSE consumer disconnecting and callingaclose(), and verifies the underlying generator'sfinallyblock runs without logging a spurious error.main(b5597a23) prior to the fix (closedremainsFalse) and pass with the fix.Verification
uv run pytest tests/utils/test_error_handlers.py tests/server/routes/test_rest_routes.py tests/server/routes/test_rest_dispatcher.py -v --cov=a2a.utils.error_handlers --cov-report=term-missing: 61 passed (src/a2a/utils/error_handlers.py97% coverage, 100% of modified lines covered).uv run ruff check src tests: All checks passed.uv run ruff format --check src tests: 274 files already formatted.uv run ty check: 0 new diagnostics (only the 3 pre-existing unusedtype: ignorewarnings inclient_factory.pyandtransports/__init__.py).uv run pytest --ignore=tests/integration/cross_version/client_server/test_client_server.py --ignore=tests/integration/push_notifications/test_default_push_notification_support.py --cov=a2a -q: 2711 passed, 215 skipped, 4 xfailed, 1 xpassed (DB tests without Postgres/MySQL DSNs skipped; the two live subprocess integration test files excluded locally).Checklist
CONTRIBUTINGGuide.ruff check,ruff format --check,ty check,pytest).AI Assistance
Produced with AI assistance and verified locally by reproducing both early-close generator leak tests before applying the fix and running the repository checks above.