Skip to content

fix(gateway): prevent 1006 errors from race condition in WebSocket upgrade#43392

Merged
grp06 merged 5 commits into
openclaw:mainfrom
dalefrieswthat:fix/43381-gateway-health-disconnected-1006
Apr 20, 2026
Merged

fix(gateway): prevent 1006 errors from race condition in WebSocket upgrade#43392
grp06 merged 5 commits into
openclaw:mainfrom
dalefrieswthat:fix/43381-gateway-health-disconnected-1006

Conversation

@dalefrieswthat

Copy link
Copy Markdown
Contributor

Summary

Fix race condition where WebSocket connections could fail with 1006 (abnormal closure) because the upgrade handler was attached after the HTTP server started listening.

Root cause: The gateway started listening for HTTP connections before the WebSocket upgrade handler was attached. During this window, any incoming WebSocket connection would be dropped (socket destroyed) without a proper close frame, causing client-side 1006 errors.

Changes:

  • Create WebSocketServer and attach upgrade handlers before the HTTP servers start listening
  • Add error logging to the upgrade handler's catch block (was previously silent)
  • Improve 1006 error message with troubleshooting hints (TLS mismatch, gateway not ready, etc.)

Change Type

  • Bug fix

Scope

  • Gateway / orchestration

Linked Issue/PR

User-visible / Behavior Changes

Before: gateway health could intermittently fail with WebSocket 1006, especially immediately after gateway startup.

After: WebSocket connections are properly handled from the moment the gateway starts listening. The 1006 error message now includes troubleshooting hints when it does occur.

Security Impact

  • New permissions/capabilities? No
  • Secrets/tokens handling changed? No
  • New/changed network calls? No
  • Command/tool execution surface changed? No
  • Data access scope changed? No

Repro + Verification

  • Environment: macOS, Node 22+
  • Steps:
    1. Start gateway: pnpm openclaw gateway run --port 18889 --bind loopback --auth token --token test --allow-unconfigured
    2. Immediately run: pnpm openclaw gateway health --url ws://127.0.0.1:18889 --token test
  • Expected: Health check succeeds
  • Actual before fix: Health check could fail with 1006 (abnormal closure) if connection arrived during the race window

Compatibility / Migration

  • Backward compatible? Yes | Config/env changes? No | Migration needed? No

@openclaw-barnacle openclaw-barnacle Bot added gateway Gateway runtime cli CLI command changes size: S labels Mar 11, 2026
@greptile-apps

greptile-apps Bot commented Mar 11, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR correctly fixes a startup race condition in the WebSocket upgrade path by restructuring the initialization order in createGatewayRuntimeState. The WebSocketServer is now created and attachGatewayUpgradeHandler is called per HTTP server before listenGatewayHttpServer, ensuring no upgrade requests can arrive on an unhandled socket. It also adds error logging to the previously-silent catch block and improves the 1006 error message with actionable troubleshooting hints.

Changes:

  • server-runtime-state.ts: WebSocketServer creation and attachGatewayUpgradeHandler moved before listenGatewayHttpServer calls, eliminating the race window
  • server-http.ts: log parameter added to attachGatewayUpgradeHandler; catch block now logs remote address and error message before destroying the socket
  • call.ts: formatGatewayCloseError expanded with multi-line troubleshooting hints for 1006 closures
  • lifecycle.test.ts: Cosmetic reformatting of a vi.fn<>() call, no logic changes

Confidence Score: 5/5

  • This PR is safe to merge — the fix is minimal, targeted, and correctly eliminates the documented race condition without introducing new risks.
  • The change simply reorders two existing operations (WSS creation + upgrade handler attachment before listen) with no new logic paths. Error logging in the catch block is additive only. The 1006 message improvement is user-facing text with no behavioral impact. All affected code paths were already exercised in production; the fix narrows a timing window rather than introducing new code branches.
  • No files require special attention.

Last reviewed commit: 68e4af2

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: c83771e6a3

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread src/cli/daemon-cli/lifecycle.test.ts Outdated
@dalefrieswthat dalefrieswthat force-pushed the fix/43381-gateway-health-disconnected-1006 branch 3 times, most recently from 68aacde to aa42f9e Compare April 1, 2026 19:13
@dalefrieswthat dalefrieswthat force-pushed the fix/43381-gateway-health-disconnected-1006 branch from aa42f9e to ed83da0 Compare April 1, 2026 19:15
@grp06 grp06 self-assigned this Apr 20, 2026
@grp06 grp06 force-pushed the fix/43381-gateway-health-disconnected-1006 branch from ed83da0 to 0bca6d3 Compare April 20, 2026 23:28
@grp06 grp06 merged commit 7b5527a into openclaw:main Apr 20, 2026
79 of 80 checks passed
@grp06

grp06 commented Apr 20, 2026

Copy link
Copy Markdown
Contributor

Merged via squash.

Thanks @dalefrieswthat!

loongfay pushed a commit to YuanbaoTeam/openclaw that referenced this pull request Apr 21, 2026
…grade (openclaw#43392)

Merged via squash.

Prepared head SHA: 0bca6d3
Co-authored-by: dalefrieswthat <176454532+dalefrieswthat@users.noreply.github.com>
Co-authored-by: grp06 <1573959+grp06@users.noreply.github.com>
Reviewed-by: @grp06
lovewanwan pushed a commit to lovewanwan/openclaw that referenced this pull request Apr 28, 2026
…grade (openclaw#43392)

Merged via squash.

Prepared head SHA: 0bca6d3
Co-authored-by: dalefrieswthat <176454532+dalefrieswthat@users.noreply.github.com>
Co-authored-by: grp06 <1573959+grp06@users.noreply.github.com>
Reviewed-by: @grp06
ogt-redknie pushed a commit to ogt-redknie/OPENX that referenced this pull request May 2, 2026
…grade (openclaw#43392)

Merged via squash.

Prepared head SHA: 0bca6d3
Co-authored-by: dalefrieswthat <176454532+dalefrieswthat@users.noreply.github.com>
Co-authored-by: grp06 <1573959+grp06@users.noreply.github.com>
Reviewed-by: @grp06
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request May 9, 2026
…grade (openclaw#43392)

Merged via squash.

Prepared head SHA: 0bca6d3
Co-authored-by: dalefrieswthat <176454532+dalefrieswthat@users.noreply.github.com>
Co-authored-by: grp06 <1573959+grp06@users.noreply.github.com>
Reviewed-by: @grp06
globalcaos pushed a commit to globalcaos/tinkerclaw that referenced this pull request May 13, 2026
…grade (openclaw#43392)

Merged via squash.

Prepared head SHA: 0bca6d3
Co-authored-by: dalefrieswthat <176454532+dalefrieswthat@users.noreply.github.com>
Co-authored-by: grp06 <1573959+grp06@users.noreply.github.com>
Reviewed-by: @grp06
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request May 24, 2026
…grade (openclaw#43392)

Merged via squash.

Prepared head SHA: 0bca6d3
Co-authored-by: dalefrieswthat <176454532+dalefrieswthat@users.noreply.github.com>
Co-authored-by: grp06 <1573959+grp06@users.noreply.github.com>
Reviewed-by: @grp06
jameslcowan pushed a commit to jameslcowan/openclaw that referenced this pull request Jun 2, 2026
…grade (openclaw#43392)

Merged via squash.

Prepared head SHA: 0bca6d3
Co-authored-by: dalefrieswthat <176454532+dalefrieswthat@users.noreply.github.com>
Co-authored-by: grp06 <1573959+grp06@users.noreply.github.com>
Reviewed-by: @grp06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cli CLI command changes gateway Gateway runtime size: M

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Gateway shows running, health check says disconnected (1006)

2 participants