Skip to content

Bug: WebSocket close before connection established causes uncaughtException crash — not covered by isBenignUncaughtExceptionError #88257

Closed

Description

@survivor998

Summary

When a Feishu/WebSocket connection is closed before the handshake completes, Node.js emits an uncaughtException with the message "WebSocket was closed before the connection was established". This error is not classified as benign by isBenignUncaughtExceptionError() in unhandled-rejections--*.js, causing the gateway process to crash.

Current Behavior (v2026.5.28)

The benign error handler in dist/unhandled-rejections--*.js covers:

  • Network errors: ECONNREFUSED, EHOSTUNREACH, ENETUNREACH, EAI_AGAIN, ENOTFOUND, ETIMEDOUT, UND_ERR_CONNECT_TIMEOUT, UND_ERR_DNS_RESOLVE_FAILED, UND_ERR_CONNECT
  • I/O errors: EPIPE, EIO

But it does not cover WebSocket close messages, which are a common transient failure during Feishu gateway reconnection.

The uncaughtException handler in dist/index.js (line ~42) and dist/cli/run-main.js (line ~416) calls isBenignUncaughtExceptionError() which returns false for WebSocket close errors, leading to process.exit(1).

Suggested Fix

Option A (preferred): Add WebSocket close messages to the benign error classification in unhandled-rejections--*.js:

// In isBenignUncaughtExceptionError():
const msg = typeof error === 'string' ? error : (error?.message || '');
if (msg.includes("WebSocket was closed before the connection was established")) {
  return true;
}

Option B: Add a dedicated guard in the uncaughtException handler in index.js and cli/run-main.js, between the benign check and the fatal log:

const msg = formatUncaughtError(error);
if (typeof msg === "string" && msg.includes("WebSocket was closed before the connection was established")) {
  console.warn("[openclaw] Ignored recoverable uncaught exception:", msg);
  return;
}

Environment

  • OpenClaw version: 2026.5.28 (67ddc1a)
  • Files: dist/index.js, dist/cli/run-main.js, dist/unhandled-rejections--*.js
  • Node.js: v22+
  • Related: The upstream isBenignUncaughtExceptionError was added in 2026.4.29 but doesn't cover this case

Impact

Gateway process crash, requiring automatic restart via LaunchAgent. During network instability, this can cause a crash loop, degrading all channel availability.

Activity

  1. clawsweeper commented on May 30, 2026

    @clawsweeper
    Contributor

    Codex review: keeping this open for maintainer follow-up; there is still a little grit to resolve. Reviewed May 30, 2026, 2:24 AM ET / 06:24 UTC.

    Summary
    Keep open: current origin/main and the latest release still miss the exact ws pre-handshake close message in the benign uncaught-exception classifier, while the global handlers exit on non-benign uncaught exceptions. This is a narrow gateway/channel availability bug with no canonical duplicate found.

    Reproducibility: Source-level reproduction is clear for the classifier gap: isBenignUncaughtExceptionError(new Error("WebSocket was closed before the connection was established")) follows the false path on current origin/main. I did not run a live Feishu gateway crash reproduction.

    Ways to help us reproduce this

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

    Next step
    This is a narrow source-proven availability fix in the shared uncaught-exception classifier with clear tests and no config or product decision needed.

    Review details

    Best possible solution:

    Add a narrow central benign-classifier case for the exact ws pre-handshake close error with focused regression coverage, then verify the Feishu/gateway path no longer exits the process.

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

    Source-level reproduction is clear for the classifier gap: isBenignUncaughtExceptionError(new Error("WebSocket was closed before the connection was established")) follows the false path on current origin/main. I did not run a live Feishu gateway crash reproduction.

    Is this the best way to solve the issue?

    Yes: the maintainable fix is to centralize the exact ws transient close classification in src/infra/unhandled-rejections.ts with tests, rather than duplicating guards in src/index.ts and src/cli/run-main.ts.

    AGENTS.md: found and applied where relevant.

    Remaining risk / open question:

    • The live Feishu SDK uncaught-exception path was not executed in this read-only review; the fix should prove the central classifier behavior and, if practical, the Feishu reconnect/cleanup path.

    Codex review notes: model gpt-5.5, reasoning high; reviewed against 94df665cdcc9.

    Label changes

    Label changes:

    • add P1: A transient channel WebSocket reconnect can take down the gateway process and degrade channel availability for real users.
    • add impact:crash-loop: The issue is specifically about an uncaught exception causing gateway exits and possible supervised restart loops.
    • add issue-rating: 🐚 platinum hermit: Current issue advisory state selects this label.
    • add clawsweeper:needs-live-repro: Current issue advisory state selects this label.
    • add clawsweeper:queueable-fix: Current issue advisory state selects this label.
    • add clawsweeper:fix-shape-clear: Current issue advisory state selects this label.

    Label justifications:

    • P1: A transient channel WebSocket reconnect can take down the gateway process and degrade channel availability for real users.
    • impact:crash-loop: The issue is specifically about an uncaught exception causing gateway exits and possible supervised restart loops.
    Evidence reviewed

    Acceptance criteria:

    • node scripts/run-vitest.mjs src/infra/unhandled-rejections.test.ts
    • node scripts/run-vitest.mjs extensions/feishu/src/monitor.cleanup.test.ts

    What I checked:

    • Current classifier gap: On current origin/main, isBenignUncaughtExceptionError only treats configured network codes/message-code regexes plus EPIPE/EIO as benign; the exact ws close-before-open message is not matched. (src/infra/unhandled-rejections.ts:94, 94df665cdcc9)
    • Global handlers still exit after non-benign uncaught exceptions: Both main entrypoints return only for registered handlers or isBenignUncaughtExceptionError; otherwise they format a fatal error and call process.exit(1). (src/index.ts:97, 94df665cdcc9)
    • Dependency contract for the reported message: Upstream ws@8.21.0 constructs WebSocket was closed before the connection was established when close() or terminate() is called while the socket is still CONNECTING, then schedules error emission through abortHandshake. (websockets/ws@8.21.0/lib/websocket.js:305)
    • Feishu default WebSocket path is plausibly affected: Feishu uses WebSocket mode by default, ships ws@8.21.0, starts a WSClient, and cleanup calls wsClient.close(), making the reported close-before-established path relevant to the channel. (extensions/feishu/src/monitor.transport.ts:164, 94df665cdcc9)
    • Regression coverage is missing: The existing benign uncaught-exception test covers network-code cases and a generic error, but it has no assertion for the exact ws close-before-open message. (src/infra/unhandled-rejections.test.ts:405, 94df665cdcc9)
    • Latest release still has the gap: Release v2026.5.27 points at 27ae826f65256c7fbd1d78475fca87b674a53e7b and contains the same classifier shape and Feishu ws@8.21.0, so there is no shipped fix to close against. (src/infra/unhandled-rejections.ts:94, 27ae826f6525)

    Likely related people:

    • vincentkoc: Blame and log history tie the central uncaught-exception classifier, tests, Feishu WebSocket cleanup path, and current gateway WebSocket setup to 723b5085d935296575fa7eba8cd99aad7fc86c77. (role: introduced current behavior and recent area contributor; confidence: high; commits: 723b5085d935; files: src/infra/unhandled-rejections.ts, src/infra/unhandled-rejections.test.ts, extensions/feishu/src/monitor.transport.ts)
    • m1heng: The Feishu package metadata describes the channel plugin as community maintained by @m1heng, which is relevant because the report names Feishu WebSocket reconnect behavior. (role: Feishu plugin owner signal; confidence: medium; files: extensions/feishu/package.json)
    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
    P1High-priority user-facing bug, regression, or broken workflow.
    impact:crash-loopCrash, hang, restart loop, or process-level availability failure.
    issue-rating: 🐚 platinum hermitGood issue quality with a plausible reproduction path needing some confirmation.
    clawsweeper:needs-live-reproClawSweeper needs live local, crabbox, or manual validation to confirm this issue.
    clawsweeper:queueable-fixClawSweeper marked this issue as an existing queue_fix_pr work candidate.
    clawsweeper:fix-shape-clearClawSweeper found a clear likely implementation shape for this issue.
    on May 30, 2026
  3. added a commit that references this issue on May 30, 2026
    f327073
  4. steipete commented on May 30, 2026

    @steipete
    Contributor

    Fixed on main by PR #88298, merged as f327073fb3f2004472daecfdcc4d0e5efe848838.

    Proof:

    node scripts/run-vitest.mjs src/infra/unhandled-rejections.test.ts
    node_modules/.bin/oxfmt --check --threads=1 src/infra/unhandled-rejections.ts src/infra/unhandled-rejections.test.ts
    node scripts/run-oxlint.mjs src/infra/unhandled-rejections.ts src/infra/unhandled-rejections.test.ts
    .agents/skills/autoreview/scripts/autoreview --mode local
    

    PR CI proof on head 58af55a5474e33a9662041da7d4591b60ad62f60:

    Real behavior proof: pass https://github.com/openclaw/openclaw/actions/runs/26685260814/job/78652292196
    dependency-guard: pass https://github.com/openclaw/openclaw/actions/runs/26685260826/job/78652307093
    Security High CodeQL shards: pass https://github.com/openclaw/openclaw/actions/runs/26685261575
    OpenGrep PR Diff: pass https://github.com/openclaw/openclaw/runs/78652324340
    

    The fix keeps the central uncaught-exception classifier narrow: the exact upstream ws pre-handshake close error is benign, while broader prefixed WebSocket errors remain fatal.

  5. added a commit that references this issue on May 30, 2026
    db6053f
  6. added a commit that references this issue on May 31, 2026
    6868b09
  7. locked as resolved and limited conversation to collaborators on Jun 2, 2026
  8. added a commit that references this issue on Jun 10, 2026
    830a9f0
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

    P1High-priority user-facing bug, regression, or broken workflow.clawsweeper:fix-shape-clearClawSweeper found a clear likely implementation shape for this issue.clawsweeper:needs-live-reproClawSweeper needs live local, crabbox, or manual validation to confirm this issue.clawsweeper:queueable-fixClawSweeper marked this issue as an existing queue_fix_pr work candidate.impact:crash-loopCrash, hang, restart loop, or process-level availability failure.issue-rating: 🐚 platinum hermitGood issue quality with a plausible reproduction path needing some confirmation.

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions