Repository navigation
fix: Distinguish invalid JSON-RPC messages from JSON parse errors, and fail requests whose answer cannot be read - #401
Merged
jeanmatthieu merged 7 commits intoNov 29, 2026
Conversation
Signed-off-by: bbdu3 <ergouyang854@gmail.com>
Signed-off-by: bbdu3 <ergouyang854@gmail.com>
…with "id": null Follow-up to the review of #375. - Server: a lone response that cannot be read (no `jsonrpc`, or not "2.0") is answered with `"id": null`, as inside a batch. A response's `id` names a request of this server, not one of the peer, so answering with it could fail an unrelated request of the peer. Unreadable messages are logged again, the unreachable `.parseError` catch branch is gone, and a body already decoded as an object is not parsed a second time. - HTTP server transports: an error response with `"id": null`, the answer to a request whose id could not be read (JSON-RPC 2.0 §5), is a response and gets 202 instead of 400 -32600; in a batch it gets no reply. Only an error may carry a null id: a `result` with one still gets -32600. - HTTPClientTransport: the `jsonrpc` check added to JSONRPCMessageKind no longer decides where an SSE stream ends. The transport ends the POST stream on any answer to its request, as before, because `Client` fails the request an unreadable answer names; it no longer resumes the stream and reports a second error for it. - Docs: the protocol guide, the stateless design note and CHANGELOG say which input gets -32700 and which -32600. - Tests name their parameterized cases by their arguments. Assisted-by: Claude
…e client An answer that names a pending request but cannot be read left that request waiting forever, since server-to-client requests have no default timeout. - Server: a response with no `jsonrpc` (or not "2.0"), or an error that does not decode, is still answered with -32600 and `"id": null`, and now also fails the server-to-client request its `id` names, alone or in a batch (Batch.Item.invalidResponse), as Client already does for its requests. - Client: an answer with a `result` or an `error` whose `method` is not a string (`null`, a number) fails the request it names. HTTPClientTransport already treated it as the answer and ended the POST stream, so the request had no stream left to complete on. Assisted-by: Claude
Both HTTP server transports answered a POSTed response whose `jsonrpc` is not "2.0" with 400 before it reached `Server`, so the server-to-client request it names (roots/list, sampling, elicitation) still waited forever over Streamable HTTP, though the stream transports now fail it. JSONRPCMessageKind classifies such an answer with a string or integer `id` as `invalidResponse` instead of failing. Each transport runs its validation pipeline (origin, authentication, session) first, then gives the body to the server, which fails the request it names, and answers 400 with -32600 and `"id": null` as before. Before, the 400 came ahead of validation. CHANGELOG: drop the HTTPClientTransport entry. On rel/1.0.0 the client already ended the POST stream on such an answer; only the intermediate commit of this branch changed that. Assisted-by: Claude
Follow-up to the review of this branch.
- Server: the receive loop's fallback decoded a single message as
`[String: Value]` to find its `id`. An answer with a member no `Value`
holds, such as `"result": {"x": 1e400}`, failed that probe: it got -32700
though it is valid JSON, and the request it names kept waiting, while
the same item in a batch failed it. The fallback now classifies the
message as `Server.Batch.Item`, so single messages and batch items share
one classification.
- Server.Batch.Item: the `case nil` branch no longer splits answers from
other items. JSONRPCMessageKind classifies an item with a `result` or an
`error` whenever its `id` can be read, so the `id` there is always `nil`
and both branches gave the same reply.
- HTTPClientTransport: `answeredRequestID(in:)` reads the answer through
JSONRPCMessageKind (`.response` with an id, or `.invalidResponse`)
instead of a separate JSONSerialization probe.
- Tests: the unreadable-answer test covers an error without its `code` and
a number no `Value` holds, alone and in a batch. An HTTP row pins that
an error without its `code` gets 202: the transports read the shape of
a message, and `Server` fails the request it names.
- Docs: over HTTP an invalid request with a `method` keeps its `id`, and a
response whose `jsonrpc` is not "2.0" gets its 400 after validation.
Assisted-by: Claude
Follow-up to the review of this branch.
- classificationError(for:) told valid JSON from invalid JSON with
JSONSerialization, which rejects a number out of range such as `1e400`.
StatelessHTTPServerTransport parses the body with JSONSerialization, so
a body holding one got -32700 though it is valid JSON, where the other
transports answer -32600. It now probes with JSONDecoder, which reads
such a number as valid JSON and still rejects invalid JSON.
- The HTTP server transports log the dropped `"id": null` -32600 that
`Server` sends for each answer it cannot read at debug, not warning:
the POST was already answered.
- Tests: `1e400` joins the invalid ids of both HTTP transports. The 202
for an error without its `code` gets a test of its own on each HTTP
transport, out of the "still accepted" table. A request whose `params`
hold `1e400` keeps its `id` on the stream transports. The null-id result
that a single stream message accepts is recorded with `withKnownIssue`;
its fix belongs to `Response` decoding.
- docs/src/protocol.src.md (and docs/protocol.md):
- "One with a `method`" becomes "One with a string `method`":
`{"jsonrpc":"2.0","id":7,"method":42}` keeps its id on stdio but not
over HTTP.
- "A response that cannot be read always gets `"id": null`" is split by
transport: over HTTP it gets 400 only when its `jsonrpc` is not "2.0",
and 202 when only its content cannot be read.
- An error response with `"id": null` answers no request "unless one has
the id `""`", since `ID` reads `null` as `""`.
- CHANGELOG: a request whose `id` can be read but whose `params` cannot
gets -32600 with its `id`; over stateful HTTP its POST got no answer.
Kept as is: the stateless transport forwards a message whose headers were
not checked byte for byte, as design/request_headers.md describes.
Assisted-by: Claude
jeanmatthieu
deleted the
fix/pr-375-follow-up-distinguish-invalid-JSON-RPC-messages
branch
November 29, 2026 12:16
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #321. Carries #375 by @bbdu3: its two commits are kept as they are, apart from one conflict in a test file. On top of them, this branch adds the fixes from reviewing it. It is based on
33f0c381. The 7 newer commits onrel/1.0.0only touch docs andSkillTypes.swift, and merge cleanly.44f6577a,c6ef9293e418afb5c8f6b807methoddid the same, becauseHTTPClientTransporthad already ended its stream.d2a5e848"jsonrpc": "2.0"is now given toServerafter validation, and still gets 400. Also drops a CHANGELOG entry for a client change that never shipped.d4a49d791e400hung alone, but not in a batch. The client's stream check reusesJSONRPCMessageKind, a deadBatch.Itembranch is removed, and the missing batch cases get tests.c87f98291e400with -32600, not -32700. The null-id -32600 thatServersends after an HTTP POST was answered is logged at debug. Three guide sentences now say what holds on each transport. The null-id stream result is recorded withwithKnownIssue.Valid JSON that is not a JSON-RPC message now gets -32600 instead of -32700 (JSON-RPC 2.0 §5.1). -32700 is kept for a body that is not valid JSON.
A response's
idnames a request of the server, not one of the peer. Answering an unreadable response with itsidcould make the peer fail an unrelated request of its own, so these answers carry"id": null.Docs:
docs/src/protocol.src.md(withdocs/protocol.mdregenerated),design/stateless.mdandCHANGELOG.md.No public API change. In the package-level
JSONRPCMessageKind,response(id:)now takes an optional id, and a newinvalidResponse(id:)case is added.Evidence
Before: the new tests fail on the base without the fixes:
unreadableAnswerFailsRequest:listRootsnever completes.undecodableAnswerwith{"id":"ID","method":7,"result":{}}: the time limit is exceeded.testInvalidAnswerFailsServerRequest(stateful HTTP, realServer):listRootsnever completes.testInvalidAnswerReachesServer…(both HTTP transports): the answer never reachesServer. On the stateful transport it got 400 ahead of session validation; it now gets 404 for another session and is not handed toServer.StatelessHTTPRequestIDTests.invalidIDwith1e400: -32700.testInvalidBodyErrorCodeget -32700.malformedAnswerIsNotResumedpasses on the base too. It pins the base's client behavior, which an intermediate commit of this branch had broken.After:
swift testpasses, 1451 tests in 144 suites, with one intended known issue (nullIDResult).scripts/run-conformance.sh, against the baselines: 493 client checks and 276 server checks pass.scripts/run-interop.sh --checkmatchesinterop/RESULTS.md.swift scripts/weave.swift --checkandswift scripts/doc-coverage.swiftare clean.Not run:
scripts/ci-linux.sh. The HTTP transport changes also affect Linux.Merge Danger
Door: two-way
Blast Radius: wire
Peers see new error codes:
"jsonrpc": "2.0"."id": null.A peer that matched on -32700 for these inputs will see a different code.
Two cases stay as they are, both because
IDreadsnullas"". A single stream message{"jsonrpc":"2.0","id":null,"result":{}}is accepted and only logged; over HTTP and in a batch it gets -32600. And an error with"id": nullcan answer a pending request whose id is"". The fix belongs toResponsedecoding, a public change kept for later;nullIDResultrecords the first case.AI assistance: written by Claude at the author's request, for human review before merge.