Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/mcp/shared/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,11 @@ def __exit__(
self._entered = False
if not self._cancel_scope: # pragma: no cover
raise RuntimeError("No active cancel scope")
self._cancel_scope.__exit__(exc_type, exc_val, exc_tb)
try:
self._cancel_scope.__exit__(exc_type, exc_val, exc_tb)
except BaseException as exc:
if not (self._completed and isinstance(exc, anyio.get_cancelled_exc_class())):
raise

async def respond(self, response: SendResultT | ErrorData) -> None:
"""Send a response for this request.
Expand Down
52 changes: 52 additions & 0 deletions tests/shared/test_session.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Any, cast

import anyio
import pytest

Expand All @@ -23,6 +25,18 @@
)


class _CancelScopeThatRaisesOnExit:
cancel_called = True

def __exit__(
self,
exc_type: type[BaseException] | None,
exc_val: BaseException | None,
exc_tb: object | None,
) -> None:
raise anyio.get_cancelled_exc_class()()


@pytest.mark.anyio
async def test_in_flight_requests_cleared_after_completion():
"""Verify that _in_flight is empty after all requests complete."""
Expand Down Expand Up @@ -98,6 +112,44 @@ async def make_request(client: Client):
await ev_cancelled.wait()


@pytest.mark.anyio
async def test_completed_request_responder_suppresses_cancel_scope_exit() -> None:
completed: list[Any] = []
responder = RequestResponder(
request_id=1,
request_meta=None,
request=types.PingRequest(),
session=cast(Any, object()),
on_complete=completed.append,
)
responder._completed = True # type: ignore[reportPrivateUsage]
responder._cancel_scope = cast( # type: ignore[reportPrivateUsage]
anyio.CancelScope, _CancelScopeThatRaisesOnExit()
)

responder.__exit__(None, None, None)

assert completed == [responder]
assert not responder._entered # type: ignore[reportPrivateUsage]


@pytest.mark.anyio
async def test_incomplete_request_responder_propagates_cancel_scope_exit() -> None:
responder = RequestResponder(
request_id=1,
request_meta=None,
request=types.PingRequest(),
session=cast(Any, object()),
on_complete=lambda _: None,
)
responder._cancel_scope = cast( # type: ignore[reportPrivateUsage]
anyio.CancelScope, _CancelScopeThatRaisesOnExit()
)

with pytest.raises(anyio.get_cancelled_exc_class()):
responder.__exit__(None, None, None)


@pytest.mark.anyio
async def test_response_id_type_mismatch_string_to_int():
"""Test that responses with string IDs are correctly matched to requests sent with
Expand Down
Loading