Fix Windows graceful process shutdown with CTRL_C_EVENT #1039
+247
−8
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.
Summary
signal.CTRL_C_EVENT
before forceful terminationyield
in lifespan context managers was not executed on WindowsProblem
On Windows, the cleanup procedure after
yield
in thelifespan
context manager was not executed becauseprocess.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:
signal.CTRL_C_EVENT
(Windows-specific signal)terminate()
if graceful shutdown failskill()
as a last resortThe fix is applied to both:
FallbackProcess.__aexit__
: Handles process cleanup when exiting context managerterminate_windows_process()
: Utility function for terminating Windows processesTechnical Details
os.kill(pid, signal.CTRL_C_EVENT)
to send interrupt signal on Windowsgetattr(signal, "CTRL_C_EVENT")
to avoid type errors on non-Windows platformsProcessLookupError
andOSError
Test Plan
Related Issues
yield
inlifespan
not executed on Windows