Skip to content

fix: fail fast when session is not started - #2631

Closed
Epochex wants to merge 2 commits into
modelcontextprotocol:mainfrom
Epochex:fix/session-requires-context
Closed

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

Conversation

@Epochex

@Epochex Epochex commented May 18, 2026 •

Copy link
Copy Markdown

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

Epochex commented May 18, 2026

Copy link
Copy Markdown
Author

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.

@maxisbey

maxisbey commented Oct 5, 2026

Copy link
Copy Markdown
Contributor

Thanks for the PR, and sorry it sat here without a proper review. This has since landed via #2838.

We're closing most of the open PR backlog. v2 is out and changed a lot of the SDK, so many older PRs no longer apply as written, and we're a small team that realistically doesn't have the capacity to work through the rest.

If this still matters to you on v2, the most useful thing you can do is open an issue (or comment on the existing one) with your use case and a repro. Hearing why it matters to you is what we use to decide what to prioritise.

AI Disclaimer

@maxisbey maxisbey closed this Oct 5, 2026
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

2 participants