Skip to content

Conversation

@newbiehwang
Copy link

Fixes #1564

Summary

This PR adds a runtime check to ClientSession to ensure it is being used within an asynchronous context manager (async with). If initialize() is called without entering the context, it now raises a clear RuntimeError instead of hanging indefinitely.

Motivation and Context

Currently, if a user attempts to call await session.initialize() without wrapping the session in an async with block, the program hangs indefinitely. This happens because the background receive loop (_receive_loop) is only started inside __aenter__.

This behavior leads to a frustrating debugging experience (deadlock) for developers. This change provides immediate feedback by enforcing correct usage of the session, preventing the deadlock.

How Has This Been Tested?

I reproduced the issue using the script provided in #1564.

  1. Before fix: The script hung indefinitely at await session.initialize().
  2. After fix: The script immediately raises RuntimeError: ClientSession must be used within an 'async with' block., as expected.
  3. Verified that normal usage (with async with) continues to work correctly.

Breaking Changes

No. This change is backward compatible for all correct usages. It only affects code that was previously incorrect and hanging.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

Additional context

I implemented this using a simple state flag (_entered) to maintain backward compatibility without requiring significant architectural refactoring of the session class.

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.

stdio_client fails with BrokenResourceError during initialization

1 participant