Skip to content

Commit b74f84a

Browse files
committed
fix: make check-tmpdir-leaks scan the same root the fix actually uses
check-tmpdir-leaks.ts was scanning os.tmpdir() for leftover run directories, but vitest-tmpdir-global-setup.ts creates them under a hard-coded /tmp. On macOS those are different paths (TMPDIR is a deep per-user /var/folders/.../T/ directory) — the guard could never find a leak on the exact platform the original leak happened on, only on Linux CI where os.tmpdir() already is /tmp. Export TEST_RUN_TMP_ROOT and TEST_RUN_TMP_PREFIX from the global-setup module and import them in the leak check instead of recomputing a path that can drift. Switched from a fixed pid-based directory name to fs.mkdtempSync so a same-named leftover from a prior killed run (or, on a shared machine, another user) can't collide with a live run. Also fixes two stale comments (in this file and ci.yml) that still described the per-file afterAll hook design that was abandoned in favor of the global setup/teardown pair, and notes the check only covers vitest runs, not the node --test lanes (test:smoke, test:integration:node). Verified live: with the old code the guard reported no leaks even with a real orphaned /tmp/agent-device-test-run-* directory present (left by a command that got killed mid-run); with this fix it correctly found and reported it.
1 parent b456704 commit b74f84a

3 files changed

Lines changed: 32 additions & 19 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -266,9 +266,9 @@ jobs:
266266
OUTPUT_ECONOMY_BASE: ${{ github.event_name == 'pull_request' && github.event.pull_request.base.sha || github.event.before }}
267267
run: pnpm test:coverage:ci
268268

269-
# Every unit-test worker redirects TMPDIR to its own directory and
270-
# removes it in an afterAll hook; anything left here means a worker was
271-
# killed before that hook ran.
269+
# Each vitest run redirects TMPDIR to one run-scoped directory and
270+
# removes it in globalTeardown; anything left here means the run was
271+
# killed before that could happen.
272272
- name: Check tmpdir leaks
273273
if: always()
274274
run: pnpm check:tmpdir-leaks

‎scripts/check-tmpdir-leaks.ts‎

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,29 @@
1-
// Fails if any `agent-device-test-run-<pid>` directory remains in the real
2-
// system tmpdir after the unit suite finishes. Each worker removes its own
3-
// directory in its vitest afterAll hook (src/__tests__/tmp-dir-setup.ts); a
4-
// leftover one means a worker was killed (crash, OOM, timeout) before that
5-
// hook ran. Run this after `pnpm test:unit`, not concurrently with it.
1+
// Fails if any agent-device-test-run-* directory remains under
2+
// TEST_RUN_TMP_ROOT after the unit suite finishes. scripts/vitest-tmpdir-global-setup.ts
3+
// creates exactly one such directory per `vitest run` invocation and removes
4+
// it in its globalTeardown; a leftover one means that invocation's process
5+
// was killed (crash, OOM, timeout) before teardown could run. Run this after
6+
// `pnpm test:unit`, not concurrently with it.
7+
//
8+
// Only covers vitest runs. node --test lanes (test:smoke,
9+
// test:integration:node, ...) still use the real os.tmpdir() unredirected.
610

711
import fs from 'node:fs';
8-
import os from 'node:os';
912
import path from 'node:path';
13+
import { TEST_RUN_TMP_PREFIX, TEST_RUN_TMP_ROOT } from './vitest-tmpdir-global-setup.ts';
1014

11-
const tmpRoot = os.tmpdir();
1215
const leaks = fs
13-
.readdirSync(tmpRoot, { withFileTypes: true })
14-
.filter((entry) => entry.isDirectory() && /^agent-device-test-run-\d+$/.test(entry.name));
16+
.readdirSync(TEST_RUN_TMP_ROOT, { withFileTypes: true })
17+
.filter((entry) => entry.isDirectory() && entry.name.startsWith(TEST_RUN_TMP_PREFIX));
1518

1619
if (leaks.length > 0) {
17-
const details = leaks.map((entry) => `- ${path.join(tmpRoot, entry.name)}`).join('\n');
20+
const details = leaks.map((entry) => `- ${path.join(TEST_RUN_TMP_ROOT, entry.name)}`).join('\n');
1821
throw new Error(
19-
`Found ${leaks.length} leftover agent-device-test-run-* director${leaks.length === 1 ? 'y' : 'ies'} in ${tmpRoot}:\n${details}\n` +
20-
'A unit-test worker was killed before its cleanup hook ran; investigate the run that produced them.',
22+
`Found ${leaks.length} leftover ${TEST_RUN_TMP_PREFIX}* director${leaks.length === 1 ? 'y' : 'ies'} in ${TEST_RUN_TMP_ROOT}:\n${details}\n` +
23+
'A vitest run was killed before its globalTeardown could run; investigate the run that produced them.',
2124
);
2225
}
2326

24-
process.stdout.write(`No leaked agent-device-test-run-* directories found in ${tmpRoot}.\n`);
27+
process.stdout.write(
28+
`No leaked ${TEST_RUN_TMP_PREFIX}* directories found in ${TEST_RUN_TMP_ROOT}.\n`,
29+
);

‎scripts/vitest-tmpdir-global-setup.ts‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,26 @@ import path from 'node:path';
77
// same process that spawns every worker, so this env mutation is inherited
88
// by all of them (confirmed: forked workers see it via normal env
99
// inheritance) and the removal below only ever runs once, after every worker
10-
// across every project has finished.
10+
// across every project has finished. A per-file afterAll hook was tried
11+
// first; it proved unreliable (some workers were torn down before running
12+
// it), which is why this is a single run-level hook instead.
1113
//
1214
// Rooted at /tmp rather than nested inside the current os.tmpdir(): macOS's
1315
// per-user TMPDIR (/var/folders/.../T/) is already close to the 104-byte
1416
// sun_path limit AF_UNIX sockets need, and tests that bind real sockets
1517
// (e.g. runner-usbmux.test.ts) started hitting EINVAL once nested one level
1618
// deeper. /tmp is short enough to leave headroom for those.
19+
//
20+
// check-tmpdir-leaks.ts imports these two rather than recomputing them, so
21+
// the two can't drift onto different directories (os.tmpdir() != /tmp on
22+
// macOS, where TMPDIR is a deep per-user path).
23+
export const TEST_RUN_TMP_ROOT = '/tmp';
24+
export const TEST_RUN_TMP_PREFIX = 'agent-device-test-run-';
25+
1726
let testRunTmpDir: string;
1827

1928
export function setup(): void {
20-
testRunTmpDir = path.join('/tmp', `agent-device-test-run-${process.pid}`);
21-
fs.mkdirSync(testRunTmpDir, { recursive: true });
29+
testRunTmpDir = fs.mkdtempSync(path.join(TEST_RUN_TMP_ROOT, TEST_RUN_TMP_PREFIX));
2230
process.env.TMPDIR = testRunTmpDir;
2331
}
2432

0 commit comments

Comments
 (0)