Skip to content

fix(terminal): prevent infinite reconnect loop on Windows hosts#336

Merged
tiann merged 1 commit intotiann:mainfrom
lifu963:fix/windows-terminal-flash
Mar 21, 2026
Merged

fix(terminal): prevent infinite reconnect loop on Windows hosts#336
tiann merged 1 commit intotiann:mainfrom
lifu963:fix/windows-terminal-flash

Conversation

@lifu963
Copy link
Contributor

@lifu963 lifu963 commented Mar 21, 2026

Problem

On Windows hosts, the remote terminal UI flashes "Terminal ID is already in use." in an infinite loop, making the terminal page unusable.

Root cause

The Bun PTY API (Bun.Terminal) is POSIX-only. When the CLI runs on Windows, it immediately emits terminal:error. Two bugs then create an infinite loop:

  1. Hub (terminalHandlers.ts): The terminal:error handler forwarded the error to the frontend without removing the terminalId from the registry. The next terminal:create attempt was rejected as a duplicate.

  2. Web (terminal.tsx): The component reset connectOnceRef on every error, and the ResizeObserver continuously re-triggered connect() — each attempt hitting the stale registry entry.

Fix

Layer File Change
Hub terminalHandlers.ts Clean up terminalRegistry on terminal:error (same pattern as terminal:exit)
Web terminal.tsx Stop resetting connectOnceRef on error/exit to break the reconnect loop
Web terminal.tsx Detect Windows hosts via session metadata; render a static "unsupported" message instead of attempting connection
Web SessionChat.tsx, HappyComposer.tsx, ComposerButtons.tsx Disable the Terminal button for Windows hosts with tooltip
Web terminalSupport.ts Utility to check session.metadata.os
Web en.ts, zh-CN.ts Add terminal.unsupportedWindows i18n key
CLI TerminalManager.ts Improve error message wording
Docs faq.md Note Windows terminal limitation

Test plan

  • Windows host: terminal page shows "Remote terminal is unavailable on Windows hosts." (no flash)
  • Windows host: Terminal button in composer is disabled with tooltip
  • Linux/macOS host: terminal still connects and works normally (not tested — no POSIX host available)
  • Unit tests pass for terminalSupport helpers
  • Unit test verifies terminalRegistry.remove() is called on terminal:error

Made with Cursor

On Windows, the Bun PTY API is POSIX-only, so the CLI emits a
terminal:error immediately. Two bugs caused the web UI to flash
"Terminal ID is already in use." in an infinite loop:

1. Hub: the terminal:error handler forwarded the error without
   removing the terminalId from the registry, causing subsequent
   create attempts to be rejected as duplicates.

2. Web: terminal.tsx reset connectOnceRef on every error/exit,
   and the ResizeObserver continuously re-triggered connect().

Fixes:
- Hub: clean up terminalRegistry on terminal:error (matching the
  pattern used by terminal:exit).
- Web: stop resetting connectOnceRef on error/exit to break the
  reconnect loop.
- Web: detect Windows hosts via session metadata and show a static
  "unsupported" message instead of attempting connection.
- Web: disable the Terminal button in the composer for Windows hosts
  with an explanatory tooltip.
- CLI: improve the Windows error message wording.
- Docs: update FAQ to note Windows terminal limitation.

Made-with: Cursor
Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Findings

  • None.

Summary
Review mode: initial. No issues found in changed lines. Residual risk: Linux/macOS terminal path not validated in this review.

Testing

  • Not run (automation)

HAPI Bot

@tiann tiann merged commit 895654d into tiann:main Mar 21, 2026
2 checks passed
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