Title: Bug: OAuth provider holds context.lock for the whole request, so the standalone GET SSE stream stalls the first tools/call by ~15s · Issue #3209 · modelcontextprotocol/python-sdk · GitHub
Open Graph Title: Bug: OAuth provider holds context.lock for the whole request, so the standalone GET SSE stream stalls the first tools/call by ~15s · Issue #3209 · modelcontextprotocol/python-sdk
X Title: Bug: OAuth provider holds context.lock for the whole request, so the standalone GET SSE stream stalls the first tools/call by ~15s · Issue #3209 · modelcontextprotocol/python-sdk
Description: Summary With auth=OAuthClientProvider on a streamable-HTTP connection, the first tools/call of every session blocks for ~15 s. No error, no warning — just latency. Nothing is concurrent here: one server, one client, one session, sequenti...
Open Graph Description: Summary With auth=OAuthClientProvider on a streamable-HTTP connection, the first tools/call of every session blocks for ~15 s. No error, no warning — just latency. Nothing is concurrent here: one s...
X Description: Summary With auth=OAuthClientProvider on a streamable-HTTP connection, the first tools/call of every session blocks for ~15 s. No error, no warning — just latency. Nothing is concurrent here: one s...
Opengraph URL: https://github.com/modelcontextprotocol/python-sdk/issues/3209
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Bug: OAuth provider holds context.lock for the whole request, so the standalone GET SSE stream stalls the first tools/call by ~15s","articleBody":"## Summary\n\nWith `auth=OAuthClientProvider` on a **streamable-HTTP** connection, the *first* `tools/call` of every session blocks for ~15 s. No error, no warning — just latency. Nothing is concurrent here: one server, one client, one session, sequential calls.\n\nRoot cause is the same `context.lock`-held-across-`yield` problem as #2847 / #2644, but the symptom is different enough that it seems worth recording separately: #2847 is about `RuntimeError: The current task is not holding this lock` under *concurrent* OAuth connections, while this is a silent latency tax paid by *every* OAuth streamable-HTTP user, concurrency or not. Both are fixed by #2858.\n\n## Environment\n\n- `mcp==1.29.0`, `anyio==4.13.0`, `httpx==0.28.1`\n- Python 3.14.6, macOS arm64\n- Server: FastMCP behind Google Cloud Run, streamable HTTP, OAuth 2.1 + PKCE\n\n## Measurement\n\nSame server, same tool, one fresh session per call, three calls per arm. The *only* variable is how the bearer token reaches the request:\n\n```\nauth=OAuthClientProvider : 15.68s / 15.74s / 15.80s\nstatic Authorization header: 0.70s / 0.62s / 0.67s\n```\n\nRaw HTTP JSON-RPC against the same endpoint (no SDK) answers the same tool in ~0.07 s, so the useful work is ~100 ms and effectively all of the 15.7 s is the wait.\n\nRepro:\n\n```python\nimport time, anyio\nfrom mcp.client.session import ClientSession\nfrom mcp.client.streamable_http import streamablehttp_client\n\nasync def one(*, use_provider: bool) -> float:\n headers = None if use_provider else {\"Authorization\": f\"Bearer {ACCESS_TOKEN}\"}\n auth = build_provider() if use_provider else None # OAuthClientProvider\n t0 = time.perf_counter()\n async with streamablehttp_client(URL, headers=headers, auth=auth) as (r, w, _):\n async with ClientSession(r, w) as session:\n await session.initialize()\n await session.call_tool(\"get_last_modified\", {})\n return time.perf_counter() - t0\n```\n\n## Why it happens\n\n1. `async_auth_flow` takes `context.lock` and holds it for the whole lifetime of the request, not just while it obtains a token — the `async with self.context.lock:` at `src/mcp/client/auth/oauth2.py:492` wraps `response = yield request`.\n2. After `initialized`, `streamablehttp_client` opens the long-lived standalone GET SSE stream.\n3. That GET goes through `async_auth_flow`, takes the lock, and does not release it until the stream ends — 15–60 s in our logs, dictated by the server's/proxy's idle timeout, not by the client.\n4. The first `tools/call` therefore waits on the lock for as long as the GET stream lives.\n5. Once the stream closes, subsequent calls run at ~100 ms until the next stream is opened.\n\nSo the stall is not a fixed 15 s: it is \"however long the server keeps an idle SSE stream open\", which for Cloud Run happens to be ~15 s and for other deployments in this thread ~60 s.\n\n## The fix\n\n#2858 (rebase of #2660 by @peisuke) already does the right thing: no HTTP `yield` — including the standalone GET SSE long-poll — runs while `context.lock` is held. Verified locally that with the lock out of the request path the same call is ~0.4–0.7 s.\n\nOur workaround in the meantime is to keep `OAuthClientProvider` out of the request path entirely: read the token from storage and pass it as a plain `Authorization` header, refreshing it ourselves. That is a fine escape hatch for an application, but it means giving up the SDK's auth handling, which most users reasonably will not want to do.\n\nWould a maintainer be able to look at #2858? It fixes a correctness bug for concurrent users and a ~15 s per-session latency bug for everyone else.\n","author":{"url":"https://github.com/stayclosetothequestion","@type":"Person","name":"stayclosetothequestion"},"datePublished":"2026-07-29T11:34:38Z","url":"https://github.com/modelcontextprotocol/python-sdk/issues/3209"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:e714e37d-e82a-05ae-f04f-671e1ec53b77 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 987C:21E3E7:1612C7E:1D15874:6B012E57 |
| html-safe-nonce | a7b51cdd5b2d4ecf0b56e3d802226f4988bcfbcac1f97ac10d6843e3b548ba29 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5ODdDOjIxRTNFNzoxNjEyQzdFOjFEMTU4NzQ6NkIwMTJFNTciLCJ2aXNpdG9yX2lkIjoiNjE1MTAwNTYyMzQxMzcxNDUxOSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | cc6700cc3cf801a01d2e0f439e8ac2704dc51c7ad72d9f20fd76fb997c1c7db2 |
| hovercard-subject-tag | issue:5008752248 |
| github-keyboard-shortcuts | repository,issues,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/_view_fragments/issues/show/modelcontextprotocol/python-sdk/3209/issue_layout |
| twitter:image | https://opengraph.githubassets.com/1a3a471c471289af04f51f9394315c636a01455a3c0b06e66f306fe064bfe8a9/modelcontextprotocol/python-sdk/issues/3209 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/1a3a471c471289af04f51f9394315c636a01455a3c0b06e66f306fe064bfe8a9/modelcontextprotocol/python-sdk/issues/3209 |
| og:image:alt | Summary With auth=OAuthClientProvider on a streamable-HTTP connection, the first tools/call of every session blocks for ~15 s. No error, no warning — just latency. Nothing is concurrent here: one s... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | stayclosetothequestion |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| turbo-cache-control | no-preview |
| 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 |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width