Skip to content

fix: PTY proxy for local mode stdin corruption#553

Open
mfazekas wants to merge 2 commits intoslopus:mainfrom
mfazekas:fix/pty-proxy-stdin-corruption
Open

fix: PTY proxy for local mode stdin corruption#553
mfazekas wants to merge 2 commits intoslopus:mainfrom
mfazekas:fix/pty-proxy-stdin-corruption

Conversation

@mfazekas
Copy link
Copy Markdown

@mfazekas mfazekas commented Feb 7, 2026

After local→remote→local mode switch, Node.js children spawned with stdio:'inherit' stop receiving stdin.

Fix: spawn local-mode children via node-pty in their own PTY (child never inherits parent's fd 0), and give Ink a separate /dev/tty fd so it never touches process.stdin.

Investigation details

Native binaries (e.g. cat) using raw read() are unaffected — only Node.js children break.

The remote mode code and Ink both manipulate process.stdin (flowing mode, setEncoding, setRawMode) between child spawns. After cleanup (pause(), removeAllListeners()), something in the libuv TTY handle state remains dirty, preventing subsequent Node.js children from reading stdin.

E2e tests reproducing the issue: test/pty-proxy-e2e (test file)

Code that touches process.stdin between spawns:

Fixes #527
Fixes #423

…ode switch

After local→remote→local switch, Ink's setEncoding('utf8') permanently corrupts
process.stdin's libuv TTYWrap, breaking stdin for subsequent Node.js children.
Fix: spawn local-mode children via node-pty in their own PTY, and give Ink a
separate /dev/tty fd so it never touches process.stdin.

Generated with [Claude Code](https://claude.ai/code)
via [Happy](https://happy.engineering)

Co-Authored-By: Claude <noreply@anthropic.com>
Co-Authored-By: Happy <yesreply@happy.engineering>
@mfazekas mfazekas force-pushed the fix/pty-proxy-stdin-corruption branch from 4108d55 to 91e3f8b Compare February 7, 2026 18:26
mfazekas pushed a commit to mfazekas/happy that referenced this pull request Feb 7, 2026
Detailed code review of PR slopus#553 which fixes stdin corruption after
local→remote→local mode switches by using node-pty isolation and
a separate /dev/tty fd for Ink.

https://claude.ai/code/session_01GAHHRpAb2Rkxx1USTzZQpw
Generated with [Claude Code](https://claude.ai/code)
via [Happy](https://happy.engineering)

Co-Authored-By: Claude <noreply@anthropic.com>
Co-Authored-By: Happy <yesreply@happy.engineering>
@mfazekas mfazekas marked this pull request as ready for review February 8, 2026 14:32
@speakman
Copy link
Copy Markdown

Hey @mfazekas! Found the root cause of the PTY exhaustion on macOS.

TL;DR: node-pty v1.1.0 has an off-by-one bug in pty_posix_spawn() that leaks 1 PTY master fd per pty.spawn() call. The upstream fix is in microsoft/node-pty#882 (merged 2026-01-28), available in 1.2.0-beta.9+.

I opened a PR against your branch with the fix: mfazekas#1

Changes:

  1. Upgrade node-pty from 1.1.01.2.0-beta.11 (includes the leak fix)
  2. Fix abort handler to kill the process group (-pid, SIGTERM) instead of just the launcher PID (SIGHUP), so the Claude binary grandchild is also terminated

The bug is in the low_fds cleanup loop in src/unix/pty.cc — it uses for (; count > 0; count--) which skips low_fds[0] when count == 0 (the normal case). The fix changes it to for (size_t i = 0; i <= count; i++).

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.

Terminal input stuck after exit (raw mode not restored) Moving from remote mode to local mode removes the history and harm the writing in local mode

2 participants