Title: Fix Windows graceful process shutdown with CTRL_C_EVENT by felixweinberger · Pull Request #1039 · modelcontextprotocol/python-sdk · GitHub
Open Graph Title: Fix Windows graceful process shutdown with CTRL_C_EVENT by felixweinberger · Pull Request #1039 · modelcontextprotocol/python-sdk
X Title: Fix Windows graceful process shutdown with CTRL_C_EVENT by felixweinberger · Pull Request #1039 · modelcontextprotocol/python-sdk
Description: Summary Enables graceful shutdown of Windows processes by using signal.CTRL_C_EVENT before forceful termination Fixes issue where cleanup code after yield in lifespan context managers was not executed on Windows Addresses GitHub Issue #1027 Problem On Windows, the cleanup procedure after yield in the lifespan context manager was not executed because process.terminate() forcefully killed processes without allowing cleanup code to run. This prevented proper resource cleanup and graceful shutdown of MCP servers on Windows. Solution Modified the Windows process termination logic to: First attempt graceful shutdown using signal.CTRL_C_EVENT (Windows-specific signal) Wait up to 2 seconds for the process to exit gracefully Fall back to terminate() if graceful shutdown fails Finally use kill() as a last resort The fix is applied to both: FallbackProcess.__aexit__: Handles process cleanup when exiting context manager terminate_windows_process(): Utility function for terminating Windows processes Technical Details Uses os.kill(pid, signal.CTRL_C_EVENT) to send interrupt signal on Windows Uses getattr(signal, "CTRL_C_EVENT") to avoid type errors on non-Windows platforms Maintains backward compatibility with existing behavior Includes proper exception handling for ProcessLookupError and OSError Test Plan All existing stdio tests pass Code passes ruff formatting and linting Code passes pyright type checking Manual testing confirms cleanup code executes properly Windows-specific testing to verify CTRL_C_EVENT behavior Verify no regression in process termination reliability Related Issues Fixes #1027: Cleanup after yield in lifespan not executed on Windows Related to #596: Fix Windows subprocess NotImplementedError Related to #575: RuntimeError during cleanup on Windows
Open Graph Description: Summary Enables graceful shutdown of Windows processes by using signal.CTRL_C_EVENT before forceful termination Fixes issue where cleanup code after yield in lifespan context managers was not exec...
X Description: Summary Enables graceful shutdown of Windows processes by using signal.CTRL_C_EVENT before forceful termination Fixes issue where cleanup code after yield in lifespan context managers was not exec...
Opengraph URL: https://github.com/modelcontextprotocol/python-sdk/pull/1039
X: @github
Domain: github.com
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:895db145-be03-0e24-46b5-4e00b93c0ebc |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | B704:28FCA6:113EC9:16696F:6A5B1582 |
| html-safe-nonce | c00650ed4e1797a965a92002688dd4884eb7eb6e097a3d9ab80307d811c3a430 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCNzA0OjI4RkNBNjoxMTNFQzk6MTY2OTZGOjZBNUIxNTgyIiwidmlzaXRvcl9pZCI6IjMzNTQxMDc3MjQ2NTMzMzU5MzgiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | bf7bab4b5ad08a0087c23e6fe09de1ed4b331d5257edf5934fa4c8db4c7be860 |
| hovercard-subject-tag | pull_request:2621088337 |
| 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/1039/files |
| twitter:image | https://avatars.githubusercontent.com/u/3823880?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/3823880?s=400&v=4 |
| og:image:alt | Summary Enables graceful shutdown of Windows processes by using signal.CTRL_C_EVENT before forceful termination Fixes issue where cleanup code after yield in lifespan context managers was not exec... |
| 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