Skip to content

Commit 92a86f9

Browse files
committed
fix(setup): forward terminal interrupts to the detached prewarm runner
A detached POSIX package-runner group no longer receives the terminal's Ctrl-C, so setup could exit while the runner kept installing in the background. The supervised launch now registers SIGINT and SIGTERM forwarding for the duration of the prewarm: the runner tree is stopped through the same family cleanup as the timeout, and the default termination is restored afterwards. An interrupted launch reports a failure reason instead of a hang.
1 parent 88dd2c1 commit 92a86f9

2 files changed

Lines changed: 51 additions & 6 deletions

File tree

‎src/application/SetupRegistrationPrewarm.test.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,17 @@ describe("materializeRegistrationRunnerCache", () => {
133133
if (result.status !== "failed") return;
134134
expect(result.reason).toContain("spawn npx ENOENT");
135135
});
136+
137+
it("reports a forwarded interrupt as a failure", async () => {
138+
const result = await materializeRegistrationRunnerCache(
139+
["npx", "-y", specifier, "mcp"],
140+
{},
141+
async () => launch({ exitCode: null, interruptedSignal: "SIGINT" }),
142+
);
143+
expect(result.status).toBe("failed");
144+
if (result.status !== "failed") return;
145+
expect(result.reason).toContain("stopped by SIGINT");
146+
});
136147
});
137148

138149
describe("launchRegistrationRunnerCommand", () => {

‎src/application/SetupRegistrationPrewarm.ts‎

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ export interface RegistrationRunnerLaunch {
2727
readonly stderr: string;
2828
readonly timedOut: boolean;
2929
readonly spawnError?: string;
30+
/** Terminal interrupt that was forwarded to the runner tree, if any. */
31+
readonly interruptedSignal?: string;
3032
}
3133

3234
/** Launch shape shared with the supervised spawn so tests can observe it. */
@@ -78,6 +80,7 @@ export const materializeRegistrationRunnerCache = async (
7880
});
7981
return launch.timedOut ||
8082
launch.spawnError !== undefined ||
83+
launch.interruptedSignal !== undefined ||
8184
launch.exitCode !== 0
8285
? { status: "failed", reason: prewarmFailureReason(launch) }
8386
: { status: "warmed" };
@@ -115,35 +118,64 @@ export const launchRegistrationRunnerCommand: RegistrationRunnerLauncher = (
115118
env: options.env,
116119
cwd: options.cwd,
117120
stdio: ["ignore", "pipe", "pipe"],
118-
// POSIX runs the runner in its own group so one signal stops the tree.
121+
// POSIX runs the runner in its own group so one signal stops the tree;
122+
// an interrupt must therefore be forwarded below, because the detached
123+
// group no longer receives the terminal's signal.
119124
...(options.platform === "win32" ? {} : { detached: true }),
120125
});
121126
const stdout = boundedStream(child.stdout);
122127
const stderr = boundedStream(child.stderr);
123128
let timedOut = false;
124129
let spawnError: string | undefined;
130+
let interruptedSignal: string | undefined;
125131
const timer = setTimeout(() => {
126132
timedOut = true;
127133
void stopRunnerTree(child, options.platform).finally(() => {
128134
child.kill("SIGKILL");
129135
});
130136
}, options.timeoutMs);
137+
const detachSignalForwarding = () => {
138+
for (const [signal, handler] of signalHandlers)
139+
process.removeListener(signal, handler);
140+
};
141+
const signalHandlers = new Map<NodeJS.Signals, () => void>(
142+
FORWARD_SIGNALS.map((signal) => [
143+
signal,
144+
() => {
145+
if (interruptedSignal !== undefined) return;
146+
interruptedSignal = signal;
147+
clearTimeout(timer);
148+
void stopRunnerTree(child, options.platform).finally(() => {
149+
child.kill("SIGKILL");
150+
detachSignalForwarding();
151+
// Restore default termination for the interrupt that arrived.
152+
process.kill(process.pid, signal);
153+
});
154+
},
155+
]),
156+
);
157+
for (const [signal, handler] of signalHandlers) process.on(signal, handler);
131158
child.on("error", (error: NodeJS.ErrnoException) => {
132159
spawnError = error.message;
133160
});
134161
child.on("close", (code, signal) => {
135162
clearTimeout(timer);
163+
detachSignalForwarding();
136164
resolve({
137165
exitCode: code ?? signal,
138166
signal: signal ?? null,
139167
stdout: stdout(),
140168
stderr: stderr(),
141169
timedOut,
142170
...(spawnError === undefined ? {} : { spawnError }),
171+
...(interruptedSignal === undefined ? {} : { interruptedSignal }),
143172
});
144173
});
145174
});
146175

176+
/** Terminal interrupts that must also stop a detached package-runner tree. */
177+
const FORWARD_SIGNALS: readonly NodeJS.Signals[] = ["SIGINT", "SIGTERM"];
178+
147179
const boundedStream = (
148180
stream: NodeJS.ReadableStream | null,
149181
): (() => string) => {
@@ -159,11 +191,13 @@ const prewarmFailureReason = (launch: RegistrationRunnerLaunch): string => {
159191
const observed =
160192
launch.spawnError !== undefined
161193
? launch.spawnError
162-
: launch.timedOut
163-
? `the package runner timed out after the bounded prewarm window`
164-
: `exit ${launch.exitCode === null ? "unknown" : String(launch.exitCode)}${
165-
launch.stderr.trim() === "" ? "" : `: ${launch.stderr.trim()}`
166-
}`;
194+
: launch.interruptedSignal !== undefined
195+
? `the package runner was stopped by ${launch.interruptedSignal}`
196+
: launch.timedOut
197+
? `the package runner timed out after the bounded prewarm window`
198+
: `exit ${launch.exitCode === null ? "unknown" : String(launch.exitCode)}${
199+
launch.stderr.trim() === "" ? "" : `: ${launch.stderr.trim()}`
200+
}`;
167201
const normalized = observed
168202
.replace(/\u001b(?:\[[0-9;?]*[ -/]*[@-~]|\([0-9A-Za-z]|[@-Z\\^_])/gu, "")
169203
.replace(/[\u0000-\u001f\u007f]+/gu, " ")

0 commit comments

Comments
 (0)