Skip to content

Commit 4353e5d

Browse files
committed
fix(core): keep the event loop free while forks run
runForks waited for each fork with spawnSync, blocking a Vitest worker's RPC with its main process for the whole run of all forks.
1 parent 8de5086 commit 4353e5d

3 files changed

Lines changed: 24 additions & 15 deletions

File tree

‎packages/core/src/isolation.ts‎

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -161,13 +161,15 @@ function isolationCommand(): string[] {
161161
* measures the benchmark like an isolated child and writes its samples to the
162162
* file named by {@link FORK_SAMPLES_FILE_ENV} instead of reporting results.
163163
*/
164-
export function runForks(uri: string, forks: number): number[] {
164+
export async function runForks(uri: string, forks: number): Promise<number[]> {
165165
const dir = mkdtempSync(join(tmpdir(), "codspeed-forks-"));
166166
try {
167167
let samples: number[] = [];
168168
for (let fork = 0; fork < forks; fork++) {
169169
const file = join(dir, `${fork}.json`);
170-
const failure = runChild(uri, { [FORK_SAMPLES_FILE_ENV]: file });
170+
const failure = await runChildAsync(uri, {
171+
[FORK_SAMPLES_FILE_ENV]: file,
172+
});
171173
if (failure !== undefined) {
172174
// process.exit() skips the `finally` below.
173175
rmSync(dir, { recursive: true, force: true });
@@ -222,14 +224,11 @@ function runInChildProcess(uri: string): void {
222224
}
223225

224226
/** Runs the benchmark command limited to `uri`; the exit code if it failed. */
225-
function runChild(
226-
uri: string,
227-
env: NodeJS.ProcessEnv = {},
228-
): number | undefined {
227+
function runChild(uri: string): number | undefined {
229228
const [executable, ...args] = isolationCommand();
230229
const result = spawnSync(executable, args, {
231230
stdio: "inherit",
232-
env: childEnv(uri, env),
231+
env: childEnv(uri),
233232
});
234233
if (result.error !== undefined) {
235234
throw startError(uri, result.error);
@@ -243,20 +242,28 @@ function runChild(
243242
* time out otherwise.
244243
*/
245244
async function runInChildProcessAsync(uri: string): Promise<void> {
245+
const failure = await runChildAsync(uri);
246+
if (failure !== undefined) {
247+
process.exit(failure);
248+
}
249+
}
250+
251+
/** {@link runChild} without blocking the event loop. */
252+
async function runChildAsync(
253+
uri: string,
254+
env: NodeJS.ProcessEnv = {},
255+
): Promise<number | undefined> {
246256
const [executable, ...args] = isolationCommand();
247257
const child = spawn(executable, args, {
248258
stdio: "inherit",
249-
env: childEnv(uri),
259+
env: childEnv(uri, env),
250260
});
251261
const { promise, resolve, reject } =
252262
Promise.withResolvers<[number | null, NodeJS.Signals | null]>();
253263
child.once("error", (error) => reject(startError(uri, error)));
254264
child.once("close", (code, signal) => resolve([code, signal]));
255265
const [status, signal] = await promise;
256-
const failure = failureCode(status, signal);
257-
if (failure !== undefined) {
258-
process.exit(failure);
259-
}
266+
return failureCode(status, signal);
260267
}
261268

262269
function childEnv(uri: string, env: NodeJS.ProcessEnv = {}): NodeJS.ProcessEnv {

‎packages/core/tests/fixtures/forks.cjs‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ if (isFork()) {
1616
const count = Number(process.env.FORKS_FIXTURE_SAMPLES);
1717
writeForkSamples(Array.from({ length: count }, (_, i) => i));
1818
} else {
19-
const samples = runForks("a", Number(process.env.FORKS_FIXTURE_COUNT));
20-
console.log(`samples ${samples.length}`);
19+
// A rejection ends the process with exit code 1.
20+
runForks("a", Number(process.env.FORKS_FIXTURE_COUNT)).then((samples) => {
21+
console.log(`samples ${samples.length}`);
22+
});
2123
}

‎packages/vitest-plugin/src/legacy/walltime.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ function patchTaskRounds(tinybench: Tinybench): void {
274274
task.loop = async () => {
275275
try {
276276
return {
277-
samples: runForks(
277+
samples: await runForks(
278278
runner.forkUri(task.name, fn),
279279
runner.parentForks!,
280280
),

0 commit comments

Comments
 (0)