Repository navigation
Conversation
## Motivation and Context A stateful Streamable HTTP request checks its session in one `@mutex` section (`validate_and_touch_session`) and looks the session's `ServerSession` up in another. A `DELETE` from another connection, or `close`, can remove the session between the two. The lookup then finds nothing, and `dispatch_handle_json` falls back to the shared `Server`: the tool runs with no session, so the termination that cancels the session's requests (modelcontextprotocol#692, modelcontextprotocol#698) never reaches it, `server_context.cancelled?` stays false, and the request is answered 200 with its result in both response modes. Notifications have the same split between `session_active?` and the lookup in `dispatch_notification`, so a custom notification handler runs against the shared `Server` and the POST is answered 202. A request or notification that names a session and finds none at the lookup is now answered 404 Session not found, the answer it gets when the session is already gone at the check, and is not dispatched. Stateless requests and requests sent without a session id are unchanged. ## How Has This Been Tested? New tests in `test/mcp/server/transports/streamable_http_transport_test.rb` hold a request with a `Queue` barrier right after its session check passes, terminate the session with `DELETE`, then release it: a `tools/call` in SSE mode and in JSON response mode is answered 404 and does not run its tool, and a custom notification is answered 404 and does not run its handler. All three fail against the previous library. Dropping the request guard, the notification guard, or the caller's use of the notification result each fails a test. ## Breaking Changes None. A request that loses its session while being admitted is answered 404 instead of running without the session. Fixes modelcontextprotocol#704.
This branch has not been deployed
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.
Follows #698: a request whose session is removed between the transport's session check and its session lookup is answered 404 instead of running against the shared
Server.Fixes #704
Motivation and Context
A stateful Streamable HTTP request checks its session in one
@mutexsection (validate_and_touch_session) and looks the session'sServerSessionup in another (lib/mcp/server/transports/streamable_http_transport.rb:2481). ADELETEfrom another connection, orclose, can remove the session between the two. The lookup then finds nothing, anddispatch_handle_jsonfalls back to the sharedServer(:2601): the tool runs with no session, so the termination that cancels the session's requests (#692, #698) never reaches it,server_context.cancelled?stays false, and the request is answered 200 with its result in both response modes. Notifications have the same split betweensession_active?(:1948) and the lookup indispatch_notification, so a custom notification handler runs against the sharedServerand the POST is answered 202.On
main(00868f7), holding the request with aQueuebarrier just after its session check and sendingDELETEbefore releasing it:A request or notification that names a session and finds none at the lookup is now answered 404 Session not found, the answer it gets when the session is already gone at the check, and is not dispatched. Stateless requests and requests sent without a session id (such as a sessionless
server/discover) still go to the sharedServeras before. The ownership check, which runs before both steps, is unchanged.How Has This Been Tested?
New tests in
test/mcp/server/transports/streamable_http_transport_test.rbhold a request with aQueuebarrier right after its session check passes, terminate the session withDELETE, then release it:tools/callin SSE mode is answered 404 and does not run its tool.All three fail against the previous library (200, 200, 202). Dropping the request guard, the notification guard, or the caller's use of the notification result each fails a test.
bundle exec rake test,bundle exec rake rubocopandbundle exec rake conformance:test(baseline) pass locally on Ruby 4.0.9.Breaking Changes
None. A request that loses its session while it is admitted is answered 404 instead of running without the session.
Types of changes
Checklist
Additional context
dispatch_notificationnow returns whether it dispatched, so its caller can answer 404; an exception inside the dispatch is still reported and answered 202 as before. I kept the check and the lookup as two steps rather than mergingvalidate_and_touch_sessionwith the lookup, because the GET path shares that helper and only needs the check.