Skip to content

Commit 322bfd4

Browse files
authored
fix(vitest): cap worker parallelism to prevent process storms (Fission-AI#500)
Vitest v3 defaults to `pool: "forks"` and scales worker processes with CPU count. This repo's tests spawn many Node processes (CLI invocations, temp FS operations), which can cause runaway CPU/memory usage when combined with high parallelism. Changes: - Explicitly set pool to 'forks' (tests rely on process isolation) - Add resolveMaxWorkers() to cap workers at min(4, availableCPUs) - Allow VITEST_MAX_WORKERS env override for CI/automation tuning
1 parent 08c3493 commit 322bfd4

1 file changed

Lines changed: 23 additions & 2 deletions

File tree

vitest.config.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,33 @@
11
import { defineConfig } from 'vitest/config';
2+
import os from 'node:os';
3+
4+
function resolveMaxWorkers(): number | undefined {
5+
// Allow callers (CI/agents) to override without editing config.
6+
const raw = process.env.VITEST_MAX_WORKERS;
7+
if (raw) {
8+
const parsed = Number(raw);
9+
if (Number.isFinite(parsed) && parsed > 0) {
10+
return parsed;
11+
}
12+
}
13+
14+
// Vitest v3 defaults to `pool: "forks"` and scales worker processes with CPU.
15+
// This repo's tests can spawn many Node processes (CLI invocations, temp FS),
16+
// so cap parallelism to avoid runaway CPU/memory usage in automation.
17+
const cpuCount = typeof os.availableParallelism === 'function'
18+
? os.availableParallelism()
19+
: os.cpus().length;
20+
return Math.min(4, Math.max(1, cpuCount));
21+
}
222

323
export default defineConfig({
424
test: {
525
globals: true,
626
environment: 'node',
727
globalSetup: './vitest.setup.ts',
8-
// Keep default pool settings; some tests rely on process.chdir,
9-
// which is not supported in worker threads
28+
// Tests rely on per-file process isolation (e.g., `process.cwd()` assumptions).
29+
pool: 'forks',
30+
maxWorkers: resolveMaxWorkers(),
1031
include: ['test/**/*.test.ts'],
1132
coverage: {
1233
reporter: ['text', 'json', 'html'],

0 commit comments

Comments
 (0)