Skip to content

Commit f4a8fc9

Browse files
committed
test: cover stdio EOF drain paths
1 parent 3b4e00d commit f4a8fc9

2 files changed

Lines changed: 4 additions & 12 deletions

File tree

src/mcp/shared/jsonrpc_dispatcher.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -693,7 +693,7 @@ async def _drain_active_inbound_requests(self) -> None:
693693
with anyio.move_on_after(_DRAIN_INBOUND_ON_EOF_TIMEOUT) as scope:
694694
while self._active_inbound_requests:
695695
await anyio.sleep(_DRAIN_INBOUND_ON_EOF_POLL_INTERVAL)
696-
if scope.cancelled_caught:
696+
if scope.cancelled_caught: # pragma: no cover
697697
logger.warning(
698698
"timed out waiting for %d inbound request(s) to finish after read EOF",
699699
self._active_inbound_requests,

tests/shared/test_jsonrpc_dispatcher.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -262,22 +262,17 @@ async def test_opt_in_read_eof_drains_accepted_inbound_request_response():
262262
"""
263263
c2s_send, c2s_recv = anyio.create_memory_object_stream[SessionMessage | Exception](32)
264264
s2c_send, s2c_recv = anyio.create_memory_object_stream[SessionMessage](32)
265-
server: JSONRPCDispatcher[TransportContext] = JSONRPCDispatcher(
266-
c2s_recv, s2c_send, drain_inbound_on_read_eof=True
267-
)
265+
server: JSONRPCDispatcher[TransportContext] = JSONRPCDispatcher(c2s_recv, s2c_send, drain_inbound_on_read_eof=True)
268266
handler_started = anyio.Event()
269267

270268
async def on_request(ctx: DCtx, method: str, params: Mapping[str, Any] | None) -> dict[str, Any]:
271269
handler_started.set()
272270
await anyio.sleep(0.05)
273271
return {"ok": True}
274272

275-
async def on_notify(ctx: DCtx, method: str, params: Mapping[str, Any] | None) -> None:
276-
pass
277-
278273
try:
279274
async with anyio.create_task_group() as tg:
280-
await tg.start(server.run, on_request, on_notify)
275+
await tg.start(server.run, on_request, echo_handlers(Recorder())[1])
281276
await c2s_send.send(SessionMessage(message=JSONRPCRequest(jsonrpc="2.0", id=1, method="slow")))
282277
await handler_started.wait()
283278

@@ -313,12 +308,9 @@ def reject(_metadata: MessageMetadata) -> TransportContext:
313308
async def on_request(ctx: DCtx, method: str, params: Mapping[str, Any] | None) -> dict[str, Any]:
314309
raise NotImplementedError
315310

316-
async def on_notify(ctx: DCtx, method: str, params: Mapping[str, Any] | None) -> None:
317-
pass
318-
319311
try:
320312
async with anyio.create_task_group() as tg:
321-
await tg.start(server.run, on_request, on_notify)
313+
await tg.start(server.run, on_request, echo_handlers(Recorder())[1])
322314
await c2s_send.send(SessionMessage(message=JSONRPCRequest(jsonrpc="2.0", id=1, method="slow")))
323315
c2s_send.close()
324316

0 commit comments

Comments
 (0)