Skip to content

Commit 1af03f8

Browse files
wiggzzclaude
andcommitted
Use lax no cover for timing-dependent exception paths; fix ruff format
Change 5 pragma annotations from strict "no cover" to "lax no cover" since these exception handlers may or may not fire depending on cancellation timing. Fix ruff-format in test file. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 2e0e00b commit 1af03f8

File tree

4 files changed

+8
-15
lines changed

4 files changed

+8
-15
lines changed

src/mcp/server/streamable_http.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -626,14 +626,14 @@ async def sse_writer(): # pragma: lax no cover
626626
# Then send the message to be processed by the server
627627
session_message = self._create_session_message(message, request, request_id, protocol_version)
628628
await writer.send(session_message)
629-
except Exception: # pragma: no cover
629+
except Exception: # pragma: lax no cover
630630
logger.exception("SSE response error")
631631
await sse_stream_writer.aclose()
632632
await self._clean_up_memory_streams(request_id)
633633
finally:
634634
await sse_stream_reader.aclose()
635635

636-
except Exception as err: # pragma: no cover
636+
except Exception as err: # pragma: lax no cover
637637
logger.exception("Error handling POST request")
638638
response = self._create_error_response(
639639
f"Error handling POST request: {err}",
@@ -816,7 +816,7 @@ async def _validate_request_headers(self, request: Request, send: Send) -> bool:
816816

817817
async def _validate_session(self, request: Request, send: Send) -> bool:
818818
"""Validate the session ID in the request."""
819-
if not self.mcp_session_id: # pragma: no cover
819+
if not self.mcp_session_id: # pragma: lax no cover
820820
# If we're not using session IDs, return True
821821
return True
822822

src/mcp/server/streamable_http_manager.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ async def run_stateless_server(*, task_status: TaskStatus[None] = anyio.TASK_STA
178178
self.app.create_initialization_options(),
179179
stateless=True,
180180
)
181-
except Exception: # pragma: no cover
181+
except Exception: # pragma: lax no cover
182182
logger.exception("Stateless session crashed")
183183

184184
# Use a request-scoped task group instead of the global one.

src/mcp/shared/session.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ async def _receive_loop(self) -> None:
423423
try:
424424
await stream.send(JSONRPCError(jsonrpc="2.0", id=id, error=error))
425425
await stream.aclose()
426-
except Exception: # pragma: no cover
426+
except Exception: # pragma: lax no cover
427427
# Stream might already be closed
428428
pass
429429
self._response_streams.clear()

tests/server/test_streamable_http_manager.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -312,12 +312,8 @@ async def handle_call_tool(ctx: ServerRequestContext, params: Any) -> CallToolRe
312312
session_manager = app._session_manager
313313

314314
async def make_and_abandon_tool_call():
315-
async with httpx.AsyncClient(
316-
transport=transport, base_url=f"http://{host}", timeout=30.0
317-
) as http_client:
318-
async with Client(
319-
streamable_http_client(f"http://{host}/mcp", http_client=http_client)
320-
) as client:
315+
async with httpx.AsyncClient(transport=transport, base_url=f"http://{host}", timeout=30.0) as http_client:
316+
async with Client(streamable_http_client(f"http://{host}/mcp", http_client=http_client)) as client:
321317
# Start tool call — this will block until tool completes
322318
# We'll cancel it from outside to simulate disconnect
323319
await client.call_tool("slow_tool", {})
@@ -343,10 +339,7 @@ async def make_and_abandon_tool_call():
343339
await anyio.sleep(0.1)
344340
leaked = len(session_manager._task_group._tasks)
345341

346-
assert leaked == 0, (
347-
f"Expected 0 lingering tasks but found {leaked}. "
348-
f"Stateless request tasks are leaking after client disconnect."
349-
)
342+
assert leaked == 0, f"Expected 0 lingering tasks but found {leaked}. Stateless request tasks are leaking after client disconnect."
350343

351344

352345
@pytest.mark.anyio

0 commit comments

Comments
 (0)