Skip to content
Prev Previous commit
Next Next commit
ruff
  • Loading branch information
soby authored and felixweinberger committed Jul 8, 2025
commit 615336a5bf4b6018506c2617d7c21b55d721de84
10 changes: 3 additions & 7 deletions src/mcp/server/streamable_http_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,21 +251,17 @@ async def run_server(*, task_status: TaskStatus[None] = anyio.TASK_STATUS_IGNORE
# Only remove from instances if not terminated
if (
http_transport.mcp_session_id
and http_transport.mcp_session_id
in self._server_instances
and http_transport.mcp_session_id in self._server_instances
and not (
hasattr(http_transport, "_terminated")
and http_transport._terminated # pyright: ignore
hasattr(http_transport, "_terminated") and http_transport._terminated # pyright: ignore
Comment thread
felixweinberger marked this conversation as resolved.
Outdated
)
):
logger.info(
"Cleaning up crashed session "
f"{http_transport.mcp_session_id} from "
"active instances."
)
del self._server_instances[
http_transport.mcp_session_id
]
del self._server_instances[http_transport.mcp_session_id]

# Assert task group is not None for type checking
assert self._task_group is not None
Expand Down
24 changes: 9 additions & 15 deletions tests/server/test_streamable_http_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,7 @@ async def send(message):
with pytest.raises(RuntimeError) as excinfo:
await manager.handle_request(scope, receive, send)

assert "Task group is not initialized. Make sure to use run()." in str(
excinfo.value
)
assert "Task group is not initialized. Make sure to use run()." in str(excinfo.value)


class TestException(Exception):
Expand Down Expand Up @@ -137,12 +135,10 @@ async def mock_receive():
# Give other tasks a chance to run. This is important for the finally block.
await anyio.sleep(0.01)

assert (
session_id not in manager._server_instances
), "Session ID should be removed from _server_instances after graceful exit"
assert (
not manager._server_instances
), "No sessions should be tracked after the only session exits gracefully"
assert session_id not in manager._server_instances, (
"Session ID should be removed from _server_instances after graceful exit"
)
assert not manager._server_instances, "No sessions should be tracked after the only session exits gracefully"


@pytest.mark.anyio
Expand Down Expand Up @@ -195,9 +191,7 @@ async def mock_receive():
# Give other tasks a chance to run to ensure the finally block executes
await anyio.sleep(0.01)

assert (
session_id not in manager._server_instances
), "Session ID should be removed from _server_instances after an exception"
assert (
not manager._server_instances
), "No sessions should be tracked after the only session crashes"
assert session_id not in manager._server_instances, (
"Session ID should be removed from _server_instances after an exception"
)
assert not manager._server_instances, "No sessions should be tracked after the only session crashes"