copilot-sdk icon indicating copy to clipboard operation
copilot-sdk copied to clipboard

fix(python): terminate owned CLI process trees on stop

Open syf2211 opened this issue 5 months ago • 0 comments

Summary

Ensure CopilotClient.stop() and force_stop() terminate the full SDK-owned CLI process tree instead of only the top-level launcher process.

Motivation

Fixes #1804. On Windows, terminating the launcher (copilot.cmd → cmd.exe) leaves orphaned node.exe / copilot.exe descendants alive after each session. POSIX spawns also benefit from process-group teardown when children outlive the parent.

Changes

  • Add copilot._process helpers:
    • popen_process_group_kwargs() — start_new_session=True on POSIX spawns
    • terminate_owned_cli_process() — killpg on POSIX, taskkill /T on Windows, with graceful→force escalation
  • Use helpers from CopilotClient.stop() / force_stop() via asyncio.to_thread()
  • Add unit tests in test_process.py; update shutdown tests to assert tree termination calls

Tests

  • python3 -m pytest test_process.py -q — 6 passed
  • TestClientShutdown updated (full test_client.py requires bundled CLI harness)

Notes

  • External-server TCP connections are unchanged (socket terminate only).
  • Windows force path uses taskkill /F /T; graceful path uses /T first.

Fixes #1804

syf2211 avatar Jun 29 '26 18:06 syf2211