Skip to content

fix(transports/http): match batch replies to requests by id - #5594

Merged
jxom merged 4 commits into
mainfrom
fix/http-batch-null-id
Nov 18, 2026
Merged

jxom merged 4 commits into
mainfrom
fix/http-batch-null-id

Conversation

@jxom

@jxom jxom commented Nov 18, 2026 •

Copy link
Copy Markdown
Member

Fixes #5561. Supersedes #5593 by @lx3133584, whose commit this keeps.

With http(url, { batch: true }), the transport sorted batch replies by id and handed them to requests by position. A reply with id: null (sent when a server cannot read a request's id) sorted as 0 and shifted every later reply onto the wrong request, with no error.

Changes

  • a63e11c (@lx3133584): the transport gives each batched request its id before the batch goes out and puts replies back in request order by id, replacing the sort.
  • On top of it: a reply that carries the id of a request in the batch now goes only to that request. In fix(http): match batch replies by id instead of by position #5593 a duplicate reply could fill a different request that got no reply. Replies that carry no requested id (null, missing, or unknown) still fill the requests left without a reply in order. Any request still without a reply gets a -32603 "Missing response for request." error, never another request's result. Matching uses a Set and a Map instead of an indexOf per reply.
  • Added a patch changeset.

When a server rejects the whole batch with one error, getHttpRpcClient now gives each copy of that error its request's id. Before, an error whose id matched one request reached only that request. Otherwise the client still returns the server's replies as-is. The batch scheduler is unchanged: it resolves by index, which is correct now that the transport returns replies in request order.

Tests

Replaced the three null-id tests with one, batch: matches responses by id. A real local server replies out of order, with an id: null error and a duplicate reply for one request. It fails on main (requests get shifted results) and on #5593 (eth_d gets "eth_a").

A batch reply carries `id: null` when the server could not read the id of
an item, and the order of the replies is not tied to the order of the
requests. The transport sorted the replies by id with `a.id - b.id` and
handed them out by index, so a `null` id took part in the comparison as
zero and moved the other replies to the wrong requests.

Each request now gets its id before the batch goes out, and the replies
are placed back by id. A reply whose id matches no request in the batch
cannot be attributed, so it fills the first request that has no reply of
its own.

The tests cover a null id at the end, at the start and in the middle of
the batch.

Closes #5561
@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:39pm UTC

Request Review

@changeset-bot

changeset-bot Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 9b0473f

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:37:31.087075Z 9b0473f New commits
ℹ️ 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.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: e7ec21be12

ℹ️ 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/utils/rpc/http.ts Outdated
Comment on lines +251 to +254
const replies = new Map(data.map((reply) => [reply?.id, reply]))
return request.map(({ id }, i) => {
const reply = replies.get(id) ?? data[i]
if (reply?.id === id || reply?.id == null) return reply

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Preserve positional matching for explicit null request IDs

When getHttpRpcClient.request receives multiple batch entries with explicit id: null (permitted by RpcRequest and preserved by the request construction), the map collapses their replies under the same key. Every such request then receives the last null-ID reply, even when the server returned responses in request order. For example, responses containing results "0x1" and "1" become two copies of "1"; previously those responses remained distinct. Bypass the map lookup for nullish request IDs so these replies retain the positional behavior described above.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This was on the superseded commit e7ec21b. The current branch no longer matches replies by id in getHttpRpcClient. The transport always assigns numeric ids, so null request ids never reach its matching.

A reply that carries the id of a request in the batch now goes only to
that request, so a duplicate reply no longer fills a request that got no
reply of its own. Replies without a requested id still fill the requests
left without a reply, and any request still without one gets a
"Missing response for request." error. Matching uses a Set and a Map
instead of indexOf per reply.

Replaces the three null-id tests with one test that fails on main and
covers out-of-order, null-id, and duplicate replies, and adds a changeset.

Amp-Thread-ID: https://ampcode.com/threads/T-01a1f0ac-6b86-7422-84b5-52795798a132
Co-authored-by: jxom <[email protected]>

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: ff6d9e94fa

ℹ️ 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/clients/transports/http.ts Outdated
Comment on lines +220 to +222
for (const reply of replies)
if (!requested.has(reply.id)) unattributed.push(reply)
else byId.set(reply.id, reply)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Preserve whole-batch errors when their ID matches a request

When a server rejects an entire batch with a single error whose ID matches one of the requests, getHttpRpcClient.request expands that error into one entry per request (src/utils/rpc/http.ts, return body.map(() => error)). This map collapses those entries under the matching ID, so only that request receives the original error; every other request receives a synthetic -32603 "Missing response for request." instead. Previously all callers received the actual server error. Preserve whole-batch error fan-out before applying per-request matching so callers retain the correct error classification and details.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in c38aaef: when a server rejects the whole batch with one error, getHttpRpcClient now gives each copy of that error its request's id, so the transport hands it to every request. The one-element-array test now has the error carry the first request's id, and it fails without this fix.

A server that rejects a whole batch with one error answers every
request. When that error carried the id of one request, the transport
gave it only to that request and a "Missing response" error to the
rest. Each copy now carries its request's id.

Amp-Thread-ID: https://ampcode.com/threads/T-01a1f0ac-6b86-7422-84b5-52795798a132
Co-authored-by: jxom <[email protected]>
@github-actions

Copy link
Copy Markdown
Contributor

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

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

@jxom
jxom merged commit 9d48225 into main Nov 18, 2026
39 of 40 checks passed
@jxom
jxom deleted the fix/http-batch-null-id 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 — 9b0473fc 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.

http transport: a batch reply with id: null gives the other requests the wrong results

3 participants