Skip to content

fix(terminal): prevent output stalls when browsers enter the background #781

Closed

Description

@gosunuts

Problem

When a browser enters the background, terminal output may stop with:

Terminal output stopped because this device could not keep up.

The WebSocket closes with code 4008 (output_stalled) when terminal output cannot be delivered or consumed within the configured backpressure limits.

This can affect both desktop and mobile browsers.

Root cause hypothesis

Background browsers may throttle or suspend JavaScript execution, delaying terminal output processing and acknowledgements.

The current lifecycle handles background transitions inconsistently:

Both interactive output and read-only observations can therefore encounter backpressure while the browser is inactive.

The exact failure path requires reproduction and confirmation.

Proposed solution

  1. Suspend hidden output. When document.visibilityState === "hidden", stop unnecessary terminal output subscriptions, including interactive attachments and read-only watches.
  2. Restore on foreground. Reconnect to the existing pane and restore its terminal view using existing mechanisms.
  3. Preserve running sessions. Backgrounding must not interrupt herdr panes, commands, or AI agents.
  4. Reuse existing lifecycle handling. Avoid duplicate state, additional buffering, or unnecessary abstractions. Preserve ownership, queued-input, and mirrored-pane safeguards.
  5. Keep bounded backpressure. Do not increase buffer limits, silently discard output, or introduce automatic reconnect loops.

A desktop window that remains visible but loses focus should retain its existing behavior.

Acceptance criteria

  • Hidden desktop and mobile browsers stop receiving unnecessary terminal output.
  • Backgrounding during sustained output does not trigger 4008 due to hidden-client streaming.
  • Returning to the browser restores the same running pane.
  • Repeated background/foreground transitions do not leak observers or duplicate subscriptions.
  • Other active clients remain unaffected.
  • Existing foreground backpressure protections remain intact.
  • Regression tests cover desktop and mobile visibility transitions, sustained output, and recovery.

Related code

  • src/components/PaneTerminal.tsx
  • src/lib/ws.ts
  • server/watch.ts
  • server/index.ts
  • server/machine-relay.ts

Related PRs: #700, #748.

Activity

  1. changed the title [-]fix(terminal): prevent output stalls when mobile browsers enter the background[/-] [+]fix(terminal): prevent output stalls when browsers enter the background[/+] on Oct 11, 2026
  2. devswha commented on Oct 17, 2026

    @devswha
    Owner

    Thanks for the report. The stall was fixed by #968, which merged after this issue was opened: a 4008 that reaches a page while it is hidden, or within 5 s of it being shown again, now attaches the pane again by itself once the page is back in use.

    We re-tested on current main (171ad70) with a really hidden tab and a really frozen one. The tests used raw CDP, because Playwright keeps every page visible: another tab opened in front, and Page.setWebLifecycleState frozen. They used headless Chrome on Linux, at desktop size and with phone emulation, against an isolated herdr session.

    Case Result
    Desktop, hidden 60 s, heavy output no close; output live on return
    Desktop, hidden 7 min, light output (intensive timer throttling) no close; output live on return
    Desktop, frozen 30 s and 5 min one close while frozen, one automatic reconnect on return, output live, no overload notice
    Phone emulation, hidden 60 s (detach + watch) and frozen 30 s same as desktop

    Not covered: a real Android device, and a tab Chrome discards entirely. Please reopen with your browser, OS and steps if you still see the overload notice after coming back to a tab.

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