Skip to content

WebSocket reconnect gives up permanently after 10 attempts #175

@danshapiro

Description

@danshapiro

Problem

When the server is completely unreachable at the TCP level (e.g. brief network blip, server restart), the WebSocket reconnect logic in src/lib/ws-client.ts has two compounding issues:

  1. Unbounded exponential backoff — delays grow as 1000 * 2^n ms, reaching 512s (~8.5 min) by the 9th attempt. Most implementations cap at 30-60s.

  2. Hard 10-attempt limit with no recovery — after 10 failed attempts (~17 min total), scheduleReconnect() logs max reconnect attempts reached and returns. The client is permanently dead until page reload.

Note: reconnectAttempts resets in ws.onopen (line 172), so this only affects scenarios where the server is totally unreachable (TCP connect fails before onopen). When the server is reachable but drops post-handshake, the counter resets and it retries at 1s indefinitely — that path is fine.

Observed behavior

Client error logs showing repeated [WsClient] reconnect failed WebSocket error with increasing gaps between attempts. The HTTP endpoint (/api/logs/client) was reachable the whole time, suggesting the outage was brief but the client had already burned through attempts or was waiting on a very long backoff delay.

Suggested fix

  • Cap max backoff delay (e.g. 30s)
  • After exhausting fast attempts, switch to a slow poll (e.g. every 60s) instead of giving up entirely
  • Optionally: retry immediately on document.visibilitychange (user returns to tab)

Relevant code

  • src/lib/ws-client.ts lines 401-425 (scheduleReconnect)
  • src/lib/ws-client.ts line 94 (maxReconnectAttempts = 10)
  • src/lib/ws-client.ts line 95 (baseReconnectDelay = 1000)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions