Title: fix: don't send log notification on transport error by maxisbey · Pull Request #2257 · modelcontextprotocol/python-sdk · GitHub
Open Graph Title: fix: don't send log notification on transport error by maxisbey · Pull Request #2257 · modelcontextprotocol/python-sdk
X Title: fix: don't send log notification on transport error by maxisbey · Pull Request #2257 · modelcontextprotocol/python-sdk
Description: When a transport yields an Exception into the server's read stream (e.g., the streamable HTTP POST handler catches an error, or JSON parsing fails on stdio/SSE/websocket), _handle_message was attempting to send_log_message back to the client. This raced with the write stream being closed during session teardown, raising ClosedResourceError inside the handler's TaskGroup and crashing the session. Fixes #1967 Fixes #2064 Supersedes #2072, #2222 Motivation and Context The send_log_message call in the case Exception() branch was added in #786 to resolve a TODO. Before that, the branch didn't exist and the match was non-exhaustive. The call introduced a shutdown race that has since been reported as #1967 and #2064. Looking at what case Exception() actually means — the transport just reported it's broken: Streamable HTTP (streamable_http.py:629-638): the client has already received an HTTP 500 before the Exception is even yielded, then terminate() closes the streams. Writing back is redundant and racy. stdio/SSE/websocket: these are JSON parse failures. A JSON-RPC -32700 parse error would be the spec-correct response — not a notifications/message with data="Internal Server Error". The TypeScript, Go, and C# SDKs all handle transport errors by logging locally only; none write back through the transport. This change aligns Python with them. The logger.error(...) at line 416 already captures the error server-side, and raise_exceptions=True still surfaces the original exception for debugging. How Has This Been Tested? Updated existing mock-based unit tests to reflect the new behavior (assert send_log_message is not called). Added test_server_run_exits_cleanly_when_transport_yields_exception_then_closes — an integration test with real anyio memory streams (no mocks, no sleeps). It deterministically reproduces the exact #1967 stack trace (ClosedResourceError in send_log_message → send_notification → _write_stream.send) on main and passes with this fix. The test also asserts the server writes nothing to the write stream. Manually verified against the HTTP-level repro from #2064 (client disconnects mid-body-read) — no longer crashes. Full suite (./scripts/test): 100% coverage. Breaking Changes Servers no longer emit notifications/message with data="Internal Server Error" when the transport layer reports an error. Given the notification was generic (no diagnostic detail) and the behavior is ~5 months old, this is unlikely to affect anyone in practice. Types of changes Bug fix (non-breaking change which fixes an issue) New feature (non-breaking change which adds functionality) Breaking change (fix or feature that would cause existing functionality to change) Documentation update Checklist I have read the MCP Documentation My code follows the repository's style guidelines New and existing tests pass locally I have added appropriate error handling I have added or updated documentation as needed Additional context Not addressed by this PR: the message.respond() race reported in a comment on #1967 (client DELETE during an in-flight tool handler → _send_response hits a closed stream). That's a distinct code path (lowlevel/server.py:480 → shared/session.py:322) and needs a separate fix.
Open Graph Description: When a transport yields an Exception into the server's read stream (e.g., the streamable HTTP POST handler catches an error, or JSON parsing fails on stdio/SSE/websocket), _handle_message was a...
X Description: When a transport yields an Exception into the server's read stream (e.g., the streamable HTTP POST handler catches an error, or JSON parsing fails on stdio/SSE/websocket), _handle_message w...
Opengraph URL: https://github.com/modelcontextprotocol/python-sdk/pull/2257
X: @github
Domain: github.com
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:56b1b48e-80d4-4ca2-e3ed-16d5f9634368 |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | 8494:3CA49:B14EB2:E798FE:6A5C256E |
| html-safe-nonce | dc39eaf676992aadd504fa154a57eef3340c8d9586dba45ed0a08e7e9c7a6a3e |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4NDk0OjNDQTQ5OkIxNEVCMjpFNzk4RkU6NkE1QzI1NkUiLCJ2aXNpdG9yX2lkIjoiNzMyNDM5ODU2OTkzNTA4Njk1OCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 3d025d9866380267000b00b0fbe7845c746d89e4f6f87641764fa83bc1bdd0b0 |
| hovercard-subject-tag | pull_request:3374262608 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/modelcontextprotocol/python-sdk/pull/2257/files |
| twitter:image | https://avatars.githubusercontent.com/u/224885523?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/224885523?s=400&v=4 |
| og:image:alt | When a transport yields an Exception into the server's read stream (e.g., the streamable HTTP POST handler catches an error, or JSON parsing fails on stdio/SSE/websocket), _handle_message was a... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | 5290d7e14309ad1e76106a9c4237bd1041517e83ea182c8ab756752cb0c6940b |
| turbo-cache-control | no-preview |
| diff-view | unified |
| go-import | github.com/modelcontextprotocol/python-sdk git https://github.com/modelcontextprotocol/python-sdk.git |
| octolytics-dimension-user_id | 182288589 |
| octolytics-dimension-user_login | modelcontextprotocol |
| octolytics-dimension-repository_id | 862584018 |
| octolytics-dimension-repository_nwo | modelcontextprotocol/python-sdk |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 862584018 |
| octolytics-dimension-repository_network_root_nwo | modelcontextprotocol/python-sdk |
| turbo-body-classes | logged-out env-production page-responsive full-width |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 9c975978430e9ad293956f2bbdaf153b1bd84a99 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width