Repository navigation
Build node-pty from patched source on macOS so terminals stop leaking descriptors - #409
Merged
Merged
Conversation
… descriptors Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Jiang Yongze <jiangyongze@gmail.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
qingshanfeihu
force-pushed
the
fix/node-pty-macos-windows-test
branch
from
November 3, 2026 08:38
13102c4 to
0ba8586
Compare
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.
Replaces #402. It keeps #402's commit unchanged, by its author, and adds one commit that fixes the new test on Windows.
Why a replacement
#402's new
apps/desktop/test/node-pty-macos.test.tschecked the spawn helper's execute bits (statSync(...).mode & 0o111) on every system. Windows has no execute bits, so the check failed there (0 !== 73) inruntime-other-platforms (windows-latest)anddesktop (windows-x64), in run 41451572250.The fix
Check the spawn helper's execute bits only off Windows: the assertion now runs only whenprocess.platform !== 'win32'.spawn-helperis a macOS binary, and the build it checks only runs on macOS. The rest of that test is unchanged: the prebuilds for both architectures, their receipts,build/being removed and nothing being rebuilt a second time are still checked on every system.Two other jobs failed on #402's run, and the fix does not cover them:
native (ubuntu-24.04, linux-x64)andpackage-other-platforms (windows-latest):apps/desktop/test/compaction.mjs:274timed out inpage.screenshotafter 45 s. That script is not changed here, and the node-pty script does nothing on Linux or Windows.build (windows-11-arm, windows-arm64)in the release workflow:tests/file-rewind.test.tsexpected the shortRUNNER~1spelling of TEMP and got the long one, along with a memory test. Neither file is changed here.Checks run here (macOS 27, arm64, scratch
CIRCLE_HOMEandCIRCLE_WORKBENCH_DATA_DIR)node --import tsx --test test/node-pty-macos.test.ts test/node-pty-descriptors.test.ts(apps/desktop): 5 pass, 0 fail.test:runtime: 826 tests, 809 pass, 0 fail, 17 skipped.typecheck: clean. Prettier on the changed file: clean.Not run here: the Windows jobs. The fix's Windows effect is verified only by this PR's CI.
#402's description
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