Skip to content

Terminals no longer leak a PTY each: node-pty 1.2.0-beta.15 - #65

Merged
BunsDev merged 1 commit into
mainfrom
fix-pty-leak
Oct 2, 2026
Merged

BunsDev merged 1 commit into
mainfrom
fix-pty-leak

Conversation

@BunsDev

@BunsDev BunsDev commented Oct 2, 2026

Copy link
Copy Markdown
Contributor

Every terminal session leaked one /dev/ptmx until Modex quit. macOS allows 511 machine-wide (kern.tty.ptmx_max), so a long-running Modex could stop terminals opening anywhere on the Mac. Found while investigating the cleanup flakes (#64).

Cause: node-pty 1.1.0 on macOS

pty_posix_spawn (src/unix/pty.cc) opens placeholder PTYs until one lands at fd ≥ 2, then opens the real master separately. Cleanup is for (; count > 0; count--) close(low_fds[count]);, which never closes low_fds[0]. In any process whose fds 0–2 are open, count is 0, so the first placeholder leaks on every spawn. An lsof trace confirmed it: each terminal's own master and slave close on stop, and one extra unused /dev/ptmx per spawn stays open.

(status.md from #64 blamed node-pty's write stream and destroy(). That was wrong; CustomWriteStream shares the socket's fd. Corrected here.)

Fix: pin node-pty to 1.2.0-beta.15

Upstream's beta closes low_fds[0..count], the parent's slave fd, and the exit watcher's kqueue (also leaked once per spawn in 1.1.0). Compared with 1.1.0:

  • JS API: unchanged except an optional pixelSize on resize(); process skips spawn_helper.
  • Native: the leak fixes above, FD_CLOEXEC, and specific spawn error messages instead of posix_spawnp failed.; spawn-helper.cc identical.
  • Same N-API prebuilds for darwin-arm64/x64, so nothing compiles at install.

It is a pre-release. status.md says to move to 1.2.0 stable when it ships. The lockfile diff is only the node-pty entry; the integrity matches the registry.

Tests

  • New: closing terminals releases every PTY they opened opens and closes five real terminals and counts this process's /dev/ptmx handles via lsof (macOS only). On 1.1.0 it fails (actual: 5, expected: 0); on the beta it passes.
  • npm run build && npm run typecheck && npm test: core 15/15, desktop 147/147.
  • Terminal unit files 3× in a row: 17/17 each.
  • playwright test e2e/terminal.spec.ts e2e/terminal-lifecycle.spec.ts e2e/release.spec.ts: 18 passed, including the bundled terminal engine starts a real PTY.

Not run: a packaged (npm run dist) build. prepare-terminal.mjs sets the beta's spawn-helper executable as before, and asarUnpack still covers node_modules/node-pty/**.

🤖 Generated with Claude Code

node-pty 1.1.0's macOS pty_posix_spawn opens a placeholder /dev/ptmx
(low_fds) to keep the master off fds 0-2, then closes low_fds[count..1]
and never low_fds[0]. In any process with 0-2 open, count is 0, so every
spawn leaked one PTY until Modex quit; macOS allows 511 machine-wide.
1.2.0-beta.15 closes low_fds[0..count], the parent's slave and the exit
watcher's kqueue. Its JS API is unchanged apart from an optional pixel
size on resize(), and it ships the same darwin prebuilds.

A real-PTY test opens and closes five terminals and counts this
process's /dev/ptmx handles: 5 leaked on 1.1.0, 0 on the beta.
status.md had blamed node-pty's write stream; corrected and moved to
"On main, not yet released".

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Copilot AI balanced review requested due to automatic review settings October 2, 2026 01:02

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟢 Approval recommended

The focused dependency update is consistent across manifests and has targeted regression coverage.

Review effort: Balanced
Findings: None

What changed in this PR

Updates node-pty to fix macOS PTY descriptor leaks and adds regression coverage.

Changes:

  • Pins node-pty to 1.2.0-beta.15.
  • Adds a macOS PTY cleanup regression test.
  • Updates project status documentation.
File Description
package-lock.json Locks the updated dependency and integrity hash.
apps/​desktop/​package.json Updates the node-pty dependency.
apps/​desktop/​test/​terminal-cleanup.test.ts Verifies closed terminals release PTYs.
docs/​status.md Documents the fix and stable-upgrade follow-up.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@BunsDev
BunsDev merged commit d62bfcf into main Oct 2, 2026
3 checks passed
@BunsDev
BunsDev deleted the fix-pty-leak branch October 2, 2026 01:06
BunsDev added a commit that referenced this pull request Oct 2, 2026
Point the PTY-leak entry at #65 instead of its branch, note that no
packaged build has run the node-pty beta yet (v0.0.5's packaged e2e will
be the first), and add #64's consistent cleanup-failure message.

Co-authored-by: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants