Repository navigation
Build node-pty from patched source on macOS so terminals stop leaking descriptors - #402
Closed
qingshanfeihu wants to merge 1 commit into
Closed
qingshanfeihu wants to merge 1 commit into
qingshanfeihu wants to merge 1 commit into
Conversation
… descriptors Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Collaborator
Author
|
Replaced by #409. It keeps this PR's commit unchanged and adds one commit: the new test's execute-bit check on |
This was referenced Nov 3, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
While investigating the stalled runtime (#401), the stuck worker held a dangling
/dev/ptmxand a(revoked)tty with no child process. node-pty 1.1.0's macOS spawn leaks three descriptors every time it starts a terminal:pty_posix_spawnopens/dev/ptmxdescriptors to keep 0–2 taken, then cleans up withfor (; count > 0; count--) close(low_fds[count]). In a normal processcountis 0 there, solow_fds[0]is never closed.slaveend;posix_spawn_file_actions_addclosecloses it only in the child. Early-return paths also leak the master.SetupExitCallbackcreates akqueue()per terminal for its exit watcher and never closes it.Upstream fixed these after 1.1.0 (microsoft/node-pty#882, #931), but only in
1.2.0-betaprereleases so far. Moving to a beta would also change Windows, whichsrc/workspace/pty-exit-cleanup.mjsholds to 1.1.0's lifecycle.What
apps/desktop/scripts/node-pty-macos.mjs, run by the existing desktop postinstall (developer-toolchain.mjs --native-permissions), on macOS only:src/unix/pty.cchas the published hash.close(kq)after the exit watcher; the spawn closes the slave, the master on failure, and every low descriptor. The cleanup loop is also safe whenposix_openptfails, where upstream'si <= countcould readlow_fds[3].pty.nodeandspawn-helperfor arm64 and x64 with npm's node-gyp, checks each withlipo -archs, and puts them inprebuilds/darwin-*with a receipt. A build that matches its receipt is kept.build/, so nothing shadows the prebuilds.docs/development/native-development-loop.md. A macOS desktop install now needs the Xcode Command Line Tools, and node-gyp may download Node's headers once.Tests
apps/desktop/test/node-pty-descriptors.test.ts(macOS): six terminals started through Circle'sspawnPty, withlsofcounting this process's/dev/ptmx, tty/revoked and kqueue descriptors.{ ptmx: 7, tty: 7, kqueue: 12 }vs{ 1, 1, 6 }, one of each per terminal.apps/desktop/test/node-pty-macos.test.ts(all systems): the fix gives the reviewed source and a second run changes nothing; any other source is refused. With a stubbed node-gyp and lipo, both archs are built into prebuilds with receipts andbuild/removed, nothing is rebuilt the second time, a wrong-arch build is refused before replacing anything, and another version is refused while Linux and Windows are left alone.npm --prefix apps/desktop ciran the fix by itself: reviewed source hash,pty.node/spawn-helperarm64 and x86_64, nobuild/, Windows prebuilds intact.lsof.pty.nodeis byte-identical to the local fixed build.Ran on macOS 27.0.1 (arm64), with scratch homes and data folders and the background-only test browser:
npm run check: 1563 pass, 0 fail, 10 skipped.npm test: 163 pass.test:runtime: 826 tests, 809 pass, 0 fail, 17 skipped; this includes all the real terminal tests on the patched spawn.format:check: clean.npm run packageandtest:launchon it: 4/4.Not verified: the x64 binaries were built and arch-checked here but not run; the Intel release runner builds and tests its own. Not run on the Intel runner or in CI yet.
🤖 Generated with Claude Code