Skip to content

fix(service): keep peer messaging active during response drain - #1524

Merged
DaleSeo merged 4 commits into
modelcontextprotocol:mainfrom
perfloop-oss:perfloop-pr-open-hy5qjn965r
Dec 1, 2026
Merged

DaleSeo merged 4 commits into
modelcontextprotocol:mainfrom
perfloop-oss:perfloop-pr-open-hy5qjn965r

Conversation

@perfloop-agent

@perfloop-agent perfloop-agent commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

An in-flight handler that reports progress after input EOF can block the response drain and lose its reply. The drain added in #759 waits for handler responses but stops polling peer messages and notification-send completions. This change keeps those paths running while output is still writable, without changing the public API.

For an incoming ping with ID 1, a handler can now send progress token 7 and then return its empty result after EOF. Both messages reach the peer. Outbound peer requests instead receive TransportClosed promptly, since shutdown no longer reads their replies.

serve_inner reuses its normal dispatch during the existing five-second EOF or two-second cancellation drain. At the deadline, it aborts and joins unfinished sends before closing the writer, so a backpressured send cannot hold the writer lock through close. Aborting a send at either deadline can leave a partial JSON line before EOF rather than waiting indefinitely for output to become writable.

Notifications still require a writable output; this does not make peer replies available after input processing stops.

Fixes #1523

cc @DaleSeo

Validation and related work

Default-feature-only tests remain unvalidated because the stdio helper targets call feature-gated transport::stdio. Validation uses default features plus transport-io.

The new public-API regressions in crates/rmcp/tests/test_peer_message_drain.rs cover messages queued during drain, already-dispatched sends, and blocked writers under both the five-second EOF and two-second cancellation budgets. The peer-messaging and blocked-send tests fail on the unmodified base. A third test checks that both replies arrive when two handlers finish together after EOF.

cargo test passed 28 focused client/server shutdown, timeout, cancellation, response-concurrency and subscription checks, with no failures or skips.

Related work: #1489 drops the outbound receiver before drain; it leaves pending peer responders and notification-send completions unserviced.

Case and evidence


Made by Perfloop. Human sponsor: Tomás Senart.

Signed-off-by: Perfloop Agent <agent@perfloop.ai>
@perfloop-agent
perfloop-agent requested a review from a team as a code owner November 30, 2026 12:58
@github-actions github-actions Bot added T-test Testing related changes T-core Core library changes labels Nov 30, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 30, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-30T19:03:22.741033Z 0a2cc63 New commits
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@github-actions
github-actions Bot requested a review from connor4312 November 30, 2026 12:58
@github-actions

Copy link
Copy Markdown
Contributor

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

@jokemanfire jokemanfire left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified at head 51ded29, base c78f2764.

Reproduced the claimed red/green with the PR's own test file, unmodified:

  • base c78f2764 + test_peer_message_drain.rs -> 0 passed; 2 failed (peer message blocked the response drain at line 149, blocked send outlived the EOF drain deadline at 213)
  • head 51ded29 -> 2 passed
  • existing regression file test_inflight_response_drain.rs at head -> 2 passed (test_inflight_response_drain_on_eof, test_eof_cancels_pending_requests_after_drain)
  • cargo fmt --all -- --check clean (only the nightly-only rustfmt option warnings)

The new gate reads consistently: drain ends only when the sink channel is closed and empty and both JoinSets are empty, transport.receive() is disabled, PeerSinkMessage::Request is answered with TransportClosed before it can be inserted into local_responder_pool, and clearing the pool on entry drops the responders of requests already on the wire. The sink_proxy_tx.as_ref().expect("not draining") in the request branch is unreachable while draining, because Quit can only come from the select!, which is only reached with batch_messages empty.

Two small things, neither blocking:

  1. blocked_sends_respect_drain_deadline pins the 5 s EOF deadline only; the 2 s cancellation budget has no new test. Parametrising the helper over QuitReason would keep the two deadlines from drifting apart.
  2. On timeout, send_task_set.shutdown() / response_send_tasks.shutdown() abort a transport.send future mid-write. With a buffered writer the last frame can be truncated on the wire (the peer sees a partial JSON line, then EOF). That is probably the right trade-off versus holding the writer lock through close, but the body only covers the backpressure case; one sentence would keep the next reader from treating it as accidental.

Public API unchanged, the shortened drain block reads correctly, and the EofReader in the test is a good touch for determinism.

@perfloop-agent

Copy link
Copy Markdown
Contributor Author

I’m extending the blocked-send helper to exercise both Closed and Cancelled, checking the five-second and two-second budgets for response and notification sends. I’ll also document that aborting an unfinished send at the deadline can leave a partial JSON line before EOF; the production drain logic will stay unchanged.

Signed-off-by: Perfloop Agent <agent@perfloop.ai>
Signed-off-by: Perfloop Agent <agent@perfloop.ai>
@perfloop-agent

Copy link
Copy Markdown
Contributor Author

The blocked-send helper now covers both EOF and cancellation, with response and notification sends checked against the five-second and two-second drain budgets. The focused Cargo tests pass 28 checks with no failures or skips; the production drain logic is unchanged.

The description now explicitly notes that aborting an unfinished send at either deadline can leave a partial JSON line before EOF, rather than waiting indefinitely for a writable output.

DaleSeo
DaleSeo previously approved these changes Nov 30, 2026

@DaleSeo DaleSeo left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix, @perfloop-agent! Approved with a couple of minor suggestions.

Comment thread crates/rmcp/src/service.rs Resolved
Comment thread crates/rmcp/src/service.rs Resolved
Signed-off-by: Perfloop Agent <agent@perfloop.ai>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

T-core Core library changes T-test Testing related changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

In-flight progress notifications block EOF response drain and lose the handler reply

3 participants