Skip to content

fix: route invalid IPC JSON to request errors - #5586

Merged
jxom merged 4 commits into
mainfrom
fix/ipc-invalid-json
Nov 18, 2026
Merged

jxom merged 4 commits into
mainfrom
fix/ipc-invalid-json

Conversation

@jxom

@jxom jxom commented Nov 18, 2026

Copy link
Copy Markdown
Member

Supersedes #5574 by @yinbowen-dc (keeps their commit). Fixes #5562.

Malformed JSON from an IPC peer threw inside the socket data listener, crashing the process or leaving requests to time out. It now rejects pending requests, as the WebSocket transport does.

On top of #5574:

  • Only JSON.parse is guarded. Errors thrown by response or subscription callbacks no longer reject unrelated requests or force a reconnect.
  • Invalid JSON calls socket.destroy(error), so the error still reaches pending requests through the socket's error event and the socket is closed when reconnect: false.
  • Moved the test into socket.timeout.test.ts to reuse its IPC peer helper, and added a test for a throwing callback.
  • Named the ipc transport in the changeset.

#5573 (IPC stray-brace framing) should land after or together with this PR: on its own it lets a stray } followed by an invalid frame reach JSON.parse and throw uncaught.

yinbowen-dc and others added 4 commits November 18, 2026 21:00
Only JSON.parse is guarded now, so an error thrown by a response or
subscription callback no longer rejects unrelated requests or forces a
reconnect. Invalid JSON destroys the socket, which reports the error
through its error event and closes the socket when it will not
reconnect.

Amp-Thread-ID: https://ampcode.com/threads/T-01a1f04d-baca-7631-b26d-fcb0d908dcd6
Co-authored-by: jxom <[email protected]>
@changeset-bot

changeset-bot Bot commented Nov 18, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 3692800

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

@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 8:34pm UTC

Request Review

@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-18T20:33:09.991564Z 3692800 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-5586 dist-tag, which follows this pull request until it closes.

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

@tydavid5 tydavid5 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Reviewed this against the issue it fixes, and it checks out.

Before, malformed JSON from an IPC peer threw inside the socket data listener, so a request could hang until timeout or crash the process. Wrapping the JSON.parse in a try and calling socket.destroy with the error is the right fix, because the socket error handler is already registered and that is what rejects the pending requests with the SyntaxError.

The test covers the exact case: it sends a truncated result, expects the request to reject as a SyntaxError, expects no pending requests left, and expects the socket destroyed. The second test is also worth keeping, it checks that a throwing response callback does not reject another pending request, and it restores the global uncaughtException listeners in a finally block.

Approving, the fix is correct and tested.

@jxom
jxom merged commit d8cac52 into main Nov 18, 2026
39 of 40 checks passed
@jxom
jxom deleted the fix/ipc-invalid-json branch November 18, 2026 21:30
@github-actions github-actions Bot mentioned this pull request Nov 18, 2026

This branch had an error being deployed

1 failed deployment
Preview — 36928009 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.

ipc transport: a frame that is not valid JSON throws an uncaught exception

4 participants