Skip to content

Commit f9b3bcc

Browse files
authored
test: keep fork shutdown proof on native worker profiles (#132820)
1 parent 65ace40 commit f9b3bcc

2 files changed

Lines changed: 39 additions & 24 deletions

File tree

‎test/fixtures/vitest-fork-shutdown.mjs‎

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import assert from "node:assert/strict";
12
import fs from "node:fs";
23
import path from "node:path";
34
import { PassThrough } from "node:stream";
@@ -244,37 +245,35 @@ export default {
244245
`
245246
import fs from "node:fs";
246247
import { it, expect } from "vitest";
248+
import { threadId } from "node:worker_threads";
247249
${scenario === "hung-exit" ? `process.once("exit", () => { fs.writeFileSync(${JSON.stringify(ready)}, "exit"); Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0); });` : ""}
248250
${scenario === "bad-exit" ? 'process.once("exit", () => { process.exitCode = 23; });' : ""}
249251
it("completes the test before worker shutdown", () => {
250-
fs.writeFileSync(${JSON.stringify(receipt)}, JSON.stringify({ pid: process.pid, home: process.env.HOME }));
252+
fs.writeFileSync(${JSON.stringify(receipt)}, JSON.stringify({ pid: process.pid, threadId, home: process.env.HOME }));
251253
${fail ? 'expect.fail("intentional fixture failure");' : "expect(true).toBe(true);"}
252254
});
253255
`,
254256
);
255-
const args =
256-
scenario === "plain" || scenario === "custom"
257-
? [
258-
path.join(repo, "scripts/run-vitest.mjs"),
259-
"run",
260-
"--config",
261-
config,
262-
"--root",
263-
root,
264-
"--configLoader",
265-
"native",
266-
]
267-
: [
268-
path.join(repo, "scripts/run-vitest-profile.mts"),
269-
"runner",
270-
"--output-dir",
271-
profiles,
272-
"--",
273-
"--root",
274-
root,
275-
"--configLoader",
276-
"native",
277-
];
257+
const args = [
258+
path.join(repo, "scripts/run-vitest.mjs"),
259+
"run",
260+
"--config",
261+
config,
262+
"--root",
263+
root,
264+
"--configLoader",
265+
"native",
266+
];
267+
if (scenario !== "plain" && scenario !== "custom") {
268+
// This shutdown contract covers Node's exit-time writes, not the Inspector
269+
// profiler's awaited cleanup. Pass native flags only to the actual worker.
270+
args.push(
271+
"--execArgv=--cpu-prof",
272+
`--execArgv=--cpu-prof-dir=${profiles}`,
273+
"--execArgv=--heap-prof",
274+
`--execArgv=--heap-prof-dir=${profiles}`,
275+
);
276+
}
278277
const { child, completion } = spawnOwnedVitestProcess({
279278
command: process.execPath,
280279
args,
@@ -289,6 +288,10 @@ it("completes the test before worker shutdown", () => {
289288
output += chunk;
290289
});
291290
const { code } = await completion.finally(detachCleanup);
291+
assert.ok(
292+
fs.existsSync(receipt),
293+
`Vitest exited before the fixture test (code ${code}):\n${output}`,
294+
);
292295
const state = JSON.parse(fs.readFileSync(receipt, "utf8"));
293296
let workerStopped = false;
294297
try {
@@ -302,6 +305,12 @@ it("completes the test before worker shutdown", () => {
302305
}
303306
const counts = { cpu: 0, heap: 0 };
304307
for (const file of fs.readdirSync(profiles)) {
308+
// Native profile names contain PID/thread ID. A launcher profile cannot
309+
// establish that the worker's exit-time writes completed.
310+
const [, , , pid, workerThreadId] = file.split(".");
311+
if (pid !== String(state.pid) || workerThreadId !== String(state.threadId)) {
312+
continue;
313+
}
305314
const profile = JSON.parse(fs.readFileSync(path.join(profiles, file), "utf8"));
306315
if (file.endsWith(".cpuprofile") && profile.nodes?.length) {
307316
counts.cpu++;
@@ -314,6 +323,7 @@ it("completes the test before worker shutdown", () => {
314323
JSON.stringify({
315324
code,
316325
output,
326+
worker: { pid: state.pid, threadId: state.threadId },
317327
workerStopped,
318328
profiles: counts,
319329
homeRemoved: !fs.existsSync(state.home),

‎test/scripts/vitest-fork-shutdown.test.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ it.each([
4545
expect(result.output).toContain("intentional fixture failure");
4646
}
4747
expect(result.workerStopped).toBe(true);
48+
if (scenario === "threads") {
49+
expect(result.worker.threadId).toBeGreaterThan(0);
50+
} else {
51+
expect(result.worker.threadId).toBe(0);
52+
}
4853
if (setup !== "raw") {
4954
// Windows has no wrapper-owned group cleanup after a forced termination.
5055
// Its unfinished home remains inside this test's auto-cleaned fixture root.

0 commit comments

Comments
 (0)