Skip to content

fix(gateway-client): bound websocket opening handshake waits - #108348

Merged
steipete merged 2 commits into
openclaw:mainfrom
hugenshen:fix/gateway-client-ws-handshake-timeout
Jul 16, 2026
Merged

steipete merged 2 commits into
openclaw:mainfrom
hugenshen:fix/gateway-client-ws-handshake-timeout

Conversation

@hugenshen

Copy link
Copy Markdown
Contributor

What Problem This Solves

GatewayClient.createSocket configured maxPayload (and optional TLS) but never set ws handshakeTimeout. The connect-challenge watchdog in protocol-client only arms after the socket open event, so a peer that accepts TCP without completing the WebSocket upgrade could leave CLI/TUI/node Gateway connects hanging until the OS TCP stack gave up.

Why This Change Was Made

  • Resolve the opening-handshake deadline through the existing resolvePreauthHandshakeTimeoutMs path (gateway.handshakeTimeoutMs / preauthHandshakeTimeoutMs, default 15s).
  • Pass that value as handshakeTimeout on the production WebSocket constructor options.
  • Add focused regression coverage: deterministic production wiring plus a real loopback TCP peer that never upgrades (short configured timeout for suite latency).

No new config/env surface.

User Impact

Before: A Gateway endpoint that accepted TCP without finishing the WebSocket handshake could pin GatewayClient.start() forever; challenge timeout never started.

After: Opening-handshake waits fail closed after the configured preauth handshake budget (Opening handshake has timed out), so reconnect/error handling can proceed.

Evidence

  • Changed: packages/gateway-client/src/client.ts
  • Production caller: GatewayClient.createSocket → GatewayProtocolClient start/reconnect (CLI/TUI/src/gateway/call.ts bootstrap)
  • Sibling: channel clients already use handshakeTimeout: 30_000 (Discord/Slack/Signal); this package reuses the existing preauth handshake budget instead of adding a parallel knob
  • Regression: packages/gateway-client/src/client.handshake.test.ts
  • Adjacent suite still green: packages/gateway-client/src/client.watchdog.test.ts

Caller-path Vitest

node scripts/run-vitest.mjs packages/gateway-client/src/client.handshake.test.ts --reporter=verbose
 ✓ |gateway-client| packages/gateway-client/src/client.handshake.test.ts > GatewayClient websocket opening handshakeTimeout > wires handshakeTimeout from resolvePreauthHandshakeTimeoutMs into WebSocket options 825ms
[gateway-client handshake live proof] timed_out=true elapsed_ms=270 handshakeTimeout_ms=250 error=Opening handshake has timed out
 ✓ |gateway-client| packages/gateway-client/src/client.handshake.test.ts > GatewayClient websocket opening handshakeTimeout > fails when a peer accepts TCP but never completes the websocket upgrade 280ms
 Test Files  1 passed (1)
      Tests  2 passed (2)
[test] passed 1 Vitest shard in 6.27s

Adjacent regression

node scripts/run-vitest.mjs packages/gateway-client/src/client.watchdog.test.ts --reporter=verbose
 Test Files  1 passed (1)
      Tests  19 passed (19)
[test] passed 1 Vitest shard in 3.02s

Real behavior proof

Commands run:

node scripts/run-vitest.mjs packages/gateway-client/src/client.handshake.test.ts --reporter=verbose
node scripts/run-vitest.mjs packages/gateway-client/src/client.watchdog.test.ts --reporter=verbose

Before fix:
Production createSocket WebSocket options had no handshakeTimeout. Against a TCP peer that accepts but never upgrades, GatewayClient.start() never reached open, so the challenge watchdog never armed and the connect attempt could hang indefinitely.

After fix:
createSocket sets handshakeTimeout from resolvePreauthHandshakeTimeoutMs. Against a real loopback TCP peer that accepts but never upgrades, with preauthHandshakeTimeoutMs: 250:

[gateway-client handshake live proof] timed_out=true elapsed_ms=270 handshakeTimeout_ms=250 error=Opening handshake has timed out

Default unresolved budget remains DEFAULT_PREAUTH_HANDSHAKE_TIMEOUT_MS (15_000), matching existing gateway preauth config.

Evidence after fix:

 ✓ wires handshakeTimeout from resolvePreauthHandshakeTimeoutMs into WebSocket options
[gateway-client handshake live proof] timed_out=true elapsed_ms=270 handshakeTimeout_ms=250 error=Opening handshake has timed out
 ✓ fails when a peer accepts TCP but never completes the websocket upgrade
 Test Files  1 passed (1)
      Tests  2 passed (2)
[test] passed 1 Vitest shard in 6.27s

What was not tested:
Live remote Gateway hosts / Tailscale Serve paths; packaging beyond the production ws client options used by GatewayClient.createSocket.

AI Assistance

AI-assisted (Cursor). Author reviewed the production ownership, timeout contract, tests, and proof output.

@hugenshen
hugenshen force-pushed the fix/gateway-client-ws-handshake-timeout branch from e98f8ab to 2ce8811 Compare July 15, 2026 15:04
@clawsweeper clawsweeper Bot added proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. P2 Normal backlog priority with limited blast radius. merge-risk: 🚨 availability 🚨 May cause crashes, hangs, restart loops, stalls, or process outages. labels Jul 16, 2026
@clawsweeper

clawsweeper Bot commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed July 15, 2026, 11:35 PM ET / July 16, 2026, 03:35 UTC.

Summary
The PR applies the existing configurable preauth timeout to the gateway client's WebSocket opening handshake and adds production-wiring plus live loopback regression coverage.

PR surface: Source +11, Tests +105. Total +116 across 2 files.

Reproducibility: yes. The production GatewayClient can be pointed at a real loopback TCP peer that accepts the socket but never returns a WebSocket upgrade; current main remains pre-open, while the patched client reports the upstream opening-handshake timeout near the configured deadline.

Review metrics: 2 noteworthy metrics.

  • Handshake deadline coverage: 1 connection phase newly bounded. The change extends the existing preauth deadline to the previously unbounded WebSocket opening phase.
  • Configuration surface: 0 added, 1 existing timeout reused. No new knob is introduced, but the existing timeout now governs an additional fail-closed transport phase.

Stored data model
Persistent data-model change detected: migration/backfill/repair: packages/gateway-client/src/client.handshake.test.ts, serialized state: packages/gateway-client/src/client.handshake.test.ts. Confirm migration or upgrade compatibility proof before merge.

Merge readiness
Overall: 🐚 platinum hermit
Proof: 🦞 diamond lobster
Patch quality: 🐚 platinum hermit
Result: ready for maintainer review.

Overall follows the weaker of proof and patch quality, so missing proof can cap an otherwise strong patch.

Rank-up moves:

  • [P2] Add delayed-success proof showing that a raised configured handshake budget permits a slow but valid WebSocket upgrade.

Risk before merge

  • [P1] Merging changes the opening HTTP/TLS upgrade from effectively unbounded to fail-closed under the existing 15-second or configured preauth budget; unusually slow proxies, TLS negotiation, or remote ingress could therefore require operators to raise gateway.handshakeTimeoutMs, and the submitted proof does not cover a live remote or Tailscale path.

Maintainer options:

  1. Add slow-success upgrade proof (recommended)
    Demonstrate that a valid delayed WebSocket upgrade succeeds when gateway.handshakeTimeoutMs is raised, then land the shared timeout boundary.
  2. Accept the configured fail-closed boundary
    Merge with the loopback proof and explicitly own the possibility that very slow ingress must increase the existing handshake timeout.
  3. Pause the timeout change
    Leave opening waits unbounded if maintainers do not want the existing preauth setting to govern HTTP/TLS upgrade latency.

Next step before merge

  • [P2] A maintainer should decide whether the existing preauth setting intentionally governs slow HTTP/TLS upgrades and request delayed-success or remote proof if needed before merge.

Maintainer decision needed

  • Question: Should the existing gateway preauth budget also become the fail-closed deadline for the WebSocket HTTP/TLS opening upgrade?
  • Rationale: The implementation is narrow and contract-aligned, but it intentionally converts a previously unbounded transport phase into an upgrade-visible timeout for slow remote ingress.
  • Likely owner: steipete — The package extraction and shared timeout architecture both trace to this owner, making them the strongest routing candidate for the boundary decision.
  • Options:
    • Prove and land shared deadline (recommended): Keep the current design after adding delayed-success or live remote proof that a raised configured budget permits a slow but valid upgrade.
    • Accept loopback proof: Land with the submitted production loopback evidence and accept that unusually slow remote ingress may need the existing timeout setting raised.
    • Keep opening wait unbounded: Decline the fail-closed boundary and retain the current possibility of indefinitely stuck gateway clients.

Security
Cleared: The two-file diff adds no dependencies, workflow permissions, secret handling, downloaded code, or widened trust boundary, and bounding unauthenticated opening waits does not introduce a concrete security regression.

Review details

Best possible solution:

Use one canonical preauth budget for both the WebSocket opening upgrade and post-open challenge phase, with coverage for timeout failure, a delayed successful upgrade under a raised configured budget, and observable reconnect/error propagation.

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

Yes. The production GatewayClient can be pointed at a real loopback TCP peer that accepts the socket but never returns a WebSocket upgrade; current main remains pre-open, while the patched client reports the upstream opening-handshake timeout near the configured deadline.

Is this the best way to solve the issue?

Yes, with one upgrade-safety proof gap. Reusing the existing preauth budget is narrower and more maintainable than adding another timeout surface, but a delayed-success test or live remote proof should confirm that raising the existing setting preserves slow valid connections.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning high; reviewed against 957cc81175a3.

Label changes

Label changes:

  • add P2: This is a focused reliability fix for a hanging gateway-client connection path with limited blast radius and no evidence of an active widespread outage.
  • add merge-risk: 🚨 availability: The PR can terminate slow but eventually valid HTTP/TLS WebSocket upgrades that current clients wait on without this explicit deadline.
  • add proof: sufficient: Contributor real behavior proof is sufficient. Submitted live output exercises the production GatewayClient and upstream ws implementation against a real OS loopback TCP peer, directly showing the after-fix opening timeout and elapsed deadline behavior.
  • add rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🦞 diamond lobster and patch quality is 🐚 platinum hermit.
  • add status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Sufficient (live_output): Submitted live output exercises the production GatewayClient and upstream ws implementation against a real OS loopback TCP peer, directly showing the after-fix opening timeout and elapsed deadline behavior.

Label justifications:

  • P2: This is a focused reliability fix for a hanging gateway-client connection path with limited blast radius and no evidence of an active widespread outage.
  • merge-risk: 🚨 availability: The PR can terminate slow but eventually valid HTTP/TLS WebSocket upgrades that current clients wait on without this explicit deadline.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🦞 diamond lobster and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Sufficient (live_output): Submitted live output exercises the production GatewayClient and upstream ws implementation against a real OS loopback TCP peer, directly showing the after-fix opening timeout and elapsed deadline behavior.
  • proof: sufficient: Contributor real behavior proof is sufficient. Submitted live output exercises the production GatewayClient and upstream ws implementation against a real OS loopback TCP peer, directly showing the after-fix opening timeout and elapsed deadline behavior.
Evidence reviewed

PR surface:

Source +11, Tests +105. Total +116 across 2 files.

View PR surface stats
Area Files Added Removed Net
Source 1 12 1 +11
Tests 1 105 0 +105
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 2 117 1 +116

What I checked:

Likely related people:

  • steipete: Authored the gateway-client package extraction and subsequent central safe-timeout work that established the owner boundary this patch extends. (role: introduced package and timeout architecture; confidence: high; commits: b1117d98622f, 6f2add2cc65a; files: packages/gateway-client/src/client.ts, packages/gateway-client/src/timeouts.ts)
  • vincentkoc: Recently changed injected-environment watchdog resolution and pre-hello connection lifecycle behavior in the same gateway-client path. (role: recent area contributor; confidence: high; commits: 9d04064e736a, 20964d3e3bbc; files: packages/gateway-client/src/client.ts, packages/gateway-client/src/protocol-client.ts, packages/gateway-client/src/timeouts.ts)
What the crustacean ranks mean
  • 🦀 challenger crab: rare, exceptional readiness with strong proof, clean implementation, and convincing validation.
  • 🦞 diamond lobster: very strong readiness with only minor maintainer review expected.
  • 🐚 platinum hermit: good normal PR, likely mergeable with ordinary maintainer review.
  • 🦐 gold shrimp: useful signal, but proof or patch confidence is still limited.
  • 🦪 silver shellfish: thin signal; proof, validation, or implementation needs work.
  • 🧂 unranked krab: not merge-ready because proof is missing/unusable or there are serious correctness or safety concerns.
  • 🌊 off-meta tidepool: rating does not apply to this item.

Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics.

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.

@steipete steipete self-assigned this Jul 16, 2026
@steipete
steipete force-pushed the fix/gateway-client-ws-handshake-timeout branch 3 times, most recently from 96ada91 to 26ec937 Compare July 16, 2026 09:44
@steipete
steipete force-pushed the fix/gateway-client-ws-handshake-timeout branch from 26ec937 to 1891e87 Compare July 16, 2026 10:20
@steipete
steipete merged commit dacf164 into openclaw:main Jul 16, 2026
99 of 100 checks passed
@steipete

Copy link
Copy Markdown
Contributor

Merged via squash.

github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Jul 17, 2026
…w#108348)

* fix(gateway-client): bound websocket opening handshake waits

* test(gateway-client): harden handshake timeout proof

---------

Co-authored-by: Peter Steinberger <[email protected]>
zooqueen pushed a commit to zooqueen/openclaw that referenced this pull request Sep 11, 2026
…w#108348)

* fix(gateway-client): bound websocket opening handshake waits

* test(gateway-client): harden handshake timeout proof

---------

Co-authored-by: Peter Steinberger <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merge-risk: 🚨 availability 🚨 May cause crashes, hangs, restart loops, stalls, or process outages. P2 Normal backlog priority with limited blast radius. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. size: S status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants