mirror of
https://github.com/getpaseo/paseo.git
synced 2026-12-01 04:36:26 +00:00
Close terminal programs on Windows when Restart and close terminals runs (#11398)
* fix(terminal): kill the program tree first on Windows in killAndWait On Windows, node-pty's kill closes the pseudoconsole at once and kills the console's processes only after a forked helper reports them. Closing the pseudoconsole closes the output pipe, and node-pty reports that as the program's exit. killAndWait therefore saw an exit inside its graceful wait and skipped the force step added in #11147. The terminal worker exits right after killAndWait during shutdown, so the helper's kill never ran and the program kept running after "Restart and close terminals". On Windows, killAndWait now kills the program's process tree before it closes the console. Windows has no graceful signal, and node-pty's own kill was already TerminateProcess, so no graceful step is lost. Refs #11134 * test(terminal): end the program before the caller of killAndWait exits --------- Co-authored-by: paseo-bot[bot] <266920839+paseo-bot[bot]@users.noreply.github.com>
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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<void> {
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user