Repository navigation
Conversation
Drop a suspended handshake-era handler fiber when the client sends notifications/cancelled, and do not answer the cancelled request. Unknown, already-finished and un-cancellable ids are ignored.
There was a problem hiding this comment.
🟡 Changes recommended
Unresolved cancellation flaws can bypass session isolation and overwrite concurrent workers’ session updates.
5 open issues
Scope fiber lookup and cancellation cache by session ID · New Lock session reload, cleanup, and save in polling callback · New Clear in-flight markers before saving the resumed session · New Cache cancellation only for locally owned fibers · New Retain fiber identity and cancellation state through transport checks · New
What changed in this PR
Adds handshake-era request cancellation to the PHP SDK server, addressing #732 by dropping suspended handlers without sending a response.
Changes:
- Tracks in-flight requests and cancellation state in the protocol.
- Adds a transport cancellation callback and checks in stdio and HTTP polling loops.
- Adds regression tests and documents the transport interface breaking change.
| File | Description |
|---|---|
tests/Unit/Server/Transport/StreamableHttpTransportTest.php |
Tests cancellation without an SSE response. |
tests/Unit/Server/Transport/StdioSuspendedFibersTest.php |
Tests cancelled elicitation and unknown cancellation IDs. |
tests/Unit/Server/ProtocolTest.php |
Covers cancellation handling, validation and typed request IDs. |
tests/Unit/Fixtures/PollingLoopTransport.php |
Exposes cancellation-aware polling for protocol tests. |
src/Server/Transport/TransportInterface.php |
Declares the cancellation checker contract. |
src/Server/Transport/StreamableHttpTransport.php |
Checks cancellation during SSE processing. |
src/Server/Transport/StdioTransport.php |
Drops cancelled fibers during polling. |
src/Server/Transport/ManagesTransportCallbacks.php |
Stores and registers the cancellation checker. |
src/Server/Transport/BaseTransport.php |
Provides shared cancellation callback invocation. |
src/Server/Protocol.php |
Tracks suspended requests and processes cancellation notifications. |
CHANGELOG.md |
Records cancellation support and the interface breaking change. |
Review effort: Balanced
| $fiber = $this->fiberForIncomingRequest($requestId); | ||
| if (null === $fiber && !$this->hasInFlightIncomingRequest($session, $key)) { |
| if (!$session instanceof EphemeralSessionInterface) { | ||
| try { | ||
| $session->save(); |
| $saveResumedSession($session); | ||
| $clearIncomingRequest($request->getId(), $session); |
|
|
||
| $this->logger->info('Cancelled an in-flight request.', ['request_id' => $requestId]); | ||
|
|
||
| $this->cancelledRequestKeys[$key] = true; |
| foreach ($this->incomingRequestIds as $fiber => $id) { | ||
| if ($id === $requestId) { | ||
| unset($this->incomingRequestIds[$fiber]); |
…ion lock A shared Protocol serves several sessions that may send the same request id, and another worker may hold the suspended fiber. The cancellation bookkeeping used the request id alone and reloaded the session outside its lock, so a token of this change could drop another session's call or overwrite a concurrent save. - Match suspended fibers and the cached cancellation by session id *and* request id, so a cancellation authenticated for one session cannot drop another's handler. - Cache a cancellation in this process only for a fiber it owns; another worker's fiber is dropped through the marker stored in the session. - Reload and clean up a cancelled request under the session lock. The handler is dropped even when the lock is busy, and the markers are cleared when a later request of that session reuses the id. - Clear the in-flight marker before the resumed request's locked save, so storage does not keep a request that was already answered. - Keep the fiber's identity and cancellation state until its transport checked whether a response may be sent, so a cancellation recorded while the resumed handler finishes still suppresses that response.
|
Pushed
New Verification is in the PR body: |


Summary
Handle
notifications/cancelledon handshake-era servers by dropping the suspended handler fiber (for example one waiting on elicitation or sampling) and not answering the cancelled request. Unknown, already-finished and un-cancellable ids are ignored. 2026-07-28 stdio cancellation ofsubscriptions/listenis unchanged.TransportInterface::setCancellationChecker()is new: transports that extendBaseTransportget it automatically; a custom implementor must add the setter and drop a cancelled fiber from the polling loop without sending a response.On review feedback, the bookkeeping was reworked so a shared
Protocolcannot mix up sessions or workers:_mcp.cancelled_requestsin the session instead._mcp.in_flight_requestsbefore its locked save, so storage does not keep an answered request in flight.Fixes #732
Verification
Head
7b52dafee(includes a merge ofmainat3d2e831).vendor/bin/phpunit tests/Unit/Server— 1766 tests, 5101 assertions, 1 failure:StdioTransportTest::testIdleInputWakesOnArrival, a transport latency budget (45 ms) that this machine misses at 56–90 ms.tests/Unit/Server/Transport/StdioTransport.phpis untouched by this change; the test also fails intermittently on reruns.ProtocolTest+ProtocolSessionLockTest+StdioSuspendedFibersTest(131 tests) andStreamableHttpTransportTest(45 tests) all pass.make csandmake phpstanare clean;make deptracreports no violations.ProtocolTestcases cover the rework: a cancellation in one session leaves another session's request with the same id alone, a foreign cancellation does not cancel a later request that reuses the id, a resumed request leaves no in-flight mark in storage, and a cancellation recorded during the resume suppresses the response. The first, third and fourth fail on the previous head.tests/Integrationcannot run here: the fixture servers are spawned with a path containing spaces, so they exit with code 126 in this workspace. Unit, static analysis and style cover this diff; CI runs the integration, interop and conformance suites.