test(scripts): remove managed child race · openclaw/openclaw@3abc90a
vincentkoc
·
2026-04-30
·
via Recent Commits to openclaw:main
| Original file line number | Diff line number | Diff line change |
|---|
@@ -48,6 +48,7 @@ export function terminateManagedChild(child, signal = "SIGTERM") {
|
48 | 48 | * env?: NodeJS.ProcessEnv; |
49 | 49 | * stdio?: import("node:child_process").StdioOptions; |
50 | 50 | * shell?: boolean; |
| 51 | + * onReady?: (child: import("node:child_process").ChildProcess) => void; |
51 | 52 | * }} options |
52 | 53 | * @returns {Promise<number>} |
53 | 54 | */ |
@@ -58,6 +59,7 @@ export async function runManagedCommand({
|
58 | 59 | env, |
59 | 60 | stdio = "inherit", |
60 | 61 | shell = process.platform === "win32", |
| 62 | + onReady, |
61 | 63 | }) { |
62 | 64 | const child = spawn(bin, args, { |
63 | 65 | cwd, |
@@ -81,6 +83,7 @@ export async function runManagedCommand({
|
81 | 83 | for (const signal of FORWARDED_SIGNALS) { |
82 | 84 | process.once(signal, forwardSignal); |
83 | 85 | } |
| 86 | +onReady?.(child); |
84 | 87 | |
85 | 88 | try { |
86 | 89 | return await new Promise((resolve, reject) => { |
|
| Original file line number | Diff line number | Diff line change |
|---|
@@ -21,6 +21,7 @@ describe("managed-child-process", () => {
|
21 | 21 | const childPath = path.join(dir, "child.mjs"); |
22 | 22 | const runnerPath = path.join(dir, "runner.mjs"); |
23 | 23 | const childPidPath = path.join(dir, "child.pid"); |
| 24 | +const runnerReadyPath = path.join(dir, "runner.ready"); |
24 | 25 | const helperUrl = pathToFileURL(path.resolve("scripts/lib/managed-child-process.mjs")).href; |
25 | 26 | |
26 | 27 | fs.writeFileSync( |
@@ -39,12 +40,14 @@ setInterval(() => {}, 1_000);
|
39 | 40 | fs.writeFileSync( |
40 | 41 | runnerPath, |
41 | 42 | ` |
| 43 | +import fs from "node:fs"; |
42 | 44 | import { runManagedCommand } from ${JSON.stringify(helperUrl)}; |
43 | 45 | |
44 | 46 | process.exitCode = await runManagedCommand({ |
45 | 47 | bin: process.execPath, |
46 | 48 | args: [${JSON.stringify(childPath)}, ${JSON.stringify(childPidPath)}], |
47 | 49 | stdio: "ignore", |
| 50 | + onReady: () => fs.writeFileSync(${JSON.stringify(runnerReadyPath)}, "1"), |
48 | 51 | }); |
49 | 52 | `, |
50 | 53 | "utf8", |
@@ -56,6 +59,7 @@ process.exitCode = await runManagedCommand({
|
56 | 59 | let childPid = 0; |
57 | 60 | |
58 | 61 | try { |
| 62 | +await waitFor(() => fs.existsSync(runnerReadyPath)); |
59 | 63 | await waitFor(() => fs.existsSync(childPidPath)); |
60 | 64 | childPid = Number(fs.readFileSync(childPidPath, "utf8")); |
61 | 65 | expect(Number.isInteger(childPid)).toBe(true); |
|
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。