Fix Linux terminal fd leak: bump node-pty to close inherited fds#1127
Conversation
Repoint node-pty from `fix/darwin-pty-fd-leak` (branched 2025-12-21, 127 commits behind upstream) to `feat/foreground-pid-dist` — upstream main + our `foregroundPid` accessor (microsoft/node-pty#918) + committed lib. This picks up upstream `52417d516` ("close inherited file descriptors in child process on Linux"), which the old base predated. Without it, every forkpty'd shell inherited the server's open fds — including prior terminals' /dev/ptmx master fds. Verified in real kolu: a split sub-terminal's shell held `fd 29 -> /dev/ptmx` (the main terminal's leaked master). After the bump a 2nd shell shows 0 leaked ptmx, and the `foregroundPid` accessor still resolves (kolu's spawn sanity-check). macOS is unaffected (POSIX_SPAWN_CLOEXEC_DEFAULT); the now-upstream macOS /dev/ptmx fix (#882) is dropped from the fork. Refs #1076
EvidenceThe fix is a resource-leak fix, so the proof is a measurement, not a pixel diff. fd-leak: before → after (reproducible)Spawn 6 PTYs sequentially (as the kolu server does), then count each shell's leaked
Each shell N inherits the master fds of all N−1 terminals opened before it — and every process that shell spawns (agents, TUIs, build tools) inherits them too. The leak is O(n²) in a long session. With the bump: zero. Cross-checked against the running app earlier in this investigation: a split sub-terminal's shell held
|
Long-lived Kolu sessions on Linux leak a
/dev/ptmxmaster fd into every shell spawned after the first, and those leaked masters ride along into each shell's children — the suspected cause of the frozen split-terminal after a raw-mode TUI exits (#1076), plus a steady fd leak that pressuresEMFILEin busy sessions. The root cause was a stale node-pty fork: our pin (fix/darwin-pty-fd-leak) branched 2025-12-21 and sat 127 commits behind upstream, predating the Linux fix for exactly this.This repoints node-pty to a refreshed fork branch and drops a fork patch that has since landed upstream.
What changed
fix/darwin-pty-fd-leak)feat/foreground-pid-dist)main(2026-05-18)52417d516)foregroundPidaccessor/dev/ptmxfix (#882)lib/for git installUpstream
52417d516closes inherited fds in theforkpty()child beforeexec(close_range(2)→/proc/self/fdfallback). macOS was never affected (POSIX_SPAWN_CLOEXEC_DEFAULT), which is why this bug is Linux-only.Verification
Measured on this branch with the rebuilt addon:
/dev/ptmxfds (it was 1 before — confirmed in a real Kolu split sub-terminal, whose shell heldfd 29 -> /dev/ptmx, the main terminal's master).foregroundPidintact: resolves to the foreground pgid (== pid), sopty-host's spawn-time sanity check still passes — agent/Dock foreground detection unaffected.just checkclean; nativepty.nodecompiles under nix (node-gyp rebuild).Fork housekeeping
The upstreaming of
foregroundPidis microsoft/node-pty#918 (rebased here, mergeable, awaiting maintainer review). Once it merges, the fork shrinks to just the committed-lib/packaging shim — and could be retired entirely if we build node-pty from source in the nix derivation.Refs #1076.
Generated by Claude Code (model
claude-opus-4-8).