Skip to content

StdioMcpTransport writes a request's progress or response after handling its notifications/cancelled #1375

Backlog

Description

@joulook

Bug description

In the modern StdioMcpTransport, handleCancel (reader thread) removes the request from inFlight (L224). Only
after that does it set the request's cancelled flag (L227)
(L219-L230).

That leaves a narrow three-thread race:

  • Response callback. It can run concurrently with the reader, and it also removes the entry before it queues
    the response
    (L269-L276).
  • Writer thread. The writer checks the flag and writes as two separate steps
    (L294-L310).

Once handleCancel has started for a request still in progress, the following are still written:

  • progress, a response, or an internal error that the request emits before the flag is set;
  • a response whose callback won the remove, together with the progress queued before it, because the cancellation
    then flags nothing;
  • an item the writer checked before the flag was set.

This breaks the rule in
stdio.mdx L81-L82:
"Servers SHOULD stop work on a cancelled request as soon as practical and MUST NOT send any further messages for
it."

Impact: a client that abandoned the request gets stray progress, a result or an error.
cancellationDropsOutputQueuedBeforeIt covers only output queued before the handling.

Environment

main 779893c, modern stack (mcp-core), stdio, JDK 17.

Steps to reproduce

  1. Send call 1 (progress token t1) and call 2.
  2. While call 1 is still running, send notifications/cancelled for call 1.
  3. Make call 1 emit progress just after handleCancel removed its entry. On main, the transport's debug line
    Request {} cancelled by the client sits exactly there.

Expected behavior

Once handleCancel has started for a request still in progress, nothing more is written for it. A response the
callback took before that may still go out, with the progress queued before it.

Minimal Complete Reproducible example

In StdioMcpTransportTests (abridged). stream is call 1's response sink.

var hook = new UnsynchronizedAppenderBase<ILoggingEvent>() {
  @Override protected void append(ILoggingEvent e) {
    if (e.getMessage().startsWith("Request {} cancelled by the client")) {
      stream.tryEmitNext(new JSONRPCNotification("notifications/progress",
          Map.of("progressToken", "t1", "progress", 1.5)));   // emitted between remove and flag
      // then let call 2 answer, and wait until its result is written
    }
  }
};
hook.start();
Logger log = (Logger) LoggerFactory.getLogger(StdioMcpTransport.class);
log.addAppender(hook); log.setLevel(Level.DEBUG);
// send tools/call 1 (t1) and 2, then notifications/cancelled for 1
// assert: no progress for t1 is written before call 2's result.   Fails on main.

Additional context

We are a research team at MPI-SWS building Daavar, a conformance checker for distributed protocol implementations,
which found this by comparing a model of the transport with one derived from the specification. A message counts
as sent once it is written to stdout. The proposed fix retains one limit: a message whose writer check read the
cancellation state before cancellation was published may still reach stdout. We would like to fix it; please
assign us. This issue and the fix were prepared with AI assistance and reviewed by us.

Reproduce with Daavar

The model has two calls with progress tokens, and the client may cancel call 1 at any point. A column names the
branch each tool handler takes: after one progress, (a) completes, (b) sends a second progress and completes, (c)
never completes. Each cell is
the number of observations the specification does not allow, out of all observations; the join prints it as
uncovered.

Tools aa ab ba ca cb
main 4 121/7 934 35 845/66 467 42 566/64 200 323/942 2 043/5 906
of which output produced after the cancellation 3 411 30 311 32 663 190 1 167

We have prepared a fix and will submit it as a pull request, together with its re-check.

git clone --recurse-submodules https://github.com/joulook/daavar.git && cd daavar
git checkout 01e3b484129e2cd5fd68deb09b716732cc4134b4 && git submodule update --init --recursive
(cd traceforge && cargo build --release -p traceforge --bin conf-join)
cd benchmarks/mcp-cancel/rust && cargo build --release
B=target/release/tf_mcp_cancel; J=../../../traceforge/target/release/conf-join
(cd ../../../jmc && ./gradlew -q --no-daemon --init-script ../scripts/print-classpath.gradle :core:printTestClasspath)
CP=$(cat ../../../jmc/cp.txt); mkdir -p classes r1 && javac -d classes -cp "$CP" ../java/bench/mcp_cancel/*.java
$B export spec --out spec.jsonl --model fifo -- spec,aa,s2
L="java -Xss4m -cp classes:$CP org.mpi_sws.jmc.mp.launch.MpLauncher"
$L bench.mcp_cancel.McpCancel:impl,aa must 0 1 "$PWD/r1" "$PWD/java.jsonl" 3600;      $J --decide spec.jsonl java.jsonl   # 4121

Run the hand model above first (1 h cap). It uses one fixed order of the transport's internal steps. The
thread-level model McpCancelThreaded keeps the SDK's threads and shared objects as processes; project_threaded.py drops its internal messages before the join. Its writer
runs independently of the reader, so it is joined against spec-nom10, the specification program with its own flush
restriction lifted. For aa: 59 661 observations after projection, 10.8 h on 16 workers, 100 GB heap, 21 GB
resident; the 12 h cap can cut the export short on a slower machine. With more interleavings come more forbidden
observations of the same kinds: 19 068 of 59 661 are uncovered under the reading above (4 121 by hand); by our
classification, 14 765 contain output produced after the cancellation and 8 853 output produced before it and
written after; some contain both (the join prints the total).

In the same shell:

$B export spec --out spec-s2.jsonl --program mcp-cancel --size spec_aa_nom10 --model fifo -- spec-nom10,aa,s2
mkdir -p thr
java -Xss4m -Xmx100g -cp "classes:$CP" org.mpi_sws.jmc.mp.launch.MpLauncher \
  bench.mcp_cancel.McpCancelThreaded:implt,aa must 0 1 "$PWD/thr" "$PWD/thr.jsonl" 43200 --split 16 --k 16
python3 ../java/scripts/project_threaded.py thr.jsonl projected.jsonl
$J --decide spec-s2.jsonl projected.jsonl   # violation, 19068 uncovered

Activity

  1. joulook commented on Dec 2, 2026

    @joulook
    Author

    A fix PR is ready on a fork branch; per CONTRIBUTING the issue needs to be assigned to the author before the PR. Could a maintainer assign it to me? Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions