|
3 | 3 | * process, so the allocator state, JIT state, and handles that other |
4 | 4 | * benchmarks or setup code leave behind can't change its measurement. |
5 | 5 | */ |
6 | | -import { spawnSync, type SpawnSyncReturns } from "child_process"; |
| 6 | +import { spawn, spawnSync } from "child_process"; |
7 | 7 | import { constants } from "os"; |
8 | 8 |
|
9 | 9 | /** Isolation is on unless this is `0` or `false`. */ |
@@ -82,7 +82,7 @@ export class Isolation { |
82 | 82 | await bench(); |
83 | 83 | return; |
84 | 84 | case "ProcessPerBenchmark": |
85 | | - runInChildProcess(uri); |
| 85 | + await runInChildProcessAsync(uri); |
86 | 86 | return; |
87 | 87 | } |
88 | 88 | } |
@@ -154,22 +154,53 @@ function runInChildProcess(uri: string): void { |
154 | 154 | const [executable, ...args] = isolationCommand(); |
155 | 155 | const result = spawnSync(executable, args, { |
156 | 156 | stdio: "inherit", |
157 | | - env: { ...process.env, [ISOLATED_BENCHMARK_ENV]: uri }, |
| 157 | + env: childEnv(uri), |
158 | 158 | }); |
159 | 159 | 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); |
166 | 161 | } |
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 }; |
168 | 186 | } |
169 | 187 |
|
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]); |
173 | 204 | } |
174 | | - return result.status ?? GENERIC_FAILURE_EXIT_CODE; |
| 205 | + process.exit(status ?? GENERIC_FAILURE_EXIT_CODE); |
175 | 206 | } |
0 commit comments