-
Notifications
You must be signed in to change notification settings - Fork 3k
Fix: Raise RuntimeError when ClientSession is used without context manager to prevent hangs #1849
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
newbiehwang
wants to merge
16
commits into
modelcontextprotocol:main
Choose a base branch
from
newbiehwang:fix/prevent-session-hang
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Fix: Raise RuntimeError when ClientSession is used without context manager to prevent hangs #1849
newbiehwang
wants to merge
16
commits into
modelcontextprotocol:main
from
newbiehwang:fix/prevent-session-hang
+39
−1
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6cb555e to
ba178a1
Compare
94cd3b6 to
f3b85aa
Compare
fa8e9fc to
72e4fd7
Compare
1d3f8ed to
a972192
Compare
9068f71 to
17ba89f
Compare
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #1564
Summary
This PR adds a runtime check to
ClientSessionto ensure it is being used within an asynchronous context manager (async with). Ifinitialize()is called without entering the context, it now raises a clearRuntimeErrorinstead of hanging indefinitely.Motivation and Context
Currently, if a user attempts to call
await session.initialize()without wrapping the session in anasync withblock, 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.
await session.initialize().RuntimeError: ClientSession must be used within an 'async with' block., as expected.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
Checklist
Additional context
I implemented this using a simple state flag (
_entered) to maintain backward compatibility without requiring significant architectural refactoring of the session class.