ai icon indicating copy to clipboard operation
ai copied to clipboard

Claude Code harness can emit an uncaught WebSocket error during connection cleanup

Open JoanLaRosa opened this issue 3 months ago • 4 comments

Hi, thanks for your work on the harness packages.

We are using version 1.0.90, the latest published version. Bridge startup can still end with this uncaught exception while the WebSocket is connecting:

WebSocket was closed before the connection was established

The error listener appears to be removed before the socket is terminated. Termination then emits an error with no listener, masking the original bridge timeout and preventing task failure handlers from running.

We saw this fail a long running Trigger task on all three attempts.

Related issues

Issue 16878 reported a related bridge hello race. Pull request 16882 fixed it in version 1.0.70. We are still seeing this cleanup failure on version 1.0.90.

Steps to reproduce

  1. Keep the bridge WebSocket in the connecting state.

  2. Let the startup timeout expire.

  3. Run cleanup.

  4. Observe the uncaught WebSocket error.

Expected behavior

Cleanup should preserve the original timeout error without emitting an uncaught exception.

JoanLaRosa avatar Aug 26 '26 17:08 JoanLaRosa

Identify issue type

Identified as Bug
Confidence: High

Rationale

This describes a concrete failure in existing Claude Code harness cleanup behavior, with reproduction steps, expected behavior, and actual behavior.

Evidence

  • Version 1.0.90 reportedly removes the WebSocket error listener before terminating a still-connecting socket.
  • When bridge startup times out, cleanup emits an uncaught "WebSocket was closed before the connection was established" error.
  • The uncaught cleanup error masks the expected bridge timeout and can terminate or repeatedly fail long-running tasks.

ai-sdk-factory[bot] avatar Aug 26 '26 17:08 ai-sdk-factory[bot]

Bug Reproduction - AI SDK 7

Bug was reproduced.

Branch: main PR: successful reproduction pull request

Details

  • A stalled WebSocket handshake on Node.js 22.23.2 causes @ai-sdk/harness-claude-code 1.0.91 to reject with the expected bridge startup timeout and separately emit the uncaught cleanup error WebSocket was closed before the connection was established.
  • packages/harness-claude-code/src/claude-code-harness.ts removes the socket's error listener before calling terminate(), while ws emits an asynchronous error when terminating a socket in CONNECTING state.
  • The behavior remains present on main after the reported 1.0.90 release, so upgrading to the checked-out 1.0.91 package does not resolve it.
  • examples/ai-functions/src/reproduction/claude-code-connecting-cleanup-error.ts expects timeout cleanup without an uncaught exception, but observed the reported exception and exited with code 1.

Relevant Documentation

  • https://github.com/websockets/ws/blob/master/doc/ws.md
  • https://nodejs.org/api/events.html
  • https://nodejs.org/api/process.html

ai-sdk-factory[bot] avatar Aug 26 '26 17:08 ai-sdk-factory[bot]

Bugfix - AI SDK 7

Created bugfix and opened PR: #19750

Root Cause

Startup cleanup removed the WebSocket error listener before calling terminate(); the reproduction confirmed that ws asynchronously emits an error when terminating a socket still in CONNECTING state, leaving that error unhandled.

Summary

Preserved the WebSocket error and close listeners through termination cleanup, added a patch changeset, and removed reproduction-only artifacts.

Testing

Added regression coverage simulating an asynchronous termination error and verifying that the original startup timeout is preserved without an unhandled error.

ai-sdk-factory[bot] avatar Aug 26 '26 18:08 ai-sdk-factory[bot]

:rocket: Published in:

Package Version
@ai-sdk/google 4.0.55 github npm
@ai-sdk/google-vertex 5.0.67 github npm
@ai-sdk/harness-claude-code 1.0.95 github npm
@ai-sdk/harness-opencode 1.0.93 github npm
@ai-sdk/xai 4.0.48 github npm

github-actions[bot] avatar Aug 27 '26 15:08 github-actions[bot]