Close streams outside mutex in session cleanup#291
Merged
koic merged 1 commit intomodelcontextprotocol:mainfrom Apr 3, 2026
Merged
Close streams outside mutex in session cleanup#291koic merged 1 commit intomodelcontextprotocol:mainfrom
koic merged 1 commit intomodelcontextprotocol:mainfrom
Conversation
## Motivation and Context `cleanup_session_unsafe` was closing streams inside the mutex, which could block all session operations if `stream.close` is slow or blocks (e.g., waiting for network I/O). The original `reap_expired_sessions` avoided this by closing streams outside the mutex, but the other 6 call sites of `cleanup_session_unsafe` did not, creating an inconsistency. This change separates stream closing from session deletion. `cleanup_session_unsafe` now only removes the session from `@sessions` and returns it. All callers close streams outside the mutex via `close_stream_safely`, ensuring consistent behavior and preventing mutex contention from blocking I/O. ## How Has This Been Tested? Added tests that verify streams are closed outside the mutex by using `Mutex#try_lock` inside mock stream close callbacks: - `reap_expired_sessions closes stream outside mutex` - `send_notification closes stream outside mutex on write error` ## Breaking Changes None. This is an internal improvement with no change to public API behavior.
atesgoral
approved these changes
Apr 3, 2026
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
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.
Motivation and Context
cleanup_session_unsafewas closing streams inside the mutex, which could block all session operations ifstream.closeis slow or blocks (e.g., waiting for network I/O). The originalreap_expired_sessionsavoided this by closing streams outside the mutex, but the other 6 call sites ofcleanup_session_unsafedid not, creating an inconsistency.This change separates stream closing from session deletion.
cleanup_session_unsafenow only removes the session from@sessionsand returns it. All callers close streams outside the mutex viaclose_stream_safely, ensuring consistent behavior and preventing mutex contention from blocking I/O.How Has This Been Tested?
Added tests that verify streams are closed outside the mutex by using
Mutex#try_lockinside mock stream close callbacks:reap_expired_sessions closes stream outside mutexsend_notification closes stream outside mutex on write errorBreaking Changes
None. This is an internal improvement with no change to public API behavior.
Types of changes
Checklist