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.

fix(pty): refuse spawns at the device ceiling before node-pty leaks another

enesa1c067f7fd62
diff --git a/src/core/pty-manager.ts b/src/core/pty-manager.ts
index 13a14e7b6..541d46e20 100644
--- a/src/core/pty-manager.ts
+++ b/src/core/pty-manager.ts
@@ -31,7 +31,13 @@ import {
} from './remote-ssh/control-master'
import { parsePaneCursor } from './pane-cursor'
import { readSpawnResources, spawnResourceNote } from './spawn-resources'
-import { primePtyCeiling, readPtyDevices, spawnFailureHint } from './pty-devices'
+import {
+ primePtyCeiling,
+ ptyDevicesExhausted,
+ readPtyDevices,
+ spawnFailureHint,
+ type PtyDevices
+} from './pty-devices'
import { REAP_SWEEP_MS, shouldReap } from './pty-reap'
import { ControlModeClient, type ControlSpawn } from './tmux-control-client'
import { TMUX_SOCKET, sessionName, isSessionName } from './tmux-naming'
@@ -1642,6 +1648,45 @@ export class PtyManager {
}
}
+ /**
+ * The sentence a caller sees when no terminal came back.
+ *
+ * One function because TWO paths now produce it: the spawn that failed (node-pty threw), and the
+ * spawn that was never attempted (the pre-flight in `spawnSession`). Both must say the same thing
+ * about the same machine — a user who is out of pty devices should not be able to tell which of
+ * the two refused them, and the exhaustion copy must exist exactly once (it lives in
+ * `spawnFailureHint`, and this is the only place that supplies its generic fallback).
+ *
+ * `archNote` is a parameter rather than a lookup because it is only ever true of a spawn that
+ * actually tried to exec the helper — see the call sites.
+ */
+ private spawnFailureError(
+ reason: string,
+ file: string,
+ cwd: string,
+ archNote: string | null,
+ devices: PtyDevices
+ ): Error {
+ // MEASURED, not guessed. node-pty discards the errno, so the old message ended every failure
+ // with the same advice — restart, or rebuild node-pty for the wrong architecture. Both are
+ // real causes and both are rare, and reading as authoritative sent at least one field report
+ // (2026-08-06) chasing an architecture that was fine. `spawnResourceNote` states what it
+ // actually counted and only names a remedy the numbers support.
+ const resources = spawnResourceNote(readSpawnResources(), this.sessions.size)
+ // ONE closing hint, picked by what was measured (`spawnFailureHint`): arch, else the system
+ // pty-device limit, else the generic guess of last resort.
+ const hint = spawnFailureHint(
+ archNote,
+ devices,
+ `If this persists, restart the app (tmux sessions survive a restart) or run ` +
+ `\`npm run rebuild\` in the repo — a release build may have rebuilt node-pty ` +
+ `for the wrong architecture.`
+ )
+ return new Error(
+ `Failed to spawn terminal (${reason}). Program: ${file}, cwd: ${cwd}, ${resources} ${hint}`
+ )
+ }
+
private spawnSession(
options: PtyCreateOptions,
/** The client this session is spawned for, or null for a relay-served (detached) pty. */
@@ -1889,6 +1934,31 @@ export class PtyManager {
args = program ? programArgs : []
}
+ // PRE-FLIGHT — refuse before node-pty is touched, not after it fails.
+ //
+ // node-pty's darwin spawn path LEAKS the pty it opened when `posix_spawn` fails:
+ // `pty_posix_spawn` (node_modules/node-pty/src/unix/pty.cc) opens the master with
+ // `posix_openpt` and the slave with `open()`, and the error branch in `PtyFork` throws without
+ // closing either — measured at 2 `/dev/ptmx` fds + 1 `/dev/ttys*` fd, i.e. 2 pty DEVICES, per
+ // failed spawn (there is a third, smaller leak of one device per SUCCESSFUL spawn from the
+ // `low_fds` cleanup loop's off-by-one). That makes exhaustion self-amplifying: at the ceiling
+ // every spawn fails, every failure eats two more devices, and each retry pushes the ceiling
+ // further away — a 31-minute-old main held 479 masters against 28 tmux panes and dozens of
+ // consecutive failed creates. The leak is node-pty's to fix; ours is to stop feeding it.
+ //
+ // FAIL-OPEN is the rule here: `ptyDevicesExhausted` is false for anything unmeasured
+ // (non-darwin, a `sysctl` that failed, or a ceiling whose async prime — kicked in `init` — has
+ // not landed yet), so an unknown machine spawns exactly as it always did. Refusing a terminal
+ // on a machine that had room would be a worse bug than the leak this avoids.
+ const devices = readPtyDevices()
+ if (ptyDevicesExhausted(devices)) {
+ // `archNote` is deliberately not consulted: it outranks the device note in
+ // `spawnFailureHint`, and no helper was exec'd here, so its architecture cannot be the
+ // reason. Saying "rebuild node-pty" to a machine that is simply full is the 2026-08-06
+ // mistake with a new cause.
+ throw this.spawnFailureError('not attempted', file, cwd, null, devices)
+ }
+
let proc: pty.IPty
try {
proc = pty.spawn(file, args, {
@@ -1904,27 +1974,11 @@ export class PtyManager {
// said: a cross-arch `electron-builder --x64` run clobbering node-pty's spawn-helper (arm64
// app can't exec an x86_64 helper), and the machine being out of pty DEVICES
// (`kern.tty.ptmx_max`, 2026-08-11 — 515 `/dev/ttys*` against a ceiling of 511).
- const openPtys = this.sessions.size
const reason = err instanceof Error ? err.message : String(err)
- const archNote = spawnHelperArchMismatch()
- // MEASURED, not guessed. node-pty discards the errno, so the old message ended every failure
- // with the same advice — restart, or rebuild node-pty for the wrong architecture. Both are
- // real causes and both are rare, and reading as authoritative sent at least one field report
- // (2026-08-06) chasing an architecture that was fine. `spawnResourceNote` states what it
- // actually counted and only names a remedy the numbers support.
- const resources = spawnResourceNote(readSpawnResources(), openPtys)
- // ONE closing hint, picked by what was measured (`spawnFailureHint`): arch, else the system
- // pty-device limit, else the generic guess of last resort.
- const hint = spawnFailureHint(
- archNote,
- readPtyDevices(),
- `If this persists, restart the app (tmux sessions survive a restart) or run ` +
- `\`npm run rebuild\` in the repo — a release build may have rebuilt node-pty ` +
- `for the wrong architecture.`
- )
- throw new Error(
- `Failed to spawn terminal (${reason}). Program: ${file}, cwd: ${cwd}, ${resources} ${hint}`
- )
+ // Re-read the devices rather than reusing the pre-flight's reading: this spawn just consumed
+ // (and, per the leak above, kept) devices of its own, so the number the user is shown should
+ // be the one that was true when the failure happened.
+ throw this.spawnFailureError(reason, file, cwd, spawnHelperArchMismatch(), readPtyDevices())
}
// tmux-backed sessions snapshot their scrollback to disk periodically so a machine reboot
diff --git a/src/core/pty-spawn-preflight.test.ts b/src/core/pty-spawn-preflight.test.ts
new file mode 100644
index 000000000..8e3535f6d
--- /dev/null
+++ b/src/core/pty-spawn-preflight.test.ts
@@ -0,0 +1,124 @@
+import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
+import { initPlatform, resetPlatformForTests } from './platform'
+import { fakePlatform, type FakePlatform } from './platform-fake'
+import { IPC } from '../shared/ipc'
+import type { PtyDevices } from './pty-devices'
+import type { PtyCreateResult } from '../shared/types'
+
+/**
+ * THE SPAWN THAT IS NEVER ATTEMPTED.
+ *
+ * node-pty's darwin spawn path leaks the pty it opened when `posix_spawn` fails: `pty_posix_spawn`
+ * (node_modules/node-pty/src/unix/pty.cc) opens the master with `posix_openpt` and the slave with
+ * `open()`, and on the error branch `PtyFork` throws without closing either — measured at exactly 2
+ * `/dev/ptmx` fds + 1 `/dev/ttys*` fd, i.e. 2 pty DEVICES, per failed spawn.
+ *
+ * That makes device exhaustion self-amplifying: at the ceiling every spawn fails, every failure
+ * eats two more devices, and the ceiling gets further away (a 31-minute-old main held 479 masters
+ * against 28 tmux panes). We cannot fix node-pty from here, so the fix is to stop feeding it: when
+ * the machine is already out of devices, the spawn is refused BEFORE node-pty is called at all.
+ *
+ * The refusal is only ever allowed on a MEASURED reading. A wrong block is worse than a leak — it
+ * would refuse terminals on a healthy machine — so anything unmeasured spawns as it always did.
+ */
+
+/** Every node-pty call is recorded, because "was it called" is the whole subject here. */
+const spawned: Array<{ file: string }> = []
+vi.mock('node-pty', () => ({
+ spawn: (file: string) => {
+ spawned.push({ file })
+ return {
+ onData: () => {},
+ onExit: () => {},
+ write: () => {},
+ resize: () => {},
+ pause: () => {},
+ resume: () => {},
+ kill: () => {},
+ pid: 4321
+ }
+ }
+}))
+
+/** What the (real) device probe would have measured — set per test. */
+const devices = vi.hoisted(() => ({
+ current: { ceiling: null, inUse: null } as PtyDevices
+}))
+vi.mock('./pty-devices', async (importOriginal) => ({
+ ...(await importOriginal<typeof import('./pty-devices')>()),
+ readPtyDevices: () => devices.current
+}))
+
+const ALICE = 1
+
+describe('pty create: pre-flight device check', () => {
+ let fake: FakePlatform
+
+ beforeEach(async () => {
+ spawned.length = 0
+ devices.current = { ceiling: null, inUse: null }
+ fake = fakePlatform()
+ initPlatform(fake)
+ const { PtyManager } = await import('./pty-manager')
+ new PtyManager().registerIpc()
+ })
+ afterEach(() => resetPlatformForTests())
+
+ const create = (): Promise<PtyCreateResult> =>
+ fake.handlers[IPC.ptyCreate](ALICE, {
+ cols: 80,
+ rows: 24,
+ persistKey: 'node-1'
+ }) as Promise<PtyCreateResult>
+
+ it('never calls node-pty when the machine is out of pty devices', async () => {
+ devices.current = { ceiling: 511, inUse: 515 }
+
+ await expect(create()).rejects.toThrow()
+
+ expect(spawned).toHaveLength(0) // ← the leak's only cure: the fd is never opened
+ })
+
+ it('still says exactly what is full, with both numbers', async () => {
+ devices.current = { ceiling: 511, inUse: 515 }
+
+ // The same sentence the post-failure diagnosis produces — one wording, one code path.
+ await expect(create()).rejects.toThrow(/out of pty devices \(515 of 511/)
+ await expect(create()).rejects.toThrow(/kern\.tty\.ptmx_max/)
+ })
+
+ it('never blames the architecture for a refusal it decided itself', async () => {
+ devices.current = { ceiling: 511, inUse: 515 }
+
+ // `spawnHelperArchMismatch` outranks the device note in `spawnFailureHint`, so a machine that
+ // ALSO has a mismatched helper would be told to rebuild node-pty — advice that cannot help,
+ // for a helper this path never exec'd.
+ await expect(create()).rejects.not.toThrow(/npm run rebuild/)
+ await expect(create()).rejects.not.toThrow(/architecture/)
+ })
+
+ it('spawns normally on a machine with devices to spare', async () => {
+ devices.current = { ceiling: 511, inUse: 62 }
+
+ const res = await create()
+
+ expect(res.sessionId).not.toBe('')
+ expect(spawned).toHaveLength(1)
+ })
+
+ it('spawns normally when the devices could not be measured at all (non-darwin)', async () => {
+ devices.current = { ceiling: null, inUse: null }
+
+ await expect(create()).resolves.toBeTruthy()
+ expect(spawned).toHaveLength(1)
+ })
+
+ it('spawns normally when only the ceiling is unknown (sysctl failed, or not primed yet)', async () => {
+ // The ceiling is read asynchronously and cached; a spawn racing the very first read sees
+ // `null` here. Fail-open: an unprimed cache must never look like a full machine.
+ devices.current = { ceiling: null, inUse: 500 }
+
+ await expect(create()).resolves.toBeTruthy()
+ expect(spawned).toHaveLength(1)
+ })
+})