Skip to content

fix(server): drop the placeholder attachment when the pty cannot spawn - #155

Merged
devswha merged 1 commit into
mainfrom
fix/attach-spawn-failure
Sep 29, 2026
Merged

devswha merged 1 commit into
mainfrom
fix/attach-spawn-failure

Conversation

@devswha

@devswha devswha commented Sep 29, 2026

Copy link
Copy Markdown
Owner

Change

Fixes #154.

spawnAttachment puts a placeholder record (pty: undefined) in attachments and then calls start(). When new PtySession() throws synchronously (Bun.spawn throws for ENOENT, for example when node is missing from PATH, or for fd/process limits), the first client got command_failed, but the placeholder stayed in the map:

  1. The next attach to that pane (a phone reconnecting) joined the dead record through ensureAttachment and got neither output nor an error.
  2. When that client closed, close → detach → closeAttachment read attachment.pty.exited and threw a TypeError that ended the process. The supervisor exits with the bridge (Bridge exited unexpectedly (1)), so the server stayed down.

The issue's other theory, a synchronous onExit from the constructor, can't happen: onExit runs inside Promise.all(...).then.

The fix:

  • Wrap the first start() in a try. A failed spawn deletes the placeholder, logs the failure and rethrows, so the attaching clients (and any joiners of the same pending creation) get command_failed in-band.
  • The retry start() inside the setTimeout (the "read in progress" retry) threw uncaught. Now it reports command_failed plus pty-exit to the pane's clients and closes the attachment. The pty it closes is the one that already exited.
  • Spawn failures are written to the server log. Before, the only record was the in-band error, which is why the reporter's log had no cause.

There's no guard in closeAttachment: a record without a pty can no longer exist, and a guard would only hide the cause. Restarting the bridge from the supervisor after a crash is a separate design question and isn't part of this PR.

Validation

Two new tests in server/output.contract.test.ts, both made to fail by breaking PATH while the sidecar spawns:

  • a terminal attach that cannot spawn (Server crashes: TypeError in closeAttachment when attachment.pty is undefined #154): the first attach gets command_failed. A second client then attaches for real, and closing it tears the attachment down cleanly. A third attach then works.
  • ends the terminal, not the server, when a retried attach cannot spawn: a refusing herdr triggers the retry, and the retry's spawn fails. The client gets command_failed plus pty-exit, and the pane attaches again afterwards.

Both tests fail on main. The first fails with the issue's exact TypeError ... at closeAttachment. The second fails with an uncaught Executable not found in $PATH: "node" from the retry timer. Both pass with the fix.

  • bun run typecheck: pass
  • bun run test:unit: 441 pass
  • bun run test:integration: 125 pass

A synchronous Bun.spawn throw in spawnAttachment left a record without a pty in
attachments. The next attach joined it and sat blank, and its close crashed the
server in closeAttachment. A retried attach that threw the same way was uncaught.

Fixes #154
@coderabbitai

coderabbitai Bot commented Sep 29, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

Navigate logical layers of code changes, visualize relationships, and explore their blast radius.

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository: devswha/herdr-web-ui/.coderabbit.yaml

Review profile: CHILL

Plan: Advanced

Run ID: ae3340dd-1f7b-4ad3-a6ca-22f68ded6f97

📥 Commits

Reviewing files that changed from the base of the PR and between 7a45468 and fdf569e.

📒 Files selected for processing (3)
  • CHANGELOG.md
  • server/index.ts
  • server/output.contract.test.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.


📝 Summary

Summary by CodeRabbit

  • Bug Fixes
    • Fixed terminal attachment failures when a helper process cannot start. The error is now reported, and the pane can be attached again without causing a later server crash.
    • Improved handling of failures during initial attachment and retry attempts.

Walkthrough

The attach path now handles synchronous helper startup failures during initial attachment and retry. It reports failures, cleans up attachment state, and includes contract tests for later attachments.

Changes

Attach spawn failures

Layer / File(s) Summary
Spawn failure handling and regression coverage
server/index.ts, server/output.contract.test.ts, CHANGELOG.md
Initial startup failures remove the placeholder attachment, log and format the error, then rethrow it. Retry failures send command_failed and a null-code pty-exit, then close the attachment. Tests cover both failure paths and subsequent attachments. The changelog records the fix.

Priority: ➖ Normal

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Suggested reviewers: yoonwoo-ha

Merge Risk: ⚪ Minimal · up to fdf56

The attachment failure handling is mergeable after normal checks.

Security Architecture Review

Security architecture risk: 🔵 Low · up to fdf56

The failure paths now remove unusable attachments and report retry failures without adding a new entrypoint or expanding client privileges. Review is warranted because several clients can share the attachment and depend on its terminal signals.

Retained concerns
No architecture-level concerns identified.

Security review details

Security Blast Radius

  • inferred — A spawn failure affects clients sharing the requested pane; preventing an uncaught retry exception also limits the potential effect on server availability.

Trust Boundaries and Controls

  • observed — The production attach path remains behind the existing WebSocket access decision. Attach geometry is validated, and observe-mode connections are denied input and resize authority.

Resilience and Maintainability Implications

  • observed — Retry-failure signaling precedes cleanup. Cleanup removes the pane claim and client output state, while the attachment still holds the exited PTY needed by the close path.
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 9 functions across 2 files. (1 skipped: 1… Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly describes the main change: removing the placeholder attachment when PTY startup fails.
Description check ✅ Passed The description includes the required Change and Validation sections. It explains the failure, the fix, regression coverage, contract tests, and test results.
Linked Issues check ✅ Passed The PR meets the coding objective in [#154]. Initial PTY spawn failure removes the placeholder attachment, logs the formatted failure, and rethrows it. Retry spawn failure reports command_failed, se…
Out of Scope Changes check ✅ Passed The changes stay within [#154]. The production changes handle failed PTY attachment startup and retry cleanup. The changelog and test updates document and verify that behavior. The existsSync adjust…
Full details: Docstring Coverage

Explanation

Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 9 functions across 2 files. (1 skipped: 1 unsupported.)

  • Fix all pre-merge checks with AI
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR

Comment @coderabbitai help to get the list of available commands.

@devswha
devswha merged commit 6c85471 into main Sep 29, 2026
9 checks passed
@devswha
devswha deleted the fix/attach-spawn-failure branch September 29, 2026 11:49
devswha added a commit that referenced this pull request Oct 1, 2026
Release v0.3.39: moves the Unreleased notes under 0.3.39 and bumps
`package.json` and `herdr-plugin.toml`.

In this release, all fixes: a password sent right after a reconnect
(#244), typing and keys outliving their connection (#235), panning a
terminal whose grid is not the browser's own on a phone (#241), omo's
question form in the chat (#237 by @nahwan-kim), and a held pane resumed
by a late herdr answer plus a new shell taken for OmO (#243). No remote
bundle change: `REMOTE_BUNDLE_VERSION` stays 9.

Changelog housekeeping that was waiting for a release:
- Compare links at the bottom now cover 0.3.31 to 0.3.39 (they stopped
at 0.3.30), and `[Unreleased]` compares from v0.3.39.
- Five references that were a bare `#N` are full PR links with their
author: #164 is the issue PR #199 fixed, #154 the issue PR #155 closed,
and #45 and #6 are PRs. No other wording of released entries changed.

`bun scripts/release-notes.ts 0.3.39` prints the notes and exits 0.
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.

Server crashes: TypeError in closeAttachment when attachment.pty is undefined

1 participant