Title: fix: dispatch client request handlers concurrently (#2489) by demoray · Pull Request #2490 · modelcontextprotocol/python-sdk · GitHub
Open Graph Title: fix: dispatch client request handlers concurrently (#2489) by demoray · Pull Request #2490 · modelcontextprotocol/python-sdk
X Title: fix: dispatch client request handlers concurrently (#2489) by demoray · Pull Request #2490 · modelcontextprotocol/python-sdk
Description: Closes #2489. Problem BaseSession._receive_loop awaited each incoming request handler inline, so concurrent server→client requests (e.g. N create_message sampling callbacks launched via asyncio.gather) ran strictly one-at-a-time. Peak in-flight handlers = 1 regardless of fan-out. Fix Add an opt-in _dispatch_requests_concurrently flag on BaseSession that spawns each request handler in the session's task group via start_soon instead of awaiting it inline. ClientSession enables the flag. ServerSession stays serial because its _received_request has an InitializeRequest state machine (Initializing → respond → Initialized) that would race if a follow-up request were dispatched concurrently before the state transitioned. Collateral fixes Concurrent dispatch widens two latent RequestResponder races: Cancel-before-enter. __enter__ used to replace the cancel scope created in __init__. With concurrent dispatch, a CancelledNotification can arrive before the handler task enters with responder: — the pre-entry cancel() would then target a scope that is about to be thrown away. Fixed by not replacing the scope in __enter__. Double cancel / pre-entry cancel. cancel() is now idempotent (early return when already completed) and no longer requires _entered. Handler exceptions are translated into a JSON-RPC error response so a raising handler can't wedge the peer (otherwise the exception would propagate from the dispatch task and tear down the session's task group). Tests Four regression tests in tests/shared/test_session.py: test_concurrent_server_to_client_requests_run_in_parallel — N=4 fan-out, asserts peak concurrency = 4. test_sampling_callback_exception_returns_error_response — handler that raises still gets a JSON-RPC error reply for its id. test_double_cancel_does_not_send_second_response — idempotent cancel. test_cancel_before_context_entered_marks_scope_cancelled — cancel is safe before __enter__. test_handler_that_responds_then_raises_emits_no_duplicate_error — responded-then-raised handlers don't produce a second error for the same id. All 1174 tests pass; 100% branch coverage maintained; strict-no-cover, ruff, and pyright clean.
Open Graph Description: Closes #2489. Problem BaseSession._receive_loop awaited each incoming request handler inline, so concurrent server→client requests (e.g. N create_message sampling callbacks launched via asyncio.gat...
X Description: Closes #2489. Problem BaseSession._receive_loop awaited each incoming request handler inline, so concurrent server→client requests (e.g. N create_message sampling callbacks launched via asyncio.gat...
Opengraph URL: https://github.com/modelcontextprotocol/python-sdk/pull/2490
X: @github
Domain: github.com
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:d255ae0c-8313-144b-2b23-c01aca8e84cb |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | 9634:142E39:182F72:20EEB7:6A5A5A91 |
| html-safe-nonce | 3ec499c231544c255d274df89c7dffc9efaa1b988fca560a7a829bd73afa25bc |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5NjM0OjE0MkUzOToxODJGNzI6MjBFRUI3OjZBNUE1QTkxIiwidmlzaXRvcl9pZCI6Ijc0MzIyNjY2ODY0NTg5MTEzNzciLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 9d14bdd42d927c7f13f583b6669fca46270405c7e7e56290effcf0c49bcacb4c |
| hovercard-subject-tag | pull_request:3564327388 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/modelcontextprotocol/python-sdk/pull/2490/files |
| twitter:image | https://avatars.githubusercontent.com/u/47821?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/47821?s=400&v=4 |
| og:image:alt | Closes #2489. Problem BaseSession._receive_loop awaited each incoming request handler inline, so concurrent server→client requests (e.g. N create_message sampling callbacks launched via asyncio.gat... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | 05b9ddf6a47d2dbe13944873a99f5fb4b83ba4871f9cb8a8e256793a63ca9687 |
| turbo-cache-control | no-preview |
| diff-view | unified |
| go-import | github.com/modelcontextprotocol/python-sdk git https://github.com/modelcontextprotocol/python-sdk.git |
| octolytics-dimension-user_id | 182288589 |
| octolytics-dimension-user_login | modelcontextprotocol |
| octolytics-dimension-repository_id | 862584018 |
| octolytics-dimension-repository_nwo | modelcontextprotocol/python-sdk |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 862584018 |
| octolytics-dimension-repository_network_root_nwo | modelcontextprotocol/python-sdk |
| turbo-body-classes | logged-out env-production page-responsive full-width |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | f8d29d1bd03dda2dd14b3f80b8bc27e1111f43bd |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width