Repository navigation
fix(transports/http): match batch replies to requests by id - #5594
Conversation
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
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
🦋 Changeset detectedLatest commit: 9b0473f The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 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".
| 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 |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
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]>
e7ec21b to
ff6d9e9
Compare
There was a problem hiding this comment.
💡 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".
| for (const reply of replies) | ||
| if (!requested.has(reply.id)) unattributed.push(reply) | ||
| else byId.set(reply.id, reply) |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
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]>
|
Published pnpm add viem@pr-5594
pnpm add [email protected] |
Keeps the CommonJS bundle under its 121 kB size limit. Amp-Thread-ID: https://ampcode.com/threads/T-01a1f0ac-6b86-7422-84b5-52795798a132 Co-authored-by: jxom <[email protected]>
Fixes #5561. Supersedes #5593 by @lx3133584, whose commit this keeps.
With
http(url, { batch: true }), the transport sorted batch replies byidand handed them to requests by position. A reply withid: null(sent when a server cannot read a request's id) sorted as0and 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 thesort.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 aSetand aMapinstead of anindexOfper reply.When a server rejects the whole batch with one error,
getHttpRpcClientnow 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 anid: nullerror and a duplicate reply for one request. It fails onmain(requests get shifted results) and on #5593 (eth_dgets"eth_a").