Skip to content

html: auto-reconnect on socket close with exponential backoff - #1536

Open
hndrewaall wants to merge 1 commit into
tsl0922:mainfrom
hndrewaall:main
Open

html: auto-reconnect on socket close with exponential backoff#1536
hndrewaall wants to merge 1 commit into
tsl0922:mainfrom
hndrewaall:main

Conversation

@hndrewaall

Copy link
Copy Markdown

Closes #808, #1134, #1397; helps #911, #1016, #1156.

Currently the web client only reconnects on non-1000 close codes
and disables reconnect after a single socket error, so any
laptop sleep / VPN flap / brief network drop sticks the terminal
at "Press Enter to Reconnect" until the user notices.

This change:

  • schedules a backoff reconnect (1s -> 30s) on every close
    while reconnect is on, not just non-1000 codes
  • keeps reconnect enabled across error events (the close
    that always follows drives the retry)
  • adds a visibilitychange listener that cancels the backoff
    and reconnects immediately when the tab becomes visible
  • resets the backoff on successful (re)open

closeOnDisconnect=true and disableReconnect=true paths are
preserved (the original window.close() / Press-Enter branches
still fire when those options are set).

…ff (#1)

* feat: autoreconnect with visibilitychange handler + exponential backoff

The stock client only reconnects immediately on non-1000 close codes and
disables reconnect entirely on the websocket `error` event. That makes
the terminal die on a single network blip (laptop sleep/wake, VPN flap,
brief WiFi drop) -- the user comes back to a dead overlay and has to
manually reload the page.

This patch keeps the existing `reconnect` and `closeOnDisconnect`
client-settings semantics but changes the reconnect strategy:

- Always schedule a reconnect on `close` when `reconnect` is enabled,
  including close code 1000.
- Don't disable reconnect on `error` -- the browser fires `close` after
  `error` anyway, so onSocketClose handles it.
- Exponential backoff: 1s -> 2s -> 4s -> ... capped at 30s.
- `visibilitychange` listener: when the tab becomes visible AND the
  socket is CLOSED/CLOSING, cancel any pending backoff timer and
  reconnect immediately. This covers the laptop-wake / VPN-flap case
  where the user returns to a dead session and would otherwise wait out
  the backoff.
- A successful (re)open resets the backoff delay.
- `closeOnDisconnect` and the !reconnect ("Press Enter to Reconnect")
  paths are unchanged.

Caveats:
- In-place reconnect resets terminal scrollback (same as the existing
  reconnect-overlay path).
- Typed-but-unsent input is lost on disconnect, matching existing
  behaviour (sendData early-returns when the socket isn't OPEN).

Co-Authored-By: Claude <noreply@anthropic.com>

* style: register visibilitychange listener via addEventListener helper; clear reconnect timer on dispose

---------

Co-authored-by: Claude <noreply@anthropic.com>
hndrewaall added a commit to hndrewaall/claude-watch that referenced this pull request May 13, 2026
The ttyd web console previously consumed the upstream
tsl0922/ttyd:1.7.7 image's embedded HTML directly. That HTML's
WebSocket-client logic only reconnects on non-1000 close codes
and bails entirely on a single socket error, so laptop sleep /
VPN flap / brief network drops stick the in-browser terminal at
"Press Enter to Reconnect" until the user notices.

Switch the frontend build to clone our ttyd fork
(https://github.com/hndrewaall/ttyd) and run yarn build + gulp
inline against it, producing a single self-contained HTML file
that ships an exponential-backoff auto-reconnect plus a
visibilitychange handler (cancels the backoff + reconnects
immediately when the tab becomes visible). The autodark CSS +
theme-swap injection still runs on top of the fork-built HTML
via inject-autodark.py.

The upstream C binary is unchanged; we still copy it out of
tsl0922/ttyd:1.7.7. Once the upstream PR
(tsl0922/ttyd#1536) merges, the
TTYD_FORK_REF pin can move back to tsl0922/ttyd:main (or a
future tag).

Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
hndrewaall added a commit to hndrewaall/claude-watch that referenced this pull request Aug 11, 2026
The ttyd web console previously consumed the upstream
tsl0922/ttyd:1.7.7 image's embedded HTML directly. That HTML's
WebSocket-client logic only reconnects on non-1000 close codes
and bails entirely on a single socket error, so laptop sleep /
VPN flap / brief network drops stick the in-browser terminal at
"Press Enter to Reconnect" until the user notices.

Switch the frontend build to clone our ttyd fork
(https://github.com/hndrewaall/ttyd) and run yarn build + gulp
inline against it, producing a single self-contained HTML file
that ships an exponential-backoff auto-reconnect plus a
visibilitychange handler (cancels the backoff + reconnects
immediately when the tab becomes visible). The autodark CSS +
theme-swap injection still runs on top of the fork-built HTML
via inject-autodark.py.

The upstream C binary is unchanged; we still copy it out of
tsl0922/ttyd:1.7.7. Once the upstream PR
(tsl0922/ttyd#1536) merges, the
TTYD_FORK_REF pin can move back to tsl0922/ttyd:main (or a
future tag).

Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
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.

Automatically reconnect after one minute without any input

1 participant