Skip to content

Commit 03a9e02

Browse files
authored
fix(managed-child): honor process groups that vanish before cleanup (#119614)
1 parent e7b7d1c commit 03a9e02

2 files changed

Lines changed: 42 additions & 16 deletions

File tree

‎scripts/lib/managed-child-process.mjs‎

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,6 @@ export async function runManagedCommand({
144144
let timeoutTimer = null;
145145
let signalTimeout;
146146
let timedOut = false;
147-
let childResult;
148147
let timeoutTermination;
149148
const timeoutTriggered = new Promise((resolve) => {
150149
signalTimeout = resolve;
@@ -222,14 +221,10 @@ export async function runManagedCommand({
222221
}
223222
throw outcome.error;
224223
}
225-
childResult = outcome.status;
226224
if (requireProcessTreeExit) {
227-
await ensureManagedProcessTreeExit(child, platform, {
228-
rejectIfLive: true,
229-
terminateIfLive: true,
230-
});
225+
await ensureManagedProcessTreeExit(child, platform, { terminateIfLive: true });
231226
}
232-
return childResult;
227+
return outcome.status;
233228
} finally {
234229
if (timeoutTimer) {
235230
clearTimeout(timeoutTimer);
@@ -276,7 +271,7 @@ function processGroupStatus(pid) {
276271
async function ensureManagedProcessTreeExit(
277272
child,
278273
platform,
279-
{ rejectIfLive = false, terminateIfLive = false, windowsTermination } = {},
274+
{ terminateIfLive = false, windowsTermination } = {},
280275
) {
281276
if (platform === "win32") {
282277
if (windowsTermination?.processTreeState === "indeterminate") {
@@ -294,18 +289,18 @@ async function ensureManagedProcessTreeExit(
294289
return;
295290
}
296291
let status = initialStatus;
297-
let sawLive = initialStatus === "live";
298-
if (terminateIfLive) {
299-
terminateManagedChild(child, "SIGKILL", { platform });
300-
}
292+
// A missing group at signal time supersedes the earlier racy liveness probe.
293+
const termination = terminateIfLive
294+
? terminateManagedChild(child, "SIGKILL", { platform })
295+
: undefined;
301296
const deadline = Date.now() + PROCESS_GROUP_DRAIN_TIMEOUT_MS;
302297
while (Date.now() < deadline) {
303298
await new Promise((resolve) => {
304299
setTimeout(resolve, PROCESS_GROUP_POLL_MS);
305300
});
306301
status = processGroupStatus(child.pid);
307302
if (status === "dead") {
308-
if (rejectIfLive && sawLive) {
303+
if (terminateIfLive && termination?.processTreeState !== "terminated") {
309304
throw createManagedCommandCleanupError(
310305
"Managed command exited while its process group remained active",
311306
child,
@@ -315,9 +310,6 @@ async function ensureManagedProcessTreeExit(
315310
}
316311
return;
317312
}
318-
if (status === "live") {
319-
sawLive = true;
320-
}
321313
}
322314
const processTreeState = status === "indeterminate" ? "indeterminate" : "live";
323315
throw createManagedCommandCleanupError(

‎test/scripts/managed-child-process.test.ts‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,40 @@ setInterval(() => {}, 1_000);
418418
expect(isProcessAlive(childPid)).toBe(false);
419419
});
420420

421+
posixIt("accepts a process group that vanishes before its cleanup signal", async () => {
422+
const originalKill = process.kill;
423+
let childPid = 0;
424+
let injectedLiveGroup = false;
425+
process.kill = ((pid: number, signal?: NodeJS.Signals | number) => {
426+
if (pid === -childPid && signal === 0 && !injectedLiveGroup) {
427+
injectedLiveGroup = true;
428+
return true;
429+
}
430+
return originalKill(pid, signal);
431+
}) as typeof process.kill;
432+
433+
try {
434+
await expect(
435+
runManagedCommand({
436+
bin: process.execPath,
437+
args: ["-e", "process.exit(0)"],
438+
onReady: (child) => {
439+
childPid = expectProcessPid(child.pid);
440+
},
441+
requireProcessTreeExit: true,
442+
shell: false,
443+
stdio: "ignore",
444+
timeoutMs: 1_000,
445+
}),
446+
).resolves.toBe(0);
447+
} finally {
448+
process.kill = originalKill;
449+
}
450+
451+
expect(injectedLiveGroup).toBe(true);
452+
expect(isProcessAlive(childPid)).toBe(false);
453+
});
454+
421455
it("allows bounded retry output and normal long-running work to complete", async () => {
422456
await expect(
423457
runManagedCommand({

0 commit comments

Comments
 (0)