diff --git a/packages/server/src/terminal/terminal.test.ts b/packages/server/src/terminal/terminal.test.ts index 06b1e78c44..8dceb8cddc 100644 --- a/packages/server/src/terminal/terminal.test.ts +++ b/packages/server/src/terminal/terminal.test.ts @@ -32,6 +32,7 @@ import { spawnSync } from "node:child_process"; import * as pty from "node-pty"; import xterm from "@xterm/headless"; import { delimiter, join } from "node:path"; +import { pathToFileURL } from "node:url"; import { tmpdir } from "node:os"; import { setImmediate as waitForImmediate, setTimeout as delay } from "node:timers/promises"; import { stripVTControlCharacters } from "node:util"; @@ -1621,9 +1622,8 @@ describe("killAndWait", () => { } } - // The worker's shutdown and "Restart and close terminals" end every terminal with - // killAndWait and then exit, so the program must be gone when it resolves. - it("ends a program that survives the soft kill before it resolves", async () => { + // A program that ignores SIGHUP, so only a hard kill ends it. + async function startStubbornProgram() { const dir = mkdtempSync(join(tmpdir(), "terminal-kill-and-wait-")); temporaryDirs.push(dir); const pidFile = join(dir, "program.pid"); @@ -1644,12 +1644,64 @@ describe("killAndWait", () => { const deadline = Date.now() + 10_000; while (!existsSync(pidFile) && Date.now() < deadline) await delay(50); const pid = Number(readFileSync(pidFile, "utf8")); + return { session, pid }; + } + + // The worker's shutdown and "Restart and close terminals" end every terminal with + // killAndWait and then exit, so the program must be gone when it resolves. + it("ends a program that survives the soft kill before it resolves", async () => { + const { session, pid } = await startStubbornProgram(); expect(isAlive(pid)).toBe(true); await session.killAndWait({ gracefulTimeoutMs: 0 }); expect(isAlive(pid)).toBe(false); }); + + // The terminal worker's shutdown calls killAndWait and exits at once. On Windows, node-pty's + // kill reports an exit when it closes the console, and kills the program only later, from a + // helper process whose report the exited worker never receives. + it("ends the program before the process that called it exits", async () => { + const dir = mkdtempSync(join(tmpdir(), "terminal-kill-and-exit-")); + temporaryDirs.push(dir); + const pidFile = join(dir, "program.pid"); + const script = join(dir, "kill-and-exit.mts"); + const terminalModule = pathToFileURL(join(import.meta.dirname, "terminal.ts")).href; + const program = `process.on("SIGHUP", () => {}); + require("node:fs").writeFileSync(${JSON.stringify(pidFile)}, String(process.pid)); + setInterval(() => {}, 1e9);`; + writeFileSync( + script, + `import { existsSync } from "node:fs"; + import { setTimeout as delay } from "node:timers/promises"; + const { createTerminal } = await import(${JSON.stringify(terminalModule)}); + const session = await createTerminal({ + workspaceId: "ws-test", + cwd: ${JSON.stringify(dir)}, + name: "Stubborn", + command: process.execPath, + args: ["-e", ${JSON.stringify(program)}], + }); + const deadline = Date.now() + 10_000; + while (!existsSync(${JSON.stringify(pidFile)}) && Date.now() < deadline) await delay(50); + await session.killAndWait(); + process.exit(0);`, + ); + const caller = spawnSync(process.execPath, ["--import", "tsx", script], { + cwd: join(import.meta.dirname, "../.."), + encoding: "utf8", + timeout: 30_000, + }); + expect(caller.status, caller.stderr).toBe(0); + const pid = Number(readFileSync(pidFile, "utf8")); + try { + const deadline = Date.now() + 5_000; + while (isAlive(pid) && Date.now() < deadline) await delay(100); + expect(isAlive(pid)).toBe(false); + } finally { + if (isAlive(pid)) process.kill(pid, "SIGKILL"); + } + }); }); // The escaping tests above assert the command line we generate. This one runs diff --git a/packages/server/src/terminal/terminal.ts b/packages/server/src/terminal/terminal.ts index 040924c39f..4c6fdd8867 100644 --- a/packages/server/src/terminal/terminal.ts +++ b/packages/server/src/terminal/terminal.ts @@ -1308,9 +1308,11 @@ export async function createTerminal( ptyProcess.kill(); } - // On Windows, node-pty's kill only closes the pseudoconsole and kills the console's - // processes after a helper process it forks reports them. A worker that exits right after - // killAndWait drops that kill, and the program keeps running. Kill the tree ourselves. + // On Windows, node-pty's kill closes the pseudoconsole at once and kills the console's + // processes only after a helper process it forks reports them. Closing the pseudoconsole + // closes the output pipe, and node-pty reports that as the program's exit. A worker that + // exits right after killAndWait drops the helper's kill, and the program keeps running. + // Kill the tree ourselves. async function forceKillPtyProcess(): Promise { if (process.platform !== "win32") { ptyProcess.kill("SIGKILL"); @@ -1357,6 +1359,11 @@ export async function createTerminal( } try { + // Windows has no graceful signal, and node-pty's exit event there does not prove the + // program ended (see forceKillPtyProcess). End the tree before closing the console. + if (process.platform === "win32") { + await forceKillPtyProcess(); + } killPtyProcess(); } catch { // process may already be gone