Skip to content

fix: fail fast when session is not started#2631

Open
Epochex wants to merge 4 commits into
modelcontextprotocol:mainfrom
Epochex:fix/session-requires-context
Open

fix: fail fast when session is not started#2631
Epochex wants to merge 4 commits into
modelcontextprotocol:mainfrom
Epochex:fix/session-requires-context

Conversation

@Epochex
Copy link
Copy Markdown

@Epochex Epochex commented May 18, 2026

When ClientSession/BaseSession is used without entering the async context manager, the receive loop never starts and initialize() can hang indefinitely waiting for a response.

This change fails fast: send_request() and send_notification() now raise a RuntimeError if the session hasn't been started via async with ... as session.

Fixes #1452.

Tests:

  • uv run --frozen python -m ruff check src/mcp/shared/session.py tests/client/test_session.py
  • uv run --frozen python -m ruff format src/mcp/shared/session.py tests/client/test_session.py
  • uv run --frozen pytest tests/client/test_session.py -q

@Epochex
Copy link
Copy Markdown
Author

Epochex commented May 18, 2026

Follow-up for CI: Python 3.14 was still failing branch coverage due to a missing arc reported on the pytest.raises(...) context manager line in ests/client/test_resource_cleanup.py. Added # pragma: no branch there to make branch coverage stable across 3.14. Pushed commit abbbc01.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

The stdio_client hangs indefinitely on session initialization

1 participant