Skip to content

fix: close the socket when a socket client is dropped - #5597

Merged
jxom merged 1 commit into
mainfrom
fix/socket-close-dropped-client
Nov 18, 2026
Merged

jxom merged 1 commit into
mainfrom
fix/socket-close-dropped-client

Conversation

@jxom

@jxom jxom commented Nov 18, 2026

Copy link
Copy Markdown
Member

Fixes #5565

With reconnect: false (or once reconnect attempts run out), the socket client dropped itself from the cache when its socket failed, but never closed the socket. A frame that is not JSON reaches that path without the peer closing the connection, so the old WebSocket stayed open and the next request opened a second one.

The fix is in the shared socket layer (src/utils/rpc/socket.ts), so it covers both webSocket and IPC:

  • When attemptReconnect() gives up, it now marks the client closed and closes its socket. Closing calls back into attemptReconnect(), so the flag stops the recursion.
  • onError and onClose now clear requests after notifying them. Before, a pending request() stayed in the map, so the close that follows an error reported it a second time, and every later disconnect reported it again.

Tests (in src/utils/rpc/webSocket.test.ts, against a real ws server, no mocks; both fail on main):

  • closes the dropped socket after a frame that is not JSON: on main the server holds 2 open sockets from one client; now 1.
  • reports a failed request once, not again on later disconnects: on main the request's onError received SyntaxError, SocketClosedError, the disconnect's error event, and another SocketClosedError; now only SyntaxError.

@vercel

vercel Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
viem Error Error Nov 18, 2026 10:20pm UTC

Request Review

@changeset-bot

changeset-bot Bot commented Nov 18, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 2f51666

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
viem Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-18T22:19:27.085257Z 2f51666 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@github-actions

Copy link
Copy Markdown
Contributor

Published [email protected] under the pr-5597 dist-tag, which follows this pull request until it closes.

pnpm add viem@pr-5597
pnpm add [email protected]

@jxom
jxom merged commit 6c3e120 into main Nov 18, 2026
39 of 40 checks passed
@jxom
jxom deleted the fix/socket-close-dropped-client branch November 18, 2026 22:44
@github-actions github-actions Bot mentioned this pull request Nov 18, 2026

This branch had an error being deployed

1 failed deployment
Preview — 2f51666b Deployed Nov 18, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

webSocket transport: with reconnect: false, a frame that is not JSON leaves the old socket open

2 participants