diff --git a/scripts/lib/managed-child-process.mjs b/scripts/lib/managed-child-process.mjs index 9bbf8e3896e..63720614f78 100644 --- a/scripts/lib/managed-child-process.mjs +++ b/scripts/lib/managed-child-process.mjs @@ -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} */ @@ -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) => { diff --git a/test/scripts/managed-child-process.test.ts b/test/scripts/managed-child-process.test.ts index 0759cb86c19..237c17a1a95 100644 --- a/test/scripts/managed-child-process.test.ts +++ b/test/scripts/managed-child-process.test.ts @@ -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);