Title: fix(stdio): resolve BrokenResourceError race condition on quick exit by gspeter-max · Pull Request #2079 · modelcontextprotocol/python-sdk · GitHub
Open Graph Title: fix(stdio): resolve BrokenResourceError race condition on quick exit by gspeter-max · Pull Request #2079 · modelcontextprotocol/python-sdk
X Title: fix(stdio): resolve BrokenResourceError race condition on quick exit by gspeter-max · Pull Request #2079 · modelcontextprotocol/python-sdk
Description: ROOT CAUSE: The stdio_client async context manager had a race condition when exiting quickly (before subprocess finished outputting data). The cleanup code in the finally block closed memory streams while background tasks (stdout_reader and stdin_writer) were still using them, resulting in BrokenResourceError. Timeline of the bug: User code exits the async with stdio_client(...) context The finally block executes Streams are closed immediately Background tasks are still running and trying to send/receive data Tasks encounter closed streams → BrokenResourceError CHANGES: Added BrokenResourceError to exception handlers in both client and server src/mcp/client/stdio.py: Lines 161, 177 (stdout_reader, stdin_writer) src/mcp/server/stdio.py: Lines 67, 77 (stdin_reader, stdout_writer) This allows tasks to exit gracefully if streams close during operation Added task cancellation before stream closure in client transport src/mcp/client/stdio.py: Line 210 (after process cleanup) tg.cancel_scope.cancel() sends cancellation signal to background tasks Tasks receive signal and finish their current operation Then streams are closed (tasks aren't using them anymore) Added regression test tests/client/test_stdio.py: test_stdio_client_quick_exit_race_condition Verifies that quick context exits don't cause ExceptionGroup crashes IMPACT: No more ExceptionGroup crashes when exiting quickly Graceful task shutdown with proper cancellation Backward compatible - all existing tests pass Better resource cleanup - tasks finish before streams close TECHNICAL NOTES: Server transport only needed exception handler changes (not task cancellation) because it doesn't manage subprocess lifecycle The fix uses defense-in-depth: both proper coordination AND graceful handling anyio.BrokenResourceError is raised when operations are attempted on closed resources, distinct from ClosedResourceError (resource already closed) FILES MODIFIED: src/mcp/client/stdio.py src/mcp/server/stdio.py tests/client/test_stdio.py Motivation and Context How Has This Been Tested? Breaking Changes 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
Open Graph Description: ROOT CAUSE: The stdio_client async context manager had a race condition when exiting quickly (before subprocess finished outputting data). The cleanup code in the finally block closed memory stream...
X Description: ROOT CAUSE: The stdio_client async context manager had a race condition when exiting quickly (before subprocess finished outputting data). The cleanup code in the finally block closed memory stream...
Opengraph URL: https://github.com/modelcontextprotocol/python-sdk/pull/2079
X: @github
Domain: github.com
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:5e3a70f4-25cc-cf1a-fa8c-11531e472cc2 |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | EBB8:2502F1:6370F9:853590:6A5B34E8 |
| html-safe-nonce | 343db559e8c697de208460513662b458ab9863390cf50f1eb5887a4df6e5c1bb |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFQkI4OjI1MDJGMTo2MzcwRjk6ODUzNTkwOjZBNUIzNEU4IiwidmlzaXRvcl9pZCI6Ijc0NDY5MzgxNjE1OTgxMTcwOTYiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | fe9218b79b4039c84f029f7a381d7f94ae6bfac327a848d71d022c5fc3a61409 |
| hovercard-subject-tag | pull_request:3298083791 |
| 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/2079/files |
| twitter:image | https://avatars.githubusercontent.com/u/193389584?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/193389584?s=400&v=4 |
| og:image:alt | ROOT CAUSE: The stdio_client async context manager had a race condition when exiting quickly (before subprocess finished outputting data). The cleanup code in the finally block closed memory stream... |
| 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