Skip to content

Commit 513ccc3

Browse files
committed
chore: drop write-only _InFlight.cancelled_by_peer; add assertion to notify-drop test
cancelled_by_peer was set by _dispatch_notification but never read; the peer-vs-outer-cancel distinction in _handle_request relies on scope.cancel_called alone (and works because nothing else cancels the per-request scope). test_runner_on_notify_drops_before_init_and_unknown_methods now registers a handler and asserts only the post-init notification reaches it (was assertionless before).
1 parent 50134cb commit 513ccc3

2 files changed

Lines changed: 9 additions & 3 deletions

File tree

src/mcp/shared/jsonrpc_dispatcher.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@ class _InFlight(Generic[TransportT]):
102102

103103
scope: anyio.CancelScope
104104
dctx: _JSONRPCDispatchContext[TransportT]
105-
cancelled_by_peer: bool = False
106105

107106

108107
@dataclass
@@ -489,7 +488,6 @@ def _dispatch_notification(
489488
if msg.method == "notifications/cancelled":
490489
match msg.params:
491490
case {"requestId": str() | int() as rid} if (in_flight := self._in_flight.get(rid)) is not None:
492-
in_flight.cancelled_by_peer = True
493491
in_flight.dctx.cancel_requested.set()
494492
if self._peer_cancel_mode == "interrupt":
495493
in_flight.scope.cancel()

tests/server/test_runner.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,11 +255,19 @@ async def on_level(ctx: Ctx, params: SetLevelRequestParams) -> None:
255255

256256
@pytest.mark.anyio
257257
async def test_runner_on_notify_drops_before_init_and_unknown_methods(server: SrvT):
258+
seen: list[Any] = []
259+
260+
async def on_roots(ctx: Ctx, params: NotificationParams | None) -> None:
261+
seen.append(params)
262+
263+
server.add_notification_handler("notifications/roots/list_changed", NotificationParams, on_roots)
258264
async with connected_runner(server, initialized=False) as (client, _):
259265
await client.notify("notifications/roots/list_changed", None) # before init: dropped
260266
await client.notify("notifications/initialized", None)
261267
await client.notify("notifications/unknown", None) # no handler: dropped
262-
# No exception raised; both drops are silent.
268+
await client.notify("notifications/roots/list_changed", None) # post-init: delivered
269+
await anyio.wait_all_tasks_blocked()
270+
assert seen == [None] # only the post-init one reached the handler
263271

264272

265273
@pytest.mark.anyio

0 commit comments

Comments
 (0)