Skip to content

Commit 6069a03

Browse files
committed
fix(scripts): keep closed runtime command groups tracked
1 parent 0767118 commit 6069a03

2 files changed

Lines changed: 69 additions & 1 deletion

File tree

‎scripts/e2e/lib/bundled-plugin-install-uninstall/runtime-smoke.mjs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,9 @@ function trackGatewayChild(child) {
552552
function trackCommandChild(child) {
553553
activeCommandChildren.add(child);
554554
const untrack = () => {
555-
activeCommandChildren.delete(child);
555+
if (!processTreeIsAlive(child)) {
556+
activeCommandChildren.delete(child);
557+
}
556558
};
557559
child.once("error", untrack);
558560
child.once("close", untrack);

‎test/scripts/bundled-plugin-install-uninstall-probe.test.ts‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -767,6 +767,72 @@ describe("bundled plugin install/uninstall probe", () => {
767767
},
768768
);
769769

770+
it.runIf(process.platform !== "win32")(
771+
"keeps closed runtime command groups tracked for parent cleanup",
772+
async () => {
773+
const root = makePackageRoot();
774+
const commandPath = path.join(root, "closed-command.mjs");
775+
const runnerPath = path.join(root, "run-closed-runtime-command.mjs");
776+
const commandSettledPath = path.join(root, "command-settled");
777+
const descendantPidPath = path.join(root, "closed-command-descendant.pid");
778+
const descendantScript = [
779+
"import fs from 'node:fs';",
780+
`fs.writeFileSync(${JSON.stringify(descendantPidPath)}, String(process.pid));`,
781+
"process.on('SIGTERM', () => {});",
782+
"setInterval(() => {}, 1000);",
783+
].join("\n");
784+
fs.writeFileSync(
785+
commandPath,
786+
[
787+
"import childProcess from 'node:child_process';",
788+
`const child = childProcess.spawn(process.execPath, ["--input-type=module", "--eval", ${JSON.stringify(
789+
descendantScript,
790+
)}], { stdio: "ignore" });`,
791+
"child.unref();",
792+
"",
793+
].join("\n"),
794+
"utf8",
795+
);
796+
fs.writeFileSync(
797+
runnerPath,
798+
[
799+
"import fs from 'node:fs';",
800+
`const runtimeSmoke = await import(${JSON.stringify(pathToFileURL(runtimeSmokePath).href)});`,
801+
`runtimeSmoke.runCommand(process.execPath, [${JSON.stringify(commandPath)}], {`,
802+
" timeoutMs: 60_000,",
803+
"}).finally(() => {",
804+
` fs.writeFileSync(${JSON.stringify(commandSettledPath)}, "1");`,
805+
"});",
806+
"setInterval(() => {}, 1000);",
807+
"",
808+
].join("\n"),
809+
"utf8",
810+
);
811+
812+
const runner = spawn(process.execPath, [runnerPath], {
813+
stdio: "ignore",
814+
});
815+
let descendantPid: number | undefined;
816+
try {
817+
await waitForFile(descendantPidPath, 1000);
818+
descendantPid = Number(fs.readFileSync(descendantPidPath, "utf8"));
819+
expect(pidIsAlive(descendantPid)).toBe(true);
820+
await waitForFile(commandSettledPath, 1000);
821+
822+
runner.kill("SIGTERM");
823+
824+
await waitForDead(descendantPid, 2000);
825+
} finally {
826+
if (runner.pid && pidIsAlive(runner.pid)) {
827+
runner.kill("SIGKILL");
828+
}
829+
if (descendantPid !== undefined && pidIsAlive(descendantPid)) {
830+
process.kill(descendantPid, "SIGKILL");
831+
}
832+
}
833+
},
834+
);
835+
770836
it.runIf(process.platform !== "win32")(
771837
"cleans detached runtime gateway groups when the parent is signaled",
772838
async () => {

0 commit comments

Comments
 (0)