Claude Code harness can emit an uncaught WebSocket error during connection cleanup
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
-
Keep the bridge WebSocket in the connecting state.
-
Let the startup timeout expire.
-
Run cleanup.
-
Observe the uncaught WebSocket error.
Expected behavior
Cleanup should preserve the original timeout error without emitting an uncaught exception.
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.
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-code1.0.91 to reject with the expected bridge startup timeout and separately emit the uncaught cleanup errorWebSocket was closed before the connection was established. packages/harness-claude-code/src/claude-code-harness.tsremoves the socket'serrorlistener before callingterminate(), whilewsemits an asynchronous error when terminating a socket inCONNECTINGstate.- The behavior remains present on
mainafter 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.tsexpects 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
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.