Skip to content

Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.

test: fake-platform root teardown never throws, tears down last

review8acbf84c73da
diff --git a/CLAUDE.md b/CLAUDE.md
index b292206f4..4e7aaf496 100644
--- a/CLAUDE.md
+++ b/CLAUDE.md
@@ -7277,9 +7277,14 @@ collected ~395,000 `nodeterm-fake-*` directories, `/tmp` ran out of inodes while
free, and full runs failed in 1,201 of 1,207 files with ENOSPC. Measured on 40 suites that use it:
271 directories left behind before, 0 after. Two halves, both needed: the directory is made on first
READ of `userDataDir` (a test that passes its own, or never reads it, makes none), and it is made
-under a run root that teardown removes once every worker is gone. **Per run, not per file**: a
-per-file `afterAll` would sweep a directory while a store's debounced write could still land in it,
-turning a leak into an ENOENT thrown from a timer. A run killed before teardown leaves ONE directory.
+under a run root that teardown removes once every test file has finished (vitest tears global setup
+down BEFORE it waits for its workers to exit, so a late timer is not ruled out — a per-file
+`afterAll` would be strictly worse, sweeping while that file's debounced writes are still due). The
+teardown never throws and is listed first so it runs last: vitest's teardown loop has no catch per
+file, and a throw there would silently skip the tmux sandbox's teardown. The leaf under the root is a
+bare `u-`, because every byte added is closer to the macOS unix-socket path budget
+(`hook-sock-path.ts`) for anything a test binds under `userDataDir`. A run killed before teardown
+leaves ONE directory.
`platform-fake.test.ts` fails if the root is not in effect, so dropping the `globalSetup` entry is loud.
## Conventions
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
index e7ac446bc..0b67822b8 100644
--- a/CONTRIBUTING.md
+++ b/CONTRIBUTING.md
@@ -1045,7 +1045,7 @@ production socket on purpose; adding a third is a review conversation, not a che
**A test's temp directory must go away when the run does.** `fakePlatform()`'s `userDataDir` is made
on first read under one per-run root (`test/setup/fake-platform-root.ts`), and that root is removed
-after the last worker exits. It used to be one `mkdtemp` in the system temp dir per call, never
+after the last test file finishes. It used to be one `mkdtemp` in the system temp dir per call, never
removed, and a development server collected ~395,000 of them until `/tmp` ran out of inodes and whole
runs failed with ENOSPC. If you `mkdtemp` in a test yourself, remove it in `afterEach`/`afterAll`.
diff --git a/src/core/platform-fake.test.ts b/src/core/platform-fake.test.ts
index 933924587..9a3e7d873 100644
--- a/src/core/platform-fake.test.ts
+++ b/src/core/platform-fake.test.ts
@@ -35,7 +35,7 @@ describe('fakePlatform userDataDir', () => {
const p = fakePlatform()
const dir = p.userDataDir
expect(path.dirname(dir)).toBe(root)
- expect(path.basename(dir)).toMatch(/^nodeterm-fake-/)
+ expect(path.basename(dir)).toMatch(/^u-/)
expect(fs.statSync(dir).isDirectory()).toBe(true)
expect(p.userDataDir).toBe(dir)
expect(fakePlatform().userDataDir).not.toBe(dir)
diff --git a/src/core/platform-fake.ts b/src/core/platform-fake.ts
index 9a972ae4b..97411681e 100644
--- a/src/core/platform-fake.ts
+++ b/src/core/platform-fake.ts
@@ -6,28 +6,37 @@ import type { CorePlatform } from './platform'
/**
* The per-RUN parent of every directory `fakePlatform` makes. `test/setup/fake-platform-root.ts`
* (vitest `globalSetup`) creates it in the main process, so the workers inherit the variable, and
- * removes it once every worker is gone — at that point no store's debounced write can still land
- * in a directory that was swept from under it, which a per-file `afterAll` could not promise.
+ * removes it once every test file has finished. Workers can still be shutting down at that moment
+ * (vitest tears global setup down before it waits for them), so this does not promise that no late
+ * timer runs — but a per-file `afterAll` would sweep a directory while the file's own debounced
+ * writes were still due, which is the ENOENT-from-a-timer this avoids.
*/
export const FAKE_PLATFORM_ROOT_ENV = 'NODETERM_FAKE_PLATFORM_ROOT'
/** Make the run's root and point this process (and so every worker it starts) at it. */
export function enterFakePlatformRoot(): string {
- const dir = mkdtempSync(path.join(os.tmpdir(), 'nodeterm-fake-run-'))
+ const dir = mkdtempSync(path.join(os.tmpdir(), 'nodeterm-fake-'))
process.env[FAKE_PLATFORM_ROOT_ENV] = dir
return dir
}
-/** Remove the run's root with everything made under it. */
+/** Remove the run's root with everything made under it. Throws if it cannot (the caller decides). */
export function leaveFakePlatformRoot(dir: string): void {
- rmSync(dir, { recursive: true, force: true })
+ // `force` only forgives ENOENT; a worker still closing a file (Windows EBUSY, ENOTEMPTY) gets retries.
+ rmSync(dir, { recursive: true, force: true, maxRetries: 3, retryDelay: 100 })
if (process.env[FAKE_PLATFORM_ROOT_ENV] === dir) delete process.env[FAKE_PLATFORM_ROOT_ENV]
}
-function fakeUserDataParent(): string {
+/**
+ * Where the next `userDataDir` goes, and its prefix. Under the run root the leaf is a bare `u-`:
+ * the root already names what it is, and every byte added here is a byte closer to the unix-socket
+ * path budget for anything a test binds under `userDataDir` (`hook-sock-path.ts`, 103 on macOS,
+ * whose tmpdir alone is ~48). Outside the vitest config (no globalSetup) it falls back to the
+ * system temp dir with the old recognisable name.
+ */
+function fakeUserDataTemplate(): string {
const root = process.env[FAKE_PLATFORM_ROOT_ENV]
- // Outside the vitest config (no globalSetup), fall back to the system temp dir as before.
- return root && existsSync(root) ? root : os.tmpdir()
+ return root && existsSync(root) ? path.join(root, 'u-') : path.join(os.tmpdir(), 'nodeterm-fake-')
}
export interface FakePlatform extends CorePlatform {
@@ -61,7 +70,7 @@ export function fakePlatform(overrides: Partial<CorePlatform> = {}): FakePlatfor
let userDataDir: string | undefined
const f: FakePlatform = {
get userDataDir(): string {
- return (userDataDir ??= mkdtempSync(path.join(fakeUserDataParent(), 'nodeterm-fake-')))
+ return (userDataDir ??= mkdtempSync(fakeUserDataTemplate()))
},
appVersion: '0.0.0-test',
isPackaged: false,
diff --git a/test/setup/fake-platform-root.ts b/test/setup/fake-platform-root.ts
index 36f28f12b..4caddafdc 100644
--- a/test/setup/fake-platform-root.ts
+++ b/test/setup/fake-platform-root.ts
@@ -6,10 +6,15 @@
// ENOSPC. Every one of them is now made under this directory, which lives exactly as long as the run.
//
// Per RUN, not per file, for the same reason as the tmux sandbox beside it: `setup` runs in the main
-// process, so the workers inherit the variable, and `teardown` runs after every worker is gone. A
-// per-file `afterAll` would sweep a directory while a store's debounced write could still be on its
-// way into it, and turn a leak into an ENOENT thrown from a timer. A run that is killed before its
+// process, so the workers inherit the variable, and `teardown` runs once every test file has
+// finished. A per-file `afterAll` would sweep a directory while that file's debounced writes were
+// still due, and turn a leak into an ENOENT thrown from a timer. A run that is killed before its
// teardown leaves ONE directory behind, not thousands.
+//
+// The teardown NEVER throws. vitest runs the global teardowns in reverse order in one loop with no
+// catch per file, so a throw here would skip the tmux sandbox's teardown — its servers left
+// running, its directory left behind (#629's shape) — while the run still exits 0. It is also
+// listed FIRST in vitest.config.ts, so it tears down LAST.
import { enterFakePlatformRoot, leaveFakePlatformRoot } from '../../src/core/platform-fake'
let dir: string | null = null
@@ -20,6 +25,10 @@ export async function setup(): Promise<void> {
export async function teardown(): Promise<void> {
if (!dir) return
- leaveFakePlatformRoot(dir)
+ try {
+ leaveFakePlatformRoot(dir)
+ } catch (e) {
+ console.warn(`[fake-platform-root] could not remove ${dir}; remove it by hand`, e)
+ }
dir = null
}
diff --git a/vitest.config.ts b/vitest.config.ts
index 41f698165..9631acb70 100644
--- a/vitest.config.ts
+++ b/vitest.config.ts
@@ -31,7 +31,8 @@ export default defineConfig({
// it inside each worker (and refuses if it is missing). See test/setup/tmux-sandbox.ts.
// `fake-platform-root.ts` gives the run one parent directory for every `fakePlatform()`
// userDataDir and removes it at the end — before it, each call leaked a directory in /tmp.
- globalSetup: ['test/setup/tmux-sandbox.ts', 'test/setup/fake-platform-root.ts'],
+ // Listed FIRST: teardowns run in reverse, so it tears down after the tmux sandbox.
+ globalSetup: ['test/setup/fake-platform-root.ts', 'test/setup/tmux-sandbox.ts'],
setupFiles: ['test/setup/tmux-worker-env.ts'],
// Issue #160: with the default (one worker per core), a 10-core Mac runs ~10 fs-heavy suites
// at once and transient fd exhaustion (EMFILE) turns into silent test flakiness — probes like