Repository navigation
fix(streamable-http-server): release the session map lock before waiting on a session #1323
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
Merged
DaleSeo
merged 3 commits into
modelcontextprotocol:main
from
TheSeydiCharyyev:fix/session-map-lock
Oct 11, 2026
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
1de7ee4
fix(streamable-http-server): release the session map lock before wait…
TheSeydiCharyyev 763b2c4
fix(streamable-http-server): make the isolation test deterministic an…
TheSeydiCharyyev 9fb54df
test(streamable-http-server): cover close_session during a pending in…
TheSeydiCharyyev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
118 changes: 118 additions & 0 deletions
118
crates/rmcp/tests/test_streamable_http_session_isolation.rs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| #![cfg(all(feature = "transport-streamable-http-server", not(feature = "local")))] | ||
|
|
||
| use std::{sync::Arc, time::Duration}; | ||
|
|
||
| use rmcp::{ | ||
| model::{ClientJsonRpcMessage, ClientRequest, PingRequest, RequestId}, | ||
| transport::streamable_http_server::session::{ | ||
| SessionId, SessionManager, | ||
| local::{LocalSessionManager, SessionConfig}, | ||
| }, | ||
| }; | ||
| use rstest::rstest; | ||
| use tokio::task::JoinHandle; | ||
|
|
||
| const PROBE_TIMEOUT: Duration = Duration::from_secs(1); | ||
|
|
||
| #[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
| enum StuckCall { | ||
| Initialize, | ||
| CreateStream, | ||
| CreateStandaloneStream, | ||
| Resume, | ||
| AcceptMessage, | ||
| } | ||
|
|
||
| fn ping() -> ClientJsonRpcMessage { | ||
| ClientJsonRpcMessage::request( | ||
| ClientRequest::PingRequest(PingRequest::default()), | ||
| RequestId::Number(1), | ||
| ) | ||
| } | ||
|
|
||
| fn spawn_call( | ||
| manager: &Arc<LocalSessionManager>, | ||
| id: &SessionId, | ||
| call: StuckCall, | ||
| ) -> JoinHandle<()> { | ||
| let manager = manager.clone(); | ||
| let id = id.clone(); | ||
| tokio::spawn(async move { | ||
| match call { | ||
| StuckCall::Initialize => { | ||
| let _ = manager.initialize_session(&id, ping()).await; | ||
| } | ||
| StuckCall::CreateStream => { | ||
| let _ = manager.create_stream(&id, ping()).await; | ||
| } | ||
| StuckCall::CreateStandaloneStream => { | ||
| let _ = manager.create_standalone_stream(&id).await; | ||
| } | ||
| StuckCall::Resume => { | ||
| let _ = manager.resume(&id, "0".to_owned()).await; | ||
| } | ||
| StuckCall::AcceptMessage => { | ||
| // The worker no longer reads its event channel, so the push | ||
| // after the channel is full waits. | ||
| for _ in 0..=SessionConfig::DEFAULT_CHANNEL_CAPACITY { | ||
|
DaleSeo marked this conversation as resolved.
Resolved
|
||
| let _ = manager.accept_message(&id, ping()).await; | ||
| } | ||
| } | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| /// While a call waits on the worker of one session, the manager must still | ||
| /// create new sessions and look up other sessions. | ||
| #[rstest] | ||
| #[case::initialize(StuckCall::Initialize)] | ||
| #[case::create_stream(StuckCall::CreateStream)] | ||
| #[case::create_standalone_stream(StuckCall::CreateStandaloneStream)] | ||
| #[case::resume(StuckCall::Resume)] | ||
| #[case::accept_message(StuckCall::AcceptMessage)] | ||
| #[tokio::test] | ||
| async fn waiting_on_one_session_does_not_block_other_sessions( | ||
|
DaleSeo marked this conversation as resolved.
Resolved
|
||
| #[case] call: StuckCall, | ||
| ) -> anyhow::Result<()> { | ||
| let manager = Arc::new(LocalSessionManager::default()); | ||
| let (other_id, _other_transport) = manager.create_session().await?; | ||
| // Nothing serves this transport, so the worker never answers initialize | ||
| // and stops reading session events. Keep the transport bound: dropping it | ||
| // cancels the worker. | ||
| let (stuck_id, _stuck_transport) = manager.create_session().await?; | ||
|
|
||
| let mut stuck = vec![spawn_call(&manager, &stuck_id, StuckCall::Initialize)]; | ||
| if call != StuckCall::Initialize { | ||
| tokio::time::sleep(Duration::from_millis(50)).await; | ||
| stuck.push(spawn_call(&manager, &stuck_id, call)); | ||
| } | ||
| tokio::time::sleep(Duration::from_millis(100)).await; | ||
|
|
||
| // A new client connects while the call is waiting... | ||
| let create = tokio::spawn({ | ||
| let manager = manager.clone(); | ||
| async move { manager.create_session().await.map(|(id, _)| id) } | ||
| }); | ||
| tokio::time::sleep(Duration::from_millis(100)).await; | ||
|
DaleSeo marked this conversation as resolved.
Resolved
|
||
|
|
||
| // ...and a request arrives on another session. | ||
| let lookup = tokio::time::timeout(PROBE_TIMEOUT, manager.has_session(&other_id)).await; | ||
| assert!( | ||
| matches!(lookup, Ok(Ok(true))), | ||
| "has_session blocked: {lookup:?}" | ||
| ); | ||
| let created = tokio::time::timeout(PROBE_TIMEOUT, create).await; | ||
| assert!( | ||
| matches!(created, Ok(Ok(Ok(_)))), | ||
| "create_session blocked: {created:?}" | ||
| ); | ||
|
|
||
| for task in stuck { | ||
| assert!( | ||
| !task.is_finished(), | ||
| "the call on the stuck session should still be waiting" | ||
| ); | ||
| task.abort(); | ||
| } | ||
| Ok(()) | ||
| } | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.