[v1.x] Drop responses/notifications when write stream is already closed#2502
Open
bobbyo wants to merge 2 commits intomodelcontextprotocol:v1.xfrom
Open
[v1.x] Drop responses/notifications when write stream is already closed#2502bobbyo wants to merge 2 commits intomodelcontextprotocol:v1.xfrom
bobbyo wants to merge 2 commits intomodelcontextprotocol:v1.xfrom
Conversation
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.
Audience / Scope / Status
BaseSession, not only in outer server layerspyrightfailures on the first pushLast verified against:
v1.xon 2026-04-24Relevant upstream issues
ClosedResourceErrorafter client disconnect. Relevant because the failing write path here is also in the shared session layer.Summary
This PR makes
BaseSession.send_notification()andBaseSession._send_response()treat writes to an already-closed transport as expected no-ops rather than exceptions.The goal is narrow: if the peer has already gone away, a late response or notification should be dropped cleanly at the session send boundary.
Failure topology
Why the catch belongs here
mcp.server.lowlevel.serverawait message.respond(response)atsrc/mcp/server/lowlevel/server.py:799-809RequestResponderpath that reaches this exact outer frameBaseSession.send_notification()_write_stream.send(...)ClosedResourceError/BrokenResourceErrorBaseSession._send_response()_write_stream.send(...)Invariants
Changes
anyio.BrokenResourceError/anyio.ClosedResourceErrorinBaseSession.send_notification()atsrc/mcp/shared/session.py:318-339.anyio.BrokenResourceError/anyio.ClosedResourceErrorinBaseSession._send_response()atsrc/mcp/shared/session.py:343-357.tests/server/test_session.py:223-282.tests/server/test_lowlevel_exception_handling.py:78-96andsrc/mcp/server/lowlevel/server.py:799-809.Validation
Functional
PYTHONPATH=src python3 -m pytest -o addopts='' \ tests/server/test_session.py \ tests/server/test_lowlevel_exception_handling.py -qLocal result after the CI-fix follow-up:
17 passedCI follow-up
The first push exposed two CI-only problems, not a runtime regression:
testmatrixlowlevel/server.pyunexercisedtest_handle_request_drops_response_when_transport_is_closed()to cover the existing outer branchpre-commitpyrightrejected loosely typed fake session/write-stream helperstests/server/test_session.pyTargeted local follow-up verification:
Local result:
0 errors, 0 warnings, 0 informationsNon-goals
lowlevel/server.py; it keeps defense in depth.