Skip to content

MCP STDIO: answer the requests sent before a clean end of input (#1765) - #1767

Open
jlouvel wants to merge 3 commits into
3.0from
feature/1765-mcp-stdio-eof
Open

jlouvel wants to merge 3 commits into
3.0from
feature/1765-mcp-stdio-eof

Conversation

@jlouvel

@jlouvel jlouvel commented Nov 29, 2026

Copy link
Copy Markdown
Collaborator

Fixes #1765.

StdioServerHelper treated a clean end of its input as a failure: it called fail(new IOException("MCP STDIO input ended")), which logged a WARNING with a stack trace and aborted every request in flight. A request piped into the server, or a client that half-closes stdin after its last request, lost its answer.

  • On a clean end of input, the reader now calls endOfInput(): it waits, up to the adapter's request timeout, for the requests that hold a pending slot (both eras) to finish and write their answers, cancels the 2026-07-28 listen streams, which never end by themselves, as a notifications/cancelled would, and then closes. The wait stops at once if the connector is stopped or fails.
  • The end of the input is logged at FINE, and getFailure() still reports it, so owners that monitor a subprocess keep seeing it. An I/O error on the input still fails, aborts what is in flight and logs a WARNING.
  • Tests in McpModernStdioTestCase with a gated tool: a request in flight at the end of the input is answered, and the connector closes after it (fails without the fix); a listen stream is cancelled at the end of the input; an input error still aborts what is in flight and gets no answer.
  • changes.md entry under "Bugs fixed".

All 244 tests of org.restlet.ext.mcp pass and spotless:check is clean.

🤖 Generated with Claude Code

…uests sent are answered (#1765)

A clean end of input was a failure: it logged a warning and aborted the
requests in flight, so a request piped into the server lost its answer.
Now the connector waits for them within the request timeout, cancels the
listen streams, and closes quietly. An I/O error still aborts.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Signed-off-by: Jerome Louvel <374450+jlouvel@users.noreply.github.com>

@jlouvel jlouvel left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Thanks for the fix. I traced the races you listed and the core design holds: a slot is released only after the frame is written and flushed (writeFrame joins the writer thread and StreamTransport.write flushes, and done runs after dispatch/refuse return), the reader is the only admitter so nothing is admitted after endOfInput starts (and the listen registration in inFlight happens synchronously on that reader), the two-semaphore sum read is safe because both terms only decrease after the end of input, and stop() or a concurrent fail() simply ends the wait (a write error during the wait keeps its own cause in getFailure). Requests refused while waiting still hold their slot until the refusal is written. The deadline arithmetic is overflow-safe (requestTimeout is validated through toNanos in the adapter). Remaining points: (1) deadline vs timed-out request and the log level at expiry, (2) a listen stream that is not in inFlight is never cancelled, (3) getFailure cannot tell a clean end from a fault, (4) the 2025-11-25 era has no test, (5) the tests do not assert the headline behavior (log level, failure) and use sleep-based negative checks. Review assisted by Claude Code.

… it from a fault (#1765)

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Signed-off-by: Jerome Louvel <374450+jlouvel@users.noreply.github.com>

@jlouvel jlouvel left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Round 2. The six round 1 fixes are right: the wait is the request timeout plus the grace, cancelListens walks the requests set, the clean end is an EOFException, the fields are volatile, and the new tests (legacy era, duplicate id, no frame after the ack) are meaningful, with no timing race that can fail them (the only timed waits are negative 300 ms polls and 10 s awaits against a 35 s deadline). One point on the log-handler test below.

Review assisted by Claude Code.

…g, with a control (#1765)

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Signed-off-by: Jerome Louvel <374450+jlouvel@users.noreply.github.com>
@sonarqubecloud

Copy link
Copy Markdown

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.

1 participant