Skip to content

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

Closed

Description

@joenivl

Summary

The server crashed with an unhandled TypeError in closeAttachment and did not come back up. The phone PWA was stuck on "reconnecting" until I restarted it by hand with herdr plugin action invoke devswha.herdr-web-ui.start.

Log (~/.local/state/herdr/plugins/devswha.herdr-web-ui/server.log)

herdr-web-ui listening on http://127.0.0.1:7317
Managed updates: automatic checks, install from Settings
374 |     const retired = attachment.pty.exited.finally(() => {
                                     ^
TypeError: undefined is not an object (evaluating 'attachment.pty.exited')
      at closeAttachment (server/index.ts:374:32)
      at detach (server/index.ts:505:40)
      at close (server/index.ts:1217:52)

Bun v1.4.2 (Linux x64)
Bridge exited unexpectedly (1).

Likely cause

In the attach path, the attachment is put in attachments with pty: undefined as unknown as PtySession (around line 422). attachment.pty = start() only runs after that (line 494). If new PtySession(...) throws, the placeholder stays in the map with no pty. It could also stay there if the constructor calls onExit synchronously. When the client later disconnects, close → detach → closeAttachment dereferences attachment.pty.exited and takes the whole process down.

I haven't confirmed which of those two paths happened. Around the time of the crash, a pane was opened and then closed in herdr (Ctrl+b x) while the phone was connected.

Suggested fix

  • Guard in closeAttachment: when attachment.pty is unset, delete the entry and return. Don't touch .exited or .kill().
  • Optionally, wrap start() in the attach path so that a failed spawn removes the placeholder attachment and reports pty-exit to its clients.
  • A crash in the bridge could also make the supervisor restart it, rather than leaving the server down.

Environment

  • herdr-web-ui 0.3.31 (installed via install.sh as a herdr plugin, checkout 7a45468)
  • herdr 0.9.1, Bun 1.4.2, Node 24
  • Linux Mint 22.3 (x64), served via tailscale serve, used from Chrome PWA on Android (Pixel 8a)

Activity

  1. devswha commented on Sep 29, 2026

    @devswha
    Owner

    Thanks for the detailed report. I reproduced it, and the fix is in #155.

    What happened. It's your first path. new PtySession() threw synchronously, because Bun.spawn throws when it can't start the node sidecar (node not on PATH, or fd/process limits). That first attach got an in-band command_failed, but the placeholder record stayed in attachments. The next attach to the same pane, your phone reconnecting, joined that dead record and got nothing. When it disconnected, closeAttachment dereferenced the missing pty and the process died. The supervisor then exits along with the bridge, so it stayed down. The second theory, a synchronous onExit, can't happen: onExit always runs asynchronously.

    Closing a pane with Ctrl+b x doesn't trigger this by itself. A missing pane is rejected with pane_not_found before the placeholder is created. So what failed on your machine was the sidecar spawn. The failure was never written to the server log, which is why yours shows no cause.

    The fix (#155):

    • A failed spawn removes the placeholder and reports the error in-band. The pane can then be opened again.
    • The same failure during the "read in progress" attach retry no longer throws uncaught. It ends that pane's terminal with pty-exit and leaves the server running.
    • Spawn failures are written to the server log (terminal attach for pane <id> failed to start: ...). If this happens again, server.log will say why, for example if node went missing from the plugin's PATH.

    There are contract tests for both paths. They fail on main with the exact TypeError from your log and pass with the fix.

    I didn't add a guard in closeAttachment. With the placeholder removed at its source, a record without a pty can't exist any more. Automatic bridge restart from the supervisor is a separate design question and isn't included here.

  2. added a commit that references this issue on Oct 13, 2026
    6c85471
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions