Skip to content

fix(http): match batch replies by id instead of by position - #5593

Closed
lx3133584 wants to merge 2 commits into
wevm:mainfrom
lx3133584:fix/http-batch-null-id-order
Closed

lx3133584 wants to merge 2 commits into
wevm:mainfrom
lx3133584:fix/http-batch-null-id-order

Conversation

@lx3133584

Copy link
Copy Markdown
Contributor

A batch reply carries id: null when the server could not read the id of an item (JSON-RPC 2.0), 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, where a null id takes part in the comparison as zero, so that reply moved the others and each request could receive a neighbour's result.

The reply array is now matched to the requests by id rather than by position: every request gets its id before the batch goes out, and each reply is placed at the position of the request that asked for it. 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.

// server answers the third item with a null id
{ jsonrpc: '2.0', id: null, error: { code: -32600, message: 'id could not be read' } }

Before, the three calls came back as ["0x1", "InvalidRequestRpcError", "0x2"]; they now come back as ["0x1", "0x2", "InvalidRequestRpcError"], which is what the server actually sent for the first two requests.

Tests

Three cases added for a null id at the end, at the start, and in the middle of the batch. On the current code the first one fails with the mismatch above; the other two pass before and after, and guard the attribution rules for the positions the old comparator happened to get right.

  • http.test.ts -t "null" — 3 passed
  • http.test.ts -t "batch" — 17 passed
  • createBatchScheduler.test.ts — 9 passed
  • full http.test.ts — 43 passed, 4 failed (default, behavior: dedupe, behavior: raw, errors: rpc error). Those four open a real RPC connection and fail the same way on main in this environment (no route to the public endpoints); the failure set is unchanged by this change.
  • tsc --noEmit and biome check clean.

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 wevm#5561
@changeset-bot

changeset-bot Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: dd760d0

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

@lx3133584 is attempting to deploy a commit to the Wevm Team on Vercel.

A member of the Team first needs to authorize it.

jxom commented Nov 18, 2026

Copy link
Copy Markdown
Member

Thank you @lx3133584 for the fix and the clear write-up! I've folded your commit (a63e11c) into #5594, which supersedes this PR and fixes #5561. On top of it, a reply that carries a batch request's id now goes only to that request (a duplicate reply could fill another request here). Matching uses a Set and a Map, a request with no reply gets an error, and there's a changeset. Closing this in favor of #5594.

@jxom jxom closed this Nov 18, 2026
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.

2 participants