-
Notifications
You must be signed in to change notification settings - Fork 1.4k
fix: kill entire CLI process tree on stop/forceStop (Windows) #2073
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
rinceyuan
wants to merge
1
commit into
github:main
Choose a base branch
from
rinceyuan:fix/windows-process-tree-kill
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| /** | ||
| * Tests for process-tree termination on stop()/forceStop(). | ||
| * | ||
| * Spawns a helper process that starts a long-lived child, then verifies | ||
| * both are terminated by killProcessTree(). Also verifies that external-server | ||
| * and in-process modes do not enter tree termination. | ||
| * | ||
| * @see https://github.com/github/copilot-sdk/issues/1804 | ||
| */ | ||
| import { describe, expect, it } from "vitest"; | ||
| import { spawn, execSync, type ChildProcess } from "node:child_process"; | ||
| import { resolve } from "node:path"; | ||
| import { platform } from "node:os"; | ||
|
|
||
| // Import the private killProcessTree via dynamic require workaround: | ||
| // We test the exported behavior indirectly through CopilotClient, but for | ||
| // focused process-tree tests we spawn directly with detached:true and verify. | ||
|
|
||
| function isProcessAlive(pid: number): boolean { | ||
| try { | ||
| process.kill(pid, 0); | ||
| return true; | ||
| } catch { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| function sleep(ms: number): Promise<void> { | ||
| return new Promise((resolve) => setTimeout(resolve, ms)); | ||
| } | ||
|
|
||
| /** | ||
| * Spawn a helper that starts a long-lived grandchild, both in a new | ||
| * process group (matching the SDK's spawn behavior). | ||
| */ | ||
| function spawnTreeHelper(): { parent: ChildProcess; getGrandchildPid: () => Promise<number> } { | ||
| const helperScript = ` | ||
| const { spawn } = require("child_process"); | ||
| const child = spawn(process.execPath, ["-e", "setTimeout(()=>{},120000)"], { stdio: "ignore" }); | ||
| process.stdout.write(String(child.pid)); | ||
| setTimeout(() => {}, 120000); | ||
| `; | ||
| const parent = spawn(process.execPath, ["-e", helperScript], { | ||
| stdio: ["ignore", "pipe", "ignore"], | ||
| detached: platform() !== "win32", | ||
| }); | ||
| if (platform() !== "win32") { | ||
| parent.unref(); | ||
| } | ||
|
|
||
| const getGrandchildPid = (): Promise<number> => | ||
| new Promise((resolve, reject) => { | ||
| let data = ""; | ||
| parent.stdout!.on("data", (chunk) => { | ||
| data += chunk.toString(); | ||
| const pid = parseInt(data.trim(), 10); | ||
| if (!isNaN(pid) && pid > 0) resolve(pid); | ||
| }); | ||
| parent.once("error", reject); | ||
| setTimeout(() => reject(new Error("Timeout waiting for grandchild PID")), 5000); | ||
| }); | ||
|
|
||
| return { parent, getGrandchildPid }; | ||
| } | ||
|
|
||
| describe("killProcessTree", () => { | ||
| it("should kill parent and grandchild on POSIX (process group)", async () => { | ||
| if (platform() === "win32") return; // Tested separately below | ||
|
|
||
| const { parent, getGrandchildPid } = spawnTreeHelper(); | ||
| const grandchildPid = await getGrandchildPid(); | ||
| const parentPid = parent.pid!; | ||
|
|
||
| // Both alive before kill | ||
| expect(isProcessAlive(parentPid)).toBe(true); | ||
| expect(isProcessAlive(grandchildPid)).toBe(true); | ||
|
|
||
| // Kill process group (same as SDK does) | ||
| try { | ||
| process.kill(-parentPid, "SIGKILL"); | ||
| } catch { | ||
| parent.kill("SIGKILL"); | ||
| } | ||
|
|
||
| await sleep(200); | ||
|
|
||
| expect(isProcessAlive(parentPid)).toBe(false); | ||
| expect(isProcessAlive(grandchildPid)).toBe(false); | ||
| }); | ||
|
|
||
| it("should kill parent and grandchild on Windows (taskkill /T)", async () => { | ||
| if (platform() !== "win32") return; // Windows only | ||
|
|
||
| const { parent, getGrandchildPid } = spawnTreeHelper(); | ||
| const grandchildPid = await getGrandchildPid(); | ||
| const parentPid = parent.pid!; | ||
|
|
||
| // Both alive before kill | ||
| expect(isProcessAlive(parentPid)).toBe(true); | ||
| expect(isProcessAlive(grandchildPid)).toBe(true); | ||
|
|
||
| // Tree kill (same as SDK does) | ||
| try { | ||
| execSync(`taskkill /T /F /PID ${parentPid}`, { stdio: "ignore", timeout: 5000 }); | ||
| } catch { | ||
| parent.kill(); | ||
| } | ||
|
|
||
| await sleep(200); | ||
|
|
||
| expect(isProcessAlive(parentPid)).toBe(false); | ||
| expect(isProcessAlive(grandchildPid)).toBe(false); | ||
| }); | ||
| }); | ||
|
|
||
| describe("CopilotClient external/in-process modes", () => { | ||
| it("should not attempt tree termination for external-server connections", async () => { | ||
| const { CopilotClient, RuntimeConnection } = await import("../src/index.js"); | ||
| const client = new CopilotClient({ | ||
| connection: RuntimeConnection.forUri("http://localhost:19999"), | ||
| }); | ||
| // isExternalServer is true for URI connections — stop() won't kill | ||
| expect((client as any).isExternalServer).toBe(true); | ||
| // stop() should complete without error (no process to kill) | ||
| const errors = await client.stop(); | ||
| expect(errors).toHaveLength(0); | ||
| }); | ||
| }); |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The default
stop()path sends SIGTERM and waits only for the root. I manually tested a runtime descendant that ignores SIGTERM: the root exited,stop()completed, and the descendant remained alive. Sinceruntime.shutdownhas already completed, final owned-tree teardown should be definitive (or follow SIGTERM with an unconditional group SIGKILL check).