Skip to content

Commit bb5d79f

Browse files
committed
fix(core): keep the event loop free while an isolated child runs
Isolation.run waited for the child with spawnSync. In a Vitest worker that blocked the worker's RPC with its main process for the whole child run, and under the simulation instrument the calls timed out (Timeout calling "onTaskUpdate"), failing the run.
1 parent 1d6e63f commit bb5d79f

1 file changed

Lines changed: 45 additions & 14 deletions

File tree

‎packages/core/src/isolation.ts‎

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* process, so the allocator state, JIT state, and handles that other
44
* benchmarks or setup code leave behind can't change its measurement.
55
*/
6-
import { spawnSync, type SpawnSyncReturns } from "child_process";
6+
import { spawn, spawnSync } from "child_process";
77
import { constants } from "os";
88

99
/** Isolation is on unless this is `0` or `false`. */
@@ -82,7 +82,7 @@ export class Isolation {
8282
await bench();
8383
return;
8484
case "ProcessPerBenchmark":
85-
runInChildProcess(uri);
85+
await runInChildProcessAsync(uri);
8686
return;
8787
}
8888
}
@@ -154,22 +154,53 @@ function runInChildProcess(uri: string): void {
154154
const [executable, ...args] = isolationCommand();
155155
const result = spawnSync(executable, args, {
156156
stdio: "inherit",
157-
env: { ...process.env, [ISOLATED_BENCHMARK_ENV]: uri },
157+
env: childEnv(uri),
158158
});
159159
if (result.error !== undefined) {
160-
throw new Error(
161-
`[CodSpeed] failed to start the process for ${uri}: ${result.error.message}`,
162-
);
163-
}
164-
if (result.status === 0) {
165-
return;
160+
throw startError(uri, result.error);
166161
}
167-
process.exit(exitCode(result));
162+
exitOnFailure(result.status, result.signal);
163+
}
164+
165+
/**
166+
* {@link runInChildProcess} without blocking the event loop while the child
167+
* runs: a Vitest worker must keep answering its main process, whose calls
168+
* time out otherwise.
169+
*/
170+
async function runInChildProcessAsync(uri: string): Promise<void> {
171+
const [executable, ...args] = isolationCommand();
172+
const child = spawn(executable, args, {
173+
stdio: "inherit",
174+
env: childEnv(uri),
175+
});
176+
const { promise, resolve, reject } =
177+
Promise.withResolvers<[number | null, NodeJS.Signals | null]>();
178+
child.once("error", (error) => reject(startError(uri, error)));
179+
child.once("close", (code, signal) => resolve([code, signal]));
180+
const [status, signal] = await promise;
181+
exitOnFailure(status, signal);
182+
}
183+
184+
function childEnv(uri: string): NodeJS.ProcessEnv {
185+
return { ...process.env, [ISOLATED_BENCHMARK_ENV]: uri };
168186
}
169187

170-
function exitCode(result: SpawnSyncReturns<Buffer>): number {
171-
if (result.signal !== null) {
172-
return SIGNAL_EXIT_CODE_OFFSET + constants.signals[result.signal];
188+
function startError(uri: string, error: Error): Error {
189+
return new Error(
190+
`[CodSpeed] failed to start the process for ${uri}: ${error.message}`,
191+
);
192+
}
193+
194+
/** Exits with a failed child's code, in the shell convention. */
195+
function exitOnFailure(
196+
status: number | null,
197+
signal: NodeJS.Signals | null,
198+
): void {
199+
if (status === 0) {
200+
return;
201+
}
202+
if (signal !== null) {
203+
process.exit(SIGNAL_EXIT_CODE_OFFSET + constants.signals[signal]);
173204
}
174-
return result.status ?? GENERIC_FAILURE_EXIT_CODE;
205+
process.exit(status ?? GENERIC_FAILURE_EXIT_CODE);
175206
}

0 commit comments

Comments
 (0)