test(scripts): remove managed child race

This commit is contained in:
Vincent Koc
2026-04-29 12:34:19 -07:00
parent 323985f4ca
commit 3abc90aac5
2 changed files with 7 additions and 0 deletions

View File

@@ -48,6 +48,7 @@ export function terminateManagedChild(child, signal = "SIGTERM") {
* env?: NodeJS.ProcessEnv;
* stdio?: import("node:child_process").StdioOptions;
* shell?: boolean;
* onReady?: (child: import("node:child_process").ChildProcess) => void;
* }} options
* @returns {Promise<number>}
*/
@@ -58,6 +59,7 @@ export async function runManagedCommand({
env,
stdio = "inherit",
shell = process.platform === "win32",
onReady,
}) {
const child = spawn(bin, args, {
cwd,
@@ -81,6 +83,7 @@ export async function runManagedCommand({
for (const signal of FORWARDED_SIGNALS) {
process.once(signal, forwardSignal);
}
onReady?.(child);
try {
return await new Promise((resolve, reject) => {

View File

@@ -21,6 +21,7 @@ describe("managed-child-process", () => {
const childPath = path.join(dir, "child.mjs");
const runnerPath = path.join(dir, "runner.mjs");
const childPidPath = path.join(dir, "child.pid");
const runnerReadyPath = path.join(dir, "runner.ready");
const helperUrl = pathToFileURL(path.resolve("scripts/lib/managed-child-process.mjs")).href;
fs.writeFileSync(
@@ -39,12 +40,14 @@ setInterval(() => {}, 1_000);
fs.writeFileSync(
runnerPath,
`
import fs from "node:fs";
import { runManagedCommand } from ${JSON.stringify(helperUrl)};
process.exitCode = await runManagedCommand({
bin: process.execPath,
args: [${JSON.stringify(childPath)}, ${JSON.stringify(childPidPath)}],
stdio: "ignore",
onReady: () => fs.writeFileSync(${JSON.stringify(runnerReadyPath)}, "1"),
});
`,
"utf8",
@@ -56,6 +59,7 @@ process.exitCode = await runManagedCommand({
let childPid = 0;
try {
await waitFor(() => fs.existsSync(runnerReadyPath));
await waitFor(() => fs.existsSync(childPidPath));
childPid = Number(fs.readFileSync(childPidPath, "utf8"));
expect(Number.isInteger(childPid)).toBe(true);