fix: Windows process tree cleanup in client.stop() (#1132)#1142
Open
Yogesh1290 wants to merge 1 commit intogithub:mainfrom
Open
fix: Windows process tree cleanup in client.stop() (#1132)#1142Yogesh1290 wants to merge 1 commit intogithub:mainfrom
Yogesh1290 wants to merge 1 commit intogithub:mainfrom
Conversation
Fixes child process cleanup on Windows and corrects force_stop() behavior. Problem: - Windows doesn't kill child processes when parent terminates - Original fix used single function for both graceful and force stop - force_stop() had 3-second wait, defeating 'force' semantics Solution: - Split into two focused functions (no boolean flag code smell) - _kill_process_tree(): Graceful termination with 3s grace period - _kill_process_tree_force(): Immediate kill, no waiting Changes: - Add _kill_process_tree_force() for immediate process kill - Update force_stop() to use _kill_process_tree_force() - Add comprehensive test suite for force kill behavior - Improve docstrings and inline comments Benefits: - Correct semantics: force_stop() now truly immediate - Clean code: Single Responsibility Principle, no boolean flags - Better testability: Independent test suites for each function - Self-documenting: Function names clearly indicate behavior Test Results: - 9 passed, 2 skipped (Unix tests on Windows) - 100% coverage of new code paths - All edge cases handled Fixes github#1132
0eaaedc to
11b6c34
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
Fixes Bug 2 from #1132: Windows process tree cleanup in
client.stop()andclient.force_stop().On Windows,
subprocess.Popen.terminate()only kills the parent process, leaving child processes (like MCP servers) running as orphans. This PR adds proper process tree cleanup usingpsutilon Windows.Problem
When using the Python SDK on Windows with MCP servers:
create_session(mcp_servers={...})spawns MCP server child processes undercopilot.exesession.disconnect()sendssession.destroyRPC, but MCP servers aren't killedclient.stop()callsterminate()which only killscopilot.exe, not its childrenReal-world impact: 464 node.exe + 155 twig-mcp.exe + 463 cmd.exe processes after a multi-agent workflow.
Solution
Added
_kill_process_tree()helper function that:psutil: Recursively terminates all child processespsutil: Falls back to simpleterminate()(with warning in docs)terminate()(children die with parent)Changes
python/copilot/client.py_kill_process_tree()helper function with proper error handling_kill_process_tree_force()separate function forforce_stop()that sends SIGKILL immediately with no grace period, preserving original force-stop semantics while still killing all child processesstop()to use_kill_process_tree()instead ofterminate()force_stop()to use_kill_process_tree_force()instead ofkill()HAS_PSUTILcheck usingimportlib.util.find_spec()python/pyproject.tomlpsutil>=5.9.0; sys_platform == 'win32'as Windows-only dependencypython/test_process_cleanup.py(NEW)_kill_process_tree()Testing
Behavior
Before (Bug)
After (Fixed)
Platform Support
Notes
session.destroy) still needs to be fixed in the CLIpsutilis optional but strongly recommended for Windows usersChecklist
Related
Fixes #1132 (partial - Bug 2 only)
Tested on Windows 11 with Python 3.12.2