Repository navigation
Conversation
Retain a bounded request prefix while draining an oversized line so a complete top-level request id can receive Invalid Request. Keep notifications, responses, malformed input, and late ids silent, with cancellation and recovery coverage.
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. |
|
@michaelneale is on triage rotation this week and is the reviewer for this one. Labels come from the PR labeler. Please review the change. Automated triage. Labels are a first pass and the assigned maintainer has the final say. |
1AbdullahButt
left a comment
There was a problem hiding this comment.
The fix works. I have one behaviour change to point out and one optional test.
read_line now keeps up to min(max_line_length, 8 KiB) of an oversized line in oversized_line_prefix. When the line ends, request_id_from_oversized_prefix tries to read an id and a string method from that prefix. Any id it finds is passed on through PendingErrorResponse::Queue(Some(id)). The scanner fails closed in the case I was most worried about. If the prefix ends in the middle of a number ("id":12 when the real id is 1234), serde still parses 12. But the scanner then needs a , or } after the value, and finds the end of the prefix instead, so it returns None and sends nothing.
I ran cargo test -p rmcp --test test_async_rw_max_line_length --all-features in a rust:1 container: 13 passed. With async_rw.rs reverted to the base commit, 3 fail: the early numeric id, escaped string id, and cancellation tests.
Behaviour change: on the base commit, Step::OversizedNeedsDiscard returned LineRead::Oversized right away, so receive logged "exceeded the maximum line length" as soon as the limit was crossed. Now read_line keeps draining and returns only at the newline. A peer that streams an unterminated line therefore produces no log until the newline. If it hits EOF first, read_line returns Eof and the error is never logged. If that log matters for diagnosing floods, consider emitting it when the limit is first crossed and sending the reply later.
Optional: add a test where the prefix ends inside the id number, to pin the truncated-number behaviour described above.
Reviewed with help from Claude Code.
Exercise the numeric ID boundary in a duplex transport. Verify silence when the retained prefix ends inside the number and a correlated reply once it includes the delimiter.
|
Thanks for testing both versions, @1AbdullahButt. I added the numeric ID boundary regression in Thanks also for flagging the change in when the oversized-line error is logged. Drafted with OpenAI Codex. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 55888475f1
ℹ️ About Codex in GitHub
Codex has been enabled to automatically 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 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| if has_method && id.is_some() { | ||
| return id; |
There was a problem hiding this comment.
Reject malformed retained bytes before replying
When an oversized line begins with complete id and method fields but is already syntactically invalid later in the retained prefix (for example {"jsonrpc":"2.0","id":1,"method":"ping",! followed by padding), this early return accepts the ID without examining the invalid byte. receive then emits a correlated error instead of following the existing syntax/EOF policy that deliberately keeps unparsable input silent, so request-shaped garbage can again provoke the response/error loops that policy avoids; continue validating the retained prefix and reject definite syntax errors before queuing the reply.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in 6ccb893. The scanner now checks the entire retained prefix before replying and distinguishes malformed JSON from a valid prefix cut short by the size limit. Four regression cases failed before the fix and now pass. The line-length suite also passed on Windows with both all features and the minimal server feature.
Drafted with OpenAI Codex.
Keep scanning retained JSON after the request ID and method. Distinguish definite errors from a cutoff at the prefix boundary and preserve only delimited request IDs.
jokemanfire
left a comment
There was a problem hiding this comment.
Verified at 6ccb893 from the code and a local run, not the description.
What I ran (Linux, stable 1.96; Windows not run here)
cargo test -p rmcp --test test_async_rw_max_line_length --all-features— 14 passed, 0 failed.- A scratch probe (not committed) that installs a capturing
tracingsubscriber aroundreceive, to check when the oversized-line error is emitted. Output in the finding below.
Code read
read_line(async_rw.rs:144): the copy intoline_buf/oversized_line_prefixand the matchingconsumehappen in one synchronous block with noawaitbetween them, so the cancellation claim holds — areceivedropped mid-line can neither lose bytes nor leavediscardinghalf-set.request_id_from_oversized_prefix(async_rw.rs:229): fields are recorded only after a,/}delimiter, so"id":12cut at the prefix boundary produces no reply;result/errorkeys, duplicateid/method, and non-stringmethodall fail closed; anidnested insideparamsis invisible because each value is consumed whole. @1AbdullahButt was right that this is the risky part, and it is closed.- The reply is queued in
pending_error_responsebefore the nextread_line, so it is flushed even while the peer keeps writing (async_rw.rs:412-423).
Finding: the oversized-line error log no longer fires for the shape this PR is about
The only tracing::error! on this path is in receive, when read_line returns LineRead::Oversized (async_rw.rs:414-417). Since this PR, that result is produced only when the terminating newline arrives. A peer that never sends one — the report in #1395, and the unterminated_flood_is_discarded_and_stream_recovers test — stays in Step::OversizedNeedsDiscard (async_rw.rs:210); if it closes instead of finishing the line, read_line returns Eof and receive returns None (async_rw.rs:426) without logging anything.
Measured with the capturing subscriber (4096-byte limit, 8 KiB of A):
- line ends with
\n:ERROR rmcp::transport::async_rw: Incoming message exceeded the maximum line length, discarding it max_line_length=4096— exactly once. - peer writes 8 KiB then closes, no newline: no event at all,
receive()returnsNone.
Before this change the log was emitted the moment the cap was crossed (165f8fd:async_rw.rs:189 returned LineRead::Oversized as soon as discard started), so an unterminated flood used to be visible in logs and now is silent. The with_max_line_length doc still promises that over-long lines are "dropped and logged" (async_rw.rs:124). @1AbdullahButt raised the timing change and your reply acknowledges it, but the head commit does not move the log.
Suggestion: emit the error once when entering discarding (the Step::OversizedNeedsDiscard arm), and keep the current one for the newline case — one line per oversized line, never per chunk. That keeps the diagnostic without reintroducing log spam.
Question, non-blocking: the error code
A line that is valid JSON and a well-formed tools/call request, just too large, is answered with -32600 Invalid Request. The HTTP transport answers the same situation with 413 Payload Too Large (streamable_http_server/tower.rs:296-301). -32600 tells a client its message was malformed when it was not; if that code stays, a data field such as {"reason":"line_too_long"} would let callers tell the two apart. I do not have a strong opinion on which is right, but the divergence is worth one sentence in the PR body.
The scanner itself I am happy with: bounded, fail-closed, and I could not construct an input that answers an id belonging to a different message. The log point is the one I would fix before merge, because it hides the failure mode the PR exists for.
|
Thanks for reproducing this and catching the logging regression, @jokemanfire. Review Fixed in I updated the PR description to clarify that stdio uses the correlated JSON-RPC |
jordiboehme
left a comment
There was a problem hiding this comment.
Thanks for picking this up. I tested the design against the case from the issue: a client that sends a large base64 file in a tools/call.
The prefix scan covers clients that write id early (rmcp, the Python SDK). The TypeScript SDK does not: it builds each request as { ...request, jsonrpc: '2.0', id: messageId } (src/shared/protocol.ts in 1.32.2 and on main). JSON.stringify keeps that order, so the line is {"method":"tools/call","params":{...},"jsonrpc":"2.0","id":5}. The id is behind the large params and outside the prefix, so the server stays silent and the client still times out. Most clients that send large inline files are built on the TypeScript SDK, so this is the common case.
A small addition would cover it: keep a short tail of the line as well (some bytes), and when the prefix has a top-level method but no id, read the id from the tail. A raw " cannot appear inside a JSON string, so "id": in the raw bytes is always a key. The member right before the final }, with no other } after its value, belongs to the top-level object. Numbers and strings without escapes would be enough there. A test with the TypeScript key order would pin it.
One smaller point: the answer only says "Invalid request". If the message named the limit, for example "Request exceeds the maximum line length of 16777216 bytes", a client or agent could tell why and send a smaller file or a path instead.
|
Thanks, @jordiboehme. I checked the TypeScript SDK's request builder and stdio serializer. A large The TypeScript field order deserves a regression test. For late-ID recovery, I'd want the reader to establish that the ID belongs to the top-level request and is unambiguous across the whole line before sending a correlated reply. That requires tracking JSON structure through the discarded bytes while keeping memory bounded. I agree about the error message too. Including the configured line limit in bytes while keeping |
Summary
Oversized JSON-RPC requests on stdio were silently discarded, so the caller waited until its own timeout even when the request ID appeared near the start of the line. This change returns a correlated
-32600 Invalid Requestafter the oversized line is drained.Fixes #1395.
Root cause
AsyncRwTransport::read_lineclearedline_bufas soon as the configured limit was crossed.receivethen saw onlyLineRead::Oversized, logged the drop, and continued. The request ID was already gone, and the existing cancellation-safe protocol-error queue could only build an uncorrelated response.Approach
Retain a bounded prefix while draining an oversized line. The retained bytes are capped at
min(max_line_length, 8 KiB), live on the transport so cancellation cannot lose them, and are cleared after the terminating newline.The transport logs each oversized line when it crosses the limit, even if the peer closes without sending a newline.
A top-level scanner delegates complete JSON tokens to Serde and recovers only an integer/string
idpaired with a stringmethod. The recovered ID is passed through the existing queue/feed/flush state from #1370. Incomplete or ambiguous prefixes fail closed.Scope
This is limited to
AsyncRwTransportand its line-limit regression suite. Public APIs, dependencies, the configured/default size limits, normal-size messages, and unbounded mode are unchanged.Oversized notifications, responses, non-JSON input, and requests whose ID is outside the retained prefix remain unanswered, preserving the error-storm protection from #938.
Regression tests
Before the production change, the expanded focused suite reported 10 passed / 2 failed: both a numeric ID and an escaped string ID produced no response.
All 16 line-length tests pass with the follow-up. On
6ccb893, the tests expecting a log before the newline and on an unterminated close both failed.Coverage includes
-32600response and delivery of the following request;Verification
Local platform: Windows, Rust 1.96.1 unless noted. Linux/macOS were not run locally.
cargo test -p rmcp --test test_async_rw_max_line_length --all-features— 16 passed.cargo test -p rmcp --test test_async_rw_max_line_length --no-default-features --features server— 16 passed.cargo test -p rmcp --lib --no-default-features— 206 passed.cargo test -q --all-features -- --skip default_http_client_preserves_connection_failure_cause --skip rmcp_crate_ships_license --skip rmcp_macros_crate_ships_license— 1,219 passed, 32 ignored, and the three named Windows tests excluded.npmspawn in test_with_js #1364.LICENSEsymlinks are materialized as../../LICENSEtext.cargo clippy --package rmcp --all-targets --all-features -- -D warnings— passed.localclippy across all 29 features — passed.cargo check --workspace --all-targets— passed.cargo +1.88.0 check --all-targets --all-features— passed at the declared MSRV.cargo +nightly fmt --all -- --checkandgit diff --check— passed.Compatibility
No API or dependency changes. The wire behavior changes only for an oversized, request-shaped line with a safely recovered ID. For oversized requests, stdio uses JSON-RPC
-32600 Invalid Requestwith the recovered ID, and Streamable HTTP uses HTTP413when the request body exceeds its limit. Memory remains bounded by the existing line cap plus at most 8 KiB, and cancellation can interrupt/resume draining without losing reply state.Related work
AI assistance
This PR was prepared primarily by OpenAI Codex, including issue analysis, implementation, regression tests, local verification, and the PR description.