Skip to content

Reconnect / "re-approval, repair" loop connecting to a pre-existing (self-hosted) gateway over wss #696

Closed

Description

@moosebuilds

Small one in case it's useful.

Connecting the Companion to my own existing gateway over Tailscale (wss), rather than the wizard-managed distro, it pairs and goes green for a minute, then drops into a constant "re-approval, repair" loop — roughly 10 reconnect/pairing events every couple of minutes on 0.6.3.

I tried the change on the websocket-lifecycle-correctness branch (the connection-generation counter) on top of current main, and the loop's gone — zero reconnect events over several minutes, and it holds a stable operator connection. So that approach looks like it fixes the connect-to-an-existing-gateway case. Might be worth getting onto a release.

Thanks!

Activity

  1. clawsweeper commented on Jun 5, 2026

    @clawsweeper

    Codex review: keeping this open for maintainer follow-up; there is still a little grit to resolve. Reviewed June 5, 2026, 7:28 AM ET / 11:28 UTC.

    Summary
    Keep open. The reported self-hosted wss reconnect/re-approval loop is not solved on current master: the relevant WebSocket lifecycle files are unchanged from v0.6.3, and the branch the reporter tested remains a closed-unmerged prior PR rather than code on the default branch.

    Reproducibility: no. live self-hosted wss reproduction was run. The reporter gives a concrete v0.6.3 scenario plus a tested branch result, and current source still lacks the branch's WebSocket connection-generation guard.

    Ways to help us reproduce this

    • Add a screenshot or short recording showing the behavior.
    • Add expected vs actual behavior.

    Next step
    A repair lane can attempt the fresh focused current-master PR requested by the closed prior WebSocket lifecycle PR, but it must stay narrow and include connection lifecycle validation.

    Review details

    Best possible solution:

    Land a fresh current-master WebSocket lifecycle fix that generation-guards stale socket/listen/reconnect work and proves stable self-hosted wss operator connection without reviving broad unrelated branch churn.

    Do we have a high-confidence way to reproduce the issue?

    No live self-hosted wss reproduction was run. The reporter gives a concrete v0.6.3 scenario plus a tested branch result, and current source still lacks the branch's WebSocket connection-generation guard.

    Is this the best way to solve the issue?

    Unclear; the reported connection-generation approach is promising and branch-tested by the reporter, but the prior PR was closed unmerged as too dirty/high-risk, so the maintainable solution is a fresh focused current-master implementation with lifecycle validation.

    AGENTS.md: found and applied where relevant.

    Remaining risk / open question:

    • The exact self-hosted gateway over Tailscale/wss runtime was not reproduced locally, so gateway-side timing or hosted-gateway behavior may still be part of the failure.
    • A WebSocket lifecycle fix touches shared operator and node reconnect behavior; it needs focused stale-socket, cancellation, pairing, and upgrade validation before release.

    Codex review notes: model gpt-5.5, reasoning high; reviewed against 99efc50cbc22.

    Label changes

    Label changes:

    • add P2: This is a current release connection bug with a concrete external report, but the blast radius is limited to a self-hosted wss gateway scenario rather than a confirmed repo-wide outage.
    • add impact:auth-provider: The symptom is a repeated pairing/re-approval loop, so the affected maintainer surface is gateway authentication and pairing state.
    • add issue-rating: 🐚 platinum hermit: Current issue advisory state selects this label.
    • add clawsweeper:needs-live-repro: Current issue advisory state selects this label.

    Label justifications:

    • P2: This is a current release connection bug with a concrete external report, but the blast radius is limited to a self-hosted wss gateway scenario rather than a confirmed repo-wide outage.
    • impact:auth-provider: The symptom is a repeated pairing/re-approval loop, so the affected maintainer surface is gateway authentication and pairing state.
    Evidence reviewed

    Acceptance criteria:

    • ./build.ps1
    • dotnet test ./tests/OpenClaw.Shared.Tests/OpenClaw.Shared.Tests.csproj --no-restore
    • dotnet test ./tests/OpenClaw.Tray.Tests/OpenClaw.Tray.Tests.csproj --no-restore
    • dotnet test ./tests/OpenClaw.Connection.Tests/OpenClaw.Connection.Tests.csproj
    • Manual Windows smoke: connect Companion to an existing self-hosted wss gateway over Tailscale and verify no repeated pairing/reconnect events for at least 5 minutes.

    What I checked:

    • Current source lacks the tested WebSocket generation guard: Current master still starts ConnectAsync without a per-socket connection-generation token and its listen loop reads the mutable _webSocket field, so stale socket close/error work can still feed the shared reconnect path. (src/OpenClaw.Shared/WebSocketClientBase.cs:106, 99efc50cbc22)
    • Relevant lifecycle files are unchanged from v0.6.3 to current master: The diff from latest release v0.6.3 to HEAD is empty for WebSocketClientBase, WindowsNodeClient, GatewayConnectionManager, and the targeted lifecycle tests, so the reported 0.6.3 behavior has not been fixed in the checked source. (99efc50cbc22)
    • The referenced branch exists as closed-unmerged work: The remote branch shanselman/websocket-lifecycle-correctness points at e60efcb, and GitHub pull Fix WebSocket lifecycle responsiveness #443 is closed with merged_at null. (e60efcb12713)
    • Prior branch shows the intended fix shape: Commit e60efcb added WebSocket lifecycle responsiveness changes across GatewayConnectionManager, WebSocketClientBase, and focused tests, including connection-generation and in-flight cancellation coverage. (src/OpenClaw.Shared/WebSocketClientBase.cs:1, e60efcb12713)
    • Prior maintainer context asks for a fresh focused PR: The closing comment on Fix WebSocket lifecycle responsiveness #443 says the concern is valid but should return as a fresh focused PR against current master with targeted lifecycle validation. (e60efcb12713)
    • Repository docs put this in core connection ownership: The connection architecture says GatewayConnectionManager owns connection lifecycle and that the tray consumes it instead of creating parallel gateway clients, making this a core connection-layer bug rather than external support only. (docs/CONNECTION_ARCHITECTURE.md:17, 99efc50cbc22)

    Likely related people:

    • shanselman: Authored the closed websocket-lifecycle-correctness branch and has substantial history in WebSocketClientBase, reconnect behavior, diagnostics, and Windows node mode. (role: recent adjacent contributor; confidence: high; commits: e60efcb12713, 76f7811a1448, 7a95b190f147; files: src/OpenClaw.Shared/WebSocketClientBase.cs, src/OpenClaw.Shared/WindowsNodeClient.cs, src/OpenClaw.Connection/GatewayConnectionManager.cs)
    • Ranjesh Jaganathan: Introduced and iterated the current connection architecture, per-gateway identity, node connector, and pairing lifecycle code paths that this loop exercises. (role: connection architecture contributor; confidence: high; commits: e3c6504aaf27, 2e5dd14d136f, 398420a92843; files: src/OpenClaw.Connection/GatewayConnectionManager.cs, src/OpenClaw.Connection/NodeConnector.cs, src/OpenClaw.Shared/WindowsNodeClient.cs)
    • Vincent Koc: Recent commits hardened Windows node bootstrap reconnects, persisted node-token startup, and app-version handshakes near the reported re-approval loop surface. (role: recent pairing and token contributor; confidence: medium; commits: 64b650cb3313, ee8fd4ef4ff0, 66683a33e770; files: src/OpenClaw.Connection/GatewayConnectionManager.cs, src/OpenClaw.Shared/WindowsNodeClient.cs)
    How this review workflow works
    • ClawSweeper keeps one durable marker-backed review comment per issue or PR.
    • Re-runs edit this comment so the latest verdict, findings, and automation markers stay together instead of adding duplicate bot comments.
    • A fresh review can be triggered by eligible @clawsweeper re-review comments, exact-item GitHub events, scheduled/background review runs, or manual workflow dispatch.
    • PR/issue authors and users with repository write access can comment @clawsweeper re-review or @clawsweeper re-run on an open PR or issue to request a fresh review only.
    • Maintainers can also comment @clawsweeper review to request a fresh review only.
    • Fresh-review commands do not start repair, autofix, rebase, CI repair, or automerge.
    • Maintainer-only repair and merge flows require explicit commands such as @clawsweeper autofix, @clawsweeper automerge, @clawsweeper fix ci, or @clawsweeper address review.
    • Maintainers can comment @clawsweeper explain to ask for more context, or @clawsweeper stop to stop active automation.
  2. added
    P2
    impact:auth-provider
    issue-rating: 🐚 platinum hermit
    clawsweeper:needs-live-repro
    on Jun 5, 2026
  3. github-actions commented on Jun 10, 2026

    @github-actions
    Contributor

    🤖 This is an automated response from Repo Assist.

    I had a look at WebSocketClientBase.cs to understand the reconnect loop root cause.

    What's happening

    ConnectAsync() assigns a new socket to _webSocket, then fires ListenForMessagesAsync() as a fire-and-forget Task.Run. When the listen loop exits (e.g., ConnectionClosedPrematurely), it calls ReconnectWithBackoffAsync(). The reconnect loop then sets _webSocket = null, disposes the old socket, and calls ConnectAsync() again — which fires another ListenForMessagesAsync().

    The race is in ConnectAsync():

    _webSocket = new ClientWebSocket();   // shared field mutated here
    await _webSocket.ConnectAsync(uri, _cts.Token);
    await OnConnectedAsync();             // may trigger pairing/challenge exchange
    _ = Task.Run(() => ListenForMessagesAsync(), _cts.Token);  // fire-and-forget

    There's no generation guard — if the reconnect loop races with an in-flight OnConnectedAsync() (e.g., because a transient socket error during TCP connect started a second reconnect path), a stale listen task can end up calling ReconnectWithBackoffAsync() against the wrong socket generation, triggering another pairing challenge.

    The fix from websocket-lifecycle-correctness

    The closed PR #443 added a _connectionGeneration counter (long, Interlocked.Increment in ConnectAsync). Each call captures its generation, and after each async step checks IsCurrentConnection(ws, connectGeneration) before proceeding. ListenForMessagesAsync receives the socket ref and generation as parameters, so it won't interfere with a newer connection even if it exits late.

    A focused fresh PR against current main implementing just this guard — plus the cancellation-propagation changes from that branch — would likely fix the re-approval loop. The _reconnectLoopActive int guard prevents concurrent reconnect loops, but doesn't prevent a stale listen task from triggering ReconnectWithBackoffAsync() when _reconnectLoopActive has already been reset to 0 by the previous loop.

    Since this touches core connection lifecycle, live testing with a self-hosted wss gateway is needed before merging (hence the clawsweeper:needs-live-repro label).

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

    Add this agentic workflows to your repo

    To install this agentic workflow, run

    gh aw add githubnext/agentics/workflows/repo-assist.md@97143ac59cb3a13ef2a77581f929f06719c7402a
    
  4. ranjeshj commented on Jun 10, 2026

    @ranjeshj
    Contributor

    Update from #741:

    The reconnect/re-approval loop class should be improved by the Windows-client fixes in #741:

    • operator and node handlers are generation/stale-client guarded so replaced clients cannot drive current state;
    • delayed reconnect scheduling is centralized with generation/disposal checks;
    • stale operator device-token recovery is explicit and diagnostics-backed;
    • node token receipt is forwarded through NodeConnector so bootstrap cleanup can complete once both role tokens are durable.

    Still pending / not covered by #741:

    The specific connection/pairing flows in #741 were validated with full real WSL E2E passing (17/17).

  5. christineyan4 commented on Jun 10, 2026

    @christineyan4
    Contributor

    Hi @markbdean01, I have PR #739 that implements the generation guard suggested by Copilot above. Could you try out that branch with your Tailscale gateway setup and see if it helps?

  6. moosebuilds commented on Jun 10, 2026

    @moosebuilds
    Author
  7. kmahone commented on Jun 12, 2026

    @kmahone
    Collaborator

    Thanks for taking a look @markbdean01. Were you able to try the build with the fix against your own gateway? Did it resolve the issue that you were hitting?

  8. moosebuilds commented on Jun 18, 2026

    @moosebuilds
    Author

    Yes — built #739 and ran it live against my own self-hosted gateway (OpenClaw over wss, reached via Tailscale). It resolves it for me.

    Test suites first: the generation-guard tests pass (StaleEventGuard 5/5) and OpenClaw.Connection.Tests is 297/297 green, no regressions. (One unrelated MXC desktop-path test fails only under my SYSTEM-context build runner — environment, not the fix.)

    Then live: the node reconnects as operator reusing its persisted device token — no re-pairing — and the operator RPCs that used to get rejected (sessions.list, chat.history) now come back clean. I forced several transient drops including a full cold restart of the node (socket fully gone, reconnected in ~3s): 0 re-approval requests, 0 PolicyViolation, 0 rate-limits across all of it. Before the fix the same setup span up ~10-20 pairing/re-approval events in a couple of minutes, so the difference is night and day.

    So from my side #696 is fixed by the generation-guard. Happy to leave it running for the week and shout if anything recurs.

  9. locked as resolved and limited conversation to collaborators on Jun 24, 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

    P2clawsweeper:needs-live-reproimpact:auth-providerissue-rating: 🐚 platinum hermit

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions