Skip to content

fix: Distinguish invalid JSON-RPC messages from JSON parse errors, and fail requests whose answer cannot be read - #401

Merged
jeanmatthieu merged 7 commits into
rel/1.0.0from
fix/pr-375-follow-up-distinguish-invalid-JSON-RPC-messages
Nov 29, 2026
Merged

jeanmatthieu merged 7 commits into
rel/1.0.0from
fix/pr-375-follow-up-distinguish-invalid-JSON-RPC-messages

Conversation

@jeanmatthieu

Copy link
Copy Markdown
Contributor

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 on rel/1.0.0 only touch docs and SkillTypes.swift, and merge cleanly.

Commit What it does
44f6577a, c6ef9293 #375 as submitted
e418afb5 Applies the review comments on #375
c8f6b807 Fixes two hangs next to the code of #375 that predate it. An answer the server could not read left the server-to-client request it names waiting forever. On the client, an answer with a non-string method did the same, because HTTPClientTransport had already ended its stream.
d2a5e848 Extends the server fix to Streamable HTTP. A POSTed answer without "jsonrpc": "2.0" is now given to Server after validation, and still gets 400. Also drops a CHANGELOG entry for a client change that never shipped.
d4a49d79 A single unreadable answer is read as a batch item is: an answer holding 1e400 hung alone, but not in a batch. The client's stream check reuses JSONRPCMessageKind, a dead Batch.Item branch is removed, and the missing batch cases get tests.
c87f9829 Stateless HTTP answers valid JSON holding 1e400 with -32600, not -32700. The null-id -32600 that Server sends 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 with withKnownIssue.

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.

 Server receive loop (stdio, in-memory, Network), unreadable message
-  -32700, with the message's id, even for a response
+  invalid JSON                    -> -32700, "id": null
+  valid JSON, request             -> -32600, the message's id (string or integer)
+  valid JSON, response            -> -32600, "id": null, and fails the server's pending request it names
+  logged as "Invalid message"

 HTTP server transports, POST body
-  anything unclassifiable                     -> 400 -32700
+  invalid JSON                                -> 400 -32700
+  scalar, array, object that is no message    -> 400 -32600 (also with a number such as 1e400)
-  response without "jsonrpc": "2.0"           -> 202
+  response without "jsonrpc": "2.0"           -> 400 -32600, still given to Server after validation,
+                                                 which fails the pending request it names
-  error response with "id": null              -> 400 -32700
+  error response with "id": null (§5)         -> 202, no reply in a batch

 Batch item, unreadable response
-  -32600 with "id": null, pending request waits forever
+  -32600 with "id": null, pending request fails with -32603

 HTTPClientTransport, SSE answer without "jsonrpc": "2.0"
   ends the POST stream, as on the base

 Client, answer with result/error and a non-string method
-  ignored: the request waits forever (the transport already ended its stream)
+  fails the request it names with -32603

A response's id names a request of the server, not one of the peer. Answering an unreadable response with its id could make the peer fail an unrelated request of its own, so these answers carry "id": null.

Docs: docs/src/protocol.src.md (with docs/protocol.md regenerated), design/stateless.md and CHANGELOG.md.

No public API change. In the package-level JSONRPCMessageKind, response(id:) now takes an optional id, and a new invalidResponse(id:) case is added.

Evidence

  • Before: the new tests fail on the base without the fixes:

    • unreadableAnswerFailsRequest: listRoots never completes.
    • undecodableAnswer with {"id":"ID","method":7,"result":{}}: the time limit is exceeded.
    • testInvalidAnswerFailsServerRequest (stateful HTTP, real Server): listRoots never completes.
    • testInvalidAnswerReachesServer… (both HTTP transports): the answer never reaches Server. On the stateful transport it got 400 ahead of session validation; it now gets 404 for another session and is not handed to Server.
    • StatelessHTTPRequestIDTests.invalidID with 1e400: -32700.
    • The -32600 rows of testInvalidBodyErrorCode get -32700.
    • malformedAnswerIsNotResumed passes on the base too. It pins the base's client behavior, which an intermediate commit of this branch had broken.

    After: swift test passes, 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 --check matches interop/RESULTS.md.

  • swift scripts/weave.swift --check and swift scripts/doc-coverage.swift are 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:

  • -32600 where they used to get -32700.
  • 400 instead of 202 for a POSTed response without "jsonrpc": "2.0".
  • 202 instead of 400 for an error response with "id": null.

A peer that matched on -32700 for these inputs will see a different code.

Two cases stay as they are, both because ID reads null as "". 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": null can answer a pending request whose id is "". The fix belongs to Response decoding, a public change kept for later; nullIDResult records the first case.

AI assistance: written by Claude at the author's request, for human review before merge.

bbdu3 and others added 7 commits November 28, 2026 16:15
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
jeanmatthieu requested a review from a team as a code owner November 29, 2026 12:03

@canarik canarik 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.

Thank you @jeanmatthieu

@jeanmatthieu
jeanmatthieu merged commit 9b6973b into rel/1.0.0 Nov 29, 2026
14 checks passed
@jeanmatthieu
jeanmatthieu deleted the fix/pr-375-follow-up-distinguish-invalid-JSON-RPC-messages branch November 29, 2026 12:16
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.

3 participants