Title: Fix process termination in stdio client by ashrobertsdragon · Pull Request #496 · modelcontextprotocol/python-sdk · GitHub
Open Graph Title: Fix process termination in stdio client by ashrobertsdragon · Pull Request #496 · modelcontextprotocol/python-sdk
X Title: Fix process termination in stdio client by ashrobertsdragon · Pull Request #496 · modelcontextprotocol/python-sdk
Description: Github-Issue: #391 Added tg.cancel_scope.cancel() in the finally block of stdio_client to ensure that stdin_writer and stdout_reader tasks are properly cancelled This ensures associated transports are closed before invoking process.terminate() Removed the now-unnecessary terminate_windows_process() function Motivation and Context This change addresses a race condition specific to Windows' Proactor event loop, where ignored exceptions would occur during shutdown of the stdio client. The underlying problem was that subprocess transports (stdin/stdout) remained open when process.terminate() was called. On Windows, this could trigger a race during garbage collection and transport teardown, resulting in uncatchable exceptions. The previous workaround, terminate_windows_process(), attempted to mitigate this by waiting before forcefully terminating the process. However, on Windows, kill() is functionally identical to terminate()(https://docs.python.org/3/library/asyncio-subprocess.html#asyncio.subprocess.Process.kill), so the method provided no real benefit. Explicitly cancelling the stdio tasks ensures a clean and predictable shutdown. How Has This Been Tested? Tested on Windows 11 by repeatedly connecting and disconnecting multiple clients over stdio transport to multiple servers found both within the repository and using installed locally. The ignored exception could not be captured via standard exception handling (try/except, sys.unraisablehook, or pytest's capsys / capfd). As a result, an automated test was not feasible for this specific issue. All testing was conducted through repeated manual runs and verification of clean shutdown behavior. Breaking Changes No breaking changes are expected. The removed function, terminate_windows_process(), was internal and not designed for external use. Unless it was being used directly (which would be a misuse), no user code should be affected. Types of changes Bug fix (non-breaking change which fixes an issue) New feature Breaking 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 There are two failing tests that predate this change: test_messages_are_executed_concurrently fails with a timing difference of less than 5ms test_desktop fails due to assumptions about POSIX-style file paths No changes to documentation or error handling were necessary for this fix.
Open Graph Description: Github-Issue: #391 Added tg.cancel_scope.cancel() in the finally block of stdio_client to ensure that stdin_writer and stdout_reader tasks are properly cancelled This ensures associated transports...
X Description: Github-Issue: #391 Added tg.cancel_scope.cancel() in the finally block of stdio_client to ensure that stdin_writer and stdout_reader tasks are properly cancelled This ensures associated transports...
Opengraph URL: https://github.com/modelcontextprotocol/python-sdk/pull/496
X: @github
Domain: github.com
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:40a69990-ad09-6f3d-c142-49b4a6bb1be4 |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | C6A2:208386:1C17B9:25576E:6A5B1A16 |
| html-safe-nonce | 6d6fa1a431213db324d7e2ab4c19b513197b51b517ad276ae2d59a10820217cd |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDNkEyOjIwODM4NjoxQzE3Qjk6MjU1NzZFOjZBNUIxQTE2IiwidmlzaXRvcl9pZCI6IjE0NzQ2NjUzNTY2MTQxMDgzOCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 395958bf2121ff00de60a5b2947c94f9dac3c7d4b9c582c9c6294da6c9971db3 |
| hovercard-subject-tag | pull_request:2455227495 |
| 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/496/files |
| twitter:image | https://avatars.githubusercontent.com/u/128648365?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/128648365?s=400&v=4 |
| og:image:alt | Github-Issue: #391 Added tg.cancel_scope.cancel() in the finally block of stdio_client to ensure that stdin_writer and stdout_reader tasks are properly cancelled This ensures associated transports... |
| 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