Skip to content

Commit f8d5f16

Browse files
committed
fix(ui): terminate child on wrapper shutdown
1 parent a2fdd5b commit f8d5f16

2 files changed

Lines changed: 120 additions & 3 deletions

File tree

‎scripts/ui.js‎

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,48 @@ function runSpawnCall(spawnCall, label) {
8888
return;
8989
}
9090

91+
let forwardedSignal = null;
92+
let forceKillTimer = null;
93+
// Keep UI dev children in the foreground process group for native TTY
94+
// resize/job-control behavior. Only forward direct wrapper termination.
95+
const forwardedSignals = ["SIGTERM"];
96+
const signalHandlers = new Map(
97+
forwardedSignals.map((signal) => [
98+
signal,
99+
() => {
100+
forwardedSignal ??= signal;
101+
child.kill(signal);
102+
forceKillTimer ??= setTimeout(() => child.kill("SIGKILL"), 5_000);
103+
},
104+
]),
105+
);
106+
const cleanupSignalHandlers = () => {
107+
for (const [signal, handler] of signalHandlers) {
108+
process.off(signal, handler);
109+
}
110+
if (forceKillTimer) {
111+
clearTimeout(forceKillTimer);
112+
}
113+
};
114+
for (const [signal, handler] of signalHandlers) {
115+
process.on(signal, handler);
116+
}
117+
91118
child.on("error", (err) => {
119+
cleanupSignalHandlers();
92120
console.error(`Failed to launch ${label}:`, err);
93121
process.exit(1);
94122
});
95-
child.on("exit", (code) => {
123+
child.on("exit", (code, signal) => {
124+
cleanupSignalHandlers();
125+
if (forwardedSignal) {
126+
process.kill(process.pid, forwardedSignal);
127+
return;
128+
}
129+
if (signal) {
130+
process.kill(process.pid, signal);
131+
return;
132+
}
96133
if (code !== 0) {
97134
process.exit(code ?? 1);
98135
}
@@ -118,7 +155,8 @@ function runSpawnCallSync(spawnCall, label) {
118155
return;
119156
}
120157
if (result.signal) {
121-
process.exit(1);
158+
process.kill(process.pid, result.signal);
159+
return;
122160
}
123161
if ((result.status ?? 1) !== 0) {
124162
process.exit(result.status ?? 1);

‎test/scripts/ui.test.ts‎

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { spawnSync } from "node:child_process";
1+
import { spawn, spawnSync, type ChildProcess } from "node:child_process";
22
import fs from "node:fs";
33
import os from "node:os";
44
import path from "node:path";
@@ -10,6 +10,38 @@ import {
1010
shouldUseCmdExeForCommand,
1111
} from "../../scripts/ui.js";
1212

13+
async function waitFor(predicate: () => boolean, label: string, timeoutMs = 3_000): Promise<void> {
14+
const startedAt = Date.now();
15+
while (!predicate()) {
16+
if (Date.now() - startedAt > timeoutMs) {
17+
throw new Error(`timed out waiting for ${label}`);
18+
}
19+
await new Promise<void>((resolve) => {
20+
setTimeout(resolve, 25);
21+
});
22+
}
23+
}
24+
25+
async function waitForExit(
26+
child: ChildProcess,
27+
timeoutMs = 3_000,
28+
): Promise<{ code: number | null; signal: NodeJS.Signals | null }> {
29+
return await new Promise((resolve, reject) => {
30+
const timer = setTimeout(() => {
31+
child.kill("SIGKILL");
32+
reject(new Error("timed out waiting for child exit"));
33+
}, timeoutMs);
34+
child.once("exit", (code, signal) => {
35+
clearTimeout(timer);
36+
resolve({ code, signal });
37+
});
38+
child.once("error", (error) => {
39+
clearTimeout(timer);
40+
reject(error);
41+
});
42+
});
43+
}
44+
1345
describe("scripts/ui windows spawn behavior", () => {
1446
it("wraps Windows command launchers with cmd.exe without enabling shell mode", () => {
1547
expect(
@@ -160,4 +192,51 @@ describe("scripts/ui windows spawn behavior", () => {
160192
expect(output).not.toContain("Missing UI runner");
161193
expect(output).toContain("vite");
162194
});
195+
196+
it.runIf(process.platform !== "win32")(
197+
"terminates the pnpm child on wrapper SIGTERM",
198+
async () => {
199+
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-ui-wrapper-signals-"));
200+
const runnerPath = path.join(tempDir, "pnpm.mjs");
201+
const readyFile = path.join(tempDir, "ready");
202+
const signaledFile = path.join(tempDir, "signaled");
203+
204+
fs.writeFileSync(
205+
runnerPath,
206+
[
207+
"import fs from 'node:fs';",
208+
"process.on('SIGTERM', () => {",
209+
" fs.writeFileSync(process.env.SIGNALED_FILE, 'SIGTERM');",
210+
" setTimeout(() => process.exit(0), 25);",
211+
"});",
212+
"fs.writeFileSync(process.env.READY_FILE, process.argv.slice(2).join(' '));",
213+
"setInterval(() => {}, 1000);",
214+
].join("\n"),
215+
);
216+
217+
const wrapper = spawn(process.execPath, ["scripts/ui.js", "install"], {
218+
cwd: path.resolve("."),
219+
env: {
220+
...process.env,
221+
npm_execpath: runnerPath,
222+
READY_FILE: readyFile,
223+
SIGNALED_FILE: signaledFile,
224+
},
225+
stdio: "ignore",
226+
});
227+
228+
try {
229+
await waitFor(() => fs.existsSync(readyFile), "UI runner readiness");
230+
expect(fs.readFileSync(readyFile, "utf8")).toBe("install");
231+
wrapper.kill("SIGTERM");
232+
233+
const exit = await waitForExit(wrapper);
234+
expect(exit).toEqual({ code: null, signal: "SIGTERM" });
235+
expect(fs.readFileSync(signaledFile, "utf8")).toBe("SIGTERM");
236+
} finally {
237+
wrapper.kill("SIGKILL");
238+
fs.rmSync(tempDir, { force: true, recursive: true });
239+
}
240+
},
241+
);
163242
});

0 commit comments

Comments
 (0)