Fix Windows graceful process shutdown with CTRL_C_EVENT#1039
Closed
felixweinberger wants to merge 2 commits intomainfrom
Closed
Fix Windows graceful process shutdown with CTRL_C_EVENT#1039felixweinberger wants to merge 2 commits intomainfrom
felixweinberger wants to merge 2 commits intomainfrom
Conversation
ihrpr
reviewed
Jun 26, 2025
Contributor
ihrpr
left a comment
There was a problem hiding this comment.
@felixweinberger is there a way to add a test for this?
Contributor
Author
Yes accidentally pushed as non-draft and planning to test on a Windows VM - working on this atm. |
30344e3 to
5e07c8e
Compare
Add comprehensive test suite for Windows-specific FallbackProcess to verify that CTRL_C_EVENT signal properly triggers cleanup code in lifespan context managers. These tests will fail until issue #1027 is fixed. Includes detailed documentation explaining why the metaprogramming approach is necessary for testing OS-level signal handling between processes. Tests include: - Graceful shutdown with CTRL_C_EVENT signal - Timeout fallback to terminate() when signal is ignored - CTRL_C_EVENT availability verification - Async stdio stream functionality Uses @pytest.mark.skipif pattern consistent with codebase conventions. Github-Issue:#1027
Implement proper graceful shutdown for Windows subprocesses by sending CTRL_C_EVENT signal before termination. This allows cleanup code in lifespan context managers to execute properly. The fix addresses issue #1027 where cleanup code after yield statements was not being executed on Windows due to forceful process termination. Changes: - Send CTRL_C_EVENT signal for graceful shutdown on Windows - Wait up to 2 seconds for process to exit gracefully - Fall back to terminate() if graceful shutdown fails - Add terminate_windows_process() helper function Github-Issue:#1027 Reported-by:Felix Weinberger
5e07c8e to
7b5ddf4
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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_EVENTbefore forceful terminationyieldin lifespan context managers was not executed on WindowsProblem
On Windows, the cleanup procedure after
yieldin thelifespancontext 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 platformsProcessLookupErrorandOSErrorTest Plan
Related Issues
yieldinlifespannot executed on Windows