Skip to content

bug: aggressive retry loop on authentication failures triggers gateway rate limiter #198

Closed

Description

@shanselman

Problem

When authentication fails (token mismatch, device signature invalid, or rate-limited), the reconnect logic retries every 1 second with no backoff. This creates a tight loop that:

  1. Floods the gateway with failed auth attempts
  2. Triggers the gateway's rate limiter (too many failed authentication attempts)
  3. Makes the rate limit worse — once rate-limited, every retry resets the cooldown
  4. Fills the log file with hundreds of identical errors per minute

Observed behavior (from logs)

[14:53:44.376] [WARN] Gateway rejected device signature with mode V3AuthToken
[14:53:44.785] [WARN] Gateway rejected device signature with mode V3EmptyToken
[14:53:45.932] [WARN] connect failed: unauthorized: gateway token mismatch
[14:53:47.035] [WARN] connect failed: unauthorized: gateway token mismatch
[14:53:48.126] [WARN] connect failed: unauthorized: gateway token mismatch
... (every ~1 second, endlessly)
[14:56:31.345] [WARN] connect failed: unauthorized: too many failed authentication attempts

The reconnect attempt counter resets to 1 on each cycle, so the exponential backoff (1s → 2s → 4s → 8s → ... → 60s) never kicks in.

Expected behavior

  • Auth failures (401/unauthorized/token mismatch) should use longer backoff than transient connection errors
  • After 3-5 consecutive auth failures, stop retrying and show a clear status: "Authentication failed — check your token in Settings"
  • The reconnect attempt counter should NOT reset on auth failures
  • Consider distinguishing between retryable errors (network timeout, server unavailable) and permanent errors (wrong token, origin rejected)

Suggested approach

In OpenClawGatewayClient.cs, when the gateway closes with PolicyViolation and an auth-related message:

  • Don't reset _reconnectAttempts
  • Use a minimum backoff of 30-60 seconds for auth failures
  • After N consecutive auth failures, stop retrying and fire a new event (e.g., AuthenticationFailed) so the UI can show a persistent warning

Activity

  1. added
    bug
    on Apr 22, 2026
  2. github-actions commented on Apr 23, 2026

    @github-actions
    Contributor

    🤖 This is an automated response from Repo Assist.

    Good diagnosis. I dug into the reconnect logic to confirm the root cause and map out a fix.

    Root cause confirmed

    The counter _reconnectAttempts lives in WebSocketClientBase and is reset to 0 at line 126 of WebSocketClientBase.cs, only when ConnectAsync reaches the await _webSocket.ConnectAsync(...) call and succeeds at the TCP/HTTP layer:

    await _webSocket.ConnectAsync(uri, _cts.Token);
    _reconnectAttempts = 0;   // ← resets here

    The backoff schedule (1s→2s→4s→8s→15s→30s→60s) therefore only applies to TCP-level failures. When the connection reaches the WebSocket handshake stage successfully but the gateway then closes it with PolicyViolation (token mismatch, rate-limit), the sequence is:

    1. ConnectAsync succeeds → _reconnectAttempts = 0
    2. ListenForMessagesAsync receives a Close frame
    3. Code falls through to ReconnectWithBackoffAsync()
    4. _reconnectAttempts is 0 again → next delay is 1 second → cycle repeats forever

    The fix in OpenClawGatewayClient

    OpenClawGatewayClient already has a ShouldAutoReconnect() override and a _pairingRequiredAwaitingApproval flag that pauses reconnection. The same pattern can be extended for auth failures:

    Option A (minimal): In HandleRequestError, when the close reason is an auth error ("token mismatch", "too many failed authentication attempts") set a _authFailedAwaitingUserIntervention flag and return false from ShouldAutoReconnect(). Fire a new AuthenticationFailed event so the UI can show a persistent banner.

    Option B (backoff-preserving, more surgical): Move the _reconnectAttempts = 0 reset from the TCP-connect success path into a new virtual method OnHandshakeSucceeded() called only after the gateway's hello-ok message is received. This way, auth failures at the challenge/response stage don't reset the counter.

    Option A is simpler and directly addresses the gateway-rate-limiter problem. Option B is more correct but touches WebSocketClientBase.

    Tests added this run

    I've opened a draft PR (pending merge) adding 13 tests that cover the HandleRequestError paths, including verifying _pairingRequiredAwaitingApproval is set and ConnectionStatus.Error is raised. These will guard against regressions once the fix lands.

    Generated by 🌈 Repo Assist, see workflow run. Learn more.

    To install this agentic workflow, run

    gh aw add githubnext/agentics/workflows/repo-assist.md@97143ac59cb3a13ef2a77581f929f06719c7402a
    

    Generated by 🌈 Repo Assist, see workflow run. Learn more.

    To install this agentic workflow, run

    gh aw add githubnext/agentics/workflows/repo-assist.md@97143ac59cb3a13ef2a77581f929f06719c7402a
    
  3. added a commit that references this issue on Apr 23, 2026
    ae88d03
  4. added a commit that references this issue on Apr 26, 2026
    12eb2a4
  5. locked as resolved and limited conversation to collaborators on May 29, 2026
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

    bug

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions