Skip to content

MCP: read the disconnect signal through a copy, so no UnsupportedOperationException is dropped (#1764) - #1766

Open
jlouvel wants to merge 2 commits into
3.0from
feature/1764-mcp-disconnect-signal
Open

jlouvel wants to merge 2 commits into
3.0from
feature/1764-mcp-disconnect-signal

Conversation

@jlouvel

@jlouvel jlouvel commented Nov 29, 2026

Copy link
Copy Markdown
Collaborator

Fixes #1764.

Every request the MCP adapter serves logged an ERROR from Reactor, Operator called default onErrorDropped, with an UnsupportedOperationException. Request.getDisconnectSignal() returns a minimal stage, whose cancel throws; McpServerAdapter.admit and McpModernExchange turned it into a Mono with Mono.fromCompletionStage and raced it against the exchange, so when the exchange won, Reactor cancelled the stage.

  • McpMessages.disconnected(Request) builds the Mono from request.getDisconnectSignal().toCompletableFuture(), a copy that can be cancelled and does not complete the signal. Both call sites use it.
  • Two tests (the legacy path through admit and the modern exchange) install Hooks.onErrorDropped and assert nothing is dropped after a served request; both fail without the fix with the UnsupportedOperationException.
  • changes.md entry under "Bugs fixed".

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

🤖 Generated with Claude Code

The signal is a minimal stage, whose cancel throws: Reactor cancelled it
when the exchange won the race against it, and dropped an
UnsupportedOperationException on every request.

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.

Verified: the fix is correct and covers the whole repository.

  • minimalCompletionStage().toCompletableFuture() returns a new, independent CompletableFuture (it does not throw, unlike cancel on the minimal stage). It is linked to the source by a relay, so it completes with the cause when the request disconnects, and a copy taken after the disconnect is already completed. The supplier overload of Mono.fromCompletionStage takes the copy at subscription time, so a late subscription is covered too.
  • Cancelling the copy only cancels the copy: the relay does not propagate back, so the request's own signal is never completed or cancelled (the same property RequestLifecycleTestCase already relies on for new Request(request)).
  • The other getDisconnectSignal() uses (RestletClientStream, RestletServerHost, SmithyServerHost, NettyClientHelper, McpLegacySessionHost in tests) only call thenAccept, never wrap the stage in a Mono, so none of them can cancel it. The two MCP call sites are the only ones to fix.
  • The changes.md entry is in the right section (first version, Bugs fixed) and follows the style.

Two optional points inline.

Review assisted by Claude Code.

…rror filter (#1764)

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.

Re-review of a3c1965: clean, no new findings.

  • The mono is built from the copy returned by toCompletableFuture() on the minimal stage, so Reactor's cancel acts on the copy and no longer reaches the throwing stage; the supplier form defers it to subscription, and the other getDisconnectSignal callers in the repository only chain thenAccept and never cancel.
  • McpDisconnectedTestCase is meaningful: it checks that the cause reaches an early and a late subscriber, and that cancelling leaves the request's own signal incomplete.
  • Both regression tests now record only UnsupportedOperationException in the global hook and reset it in a finally block.

Review assisted by Claude Code.

@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