Title: Bug: anyio.Lock in async_auth_flow causes RuntimeError under concurrent OAuth MCP connections · Issue #2847 · modelcontextprotocol/python-sdk · GitHub
Open Graph Title: Bug: anyio.Lock in async_auth_flow causes RuntimeError under concurrent OAuth MCP connections · Issue #2847 · modelcontextprotocol/python-sdk
X Title: Bug: anyio.Lock in async_auth_flow causes RuntimeError under concurrent OAuth MCP connections · Issue #2847 · modelcontextprotocol/python-sdk
Description: Summary Confirmed production occurrence of the bug described in #2644 (self-closed by the original reporter before they could follow through). Filing to provide a second validated repro and draw attention to the existing draft fix in #26...
Open Graph Description: Summary Confirmed production occurrence of the bug described in #2644 (self-closed by the original reporter before they could follow through). Filing to provide a second validated repro and draw at...
X Description: Summary Confirmed production occurrence of the bug described in #2644 (self-closed by the original reporter before they could follow through). Filing to provide a second validated repro and draw at...
Opengraph URL: https://github.com/modelcontextprotocol/python-sdk/issues/2847
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Bug: anyio.Lock in async_auth_flow causes RuntimeError under concurrent OAuth MCP connections","articleBody":"## Summary\n\nConfirmed production occurrence of the bug described in #2644 (self-closed by the original reporter before they could follow through). Filing to provide a second validated repro and draw attention to the existing draft fix in #2660.\n\n## Environment\n\n- `mcp==1.26.0`\n- `anyio==4.13.0`\n- `httpx==0.28.1`\n- Python 3.11.14, macOS\n\n## What happens\n\nWhen a gateway session starts and connects to multiple OAuth-authenticated MCP servers concurrently (Notion + TinyFish, both OAuth 2.1 PKCE), Notion fails intermittently:\n\n```\nERROR asyncio: Task exception was never retrieved\nRuntimeError: The current task is not holding this lock\n File \".../mcp/client/auth/oauth2.py\", line 503, in async_auth_flow\n File \".../mcp/client/auth/oauth2.py\", line 484, in async_auth_flow\n raise RuntimeError(\"The current task is not holding this lock\")\n\nWARNING tools.mcp_tool: MCP server 'notion' connection lost (attempt 1/5), reconnecting in 1s\nWARNING tools.mcp_tool: Failed to connect to MCP server 'notion': CancelledError\nINFO tools.mcp_tool: MCP: registered 114 tool(s) from 4 server(s) (1 failed)\n```\n\nNotion is affected more often than TinyFish because its OAuth token refreshes frequently (~every 15–60 min), consistently triggering the refresh `yield` path in `async_auth_flow`. TinyFish tokens expire less often and typically take the happy path (add header, yield once).\n\n## Root cause\n\n`OAuthContext.lock` is `anyio.Lock`, which records task identity at `acquire()` and enforces same-task `release()`. `async_auth_flow` is an async generator that holds this lock across `yield` points. When httpx drives the generator from a different task during concurrent connections, `anyio.Lock.release()` throws.\n\n## Existing draft fix\n\nPR #2660 addresses this correctly by narrowing lock scope so no lock is held across yields — GET SSE long-polls and token refresh yields both run outside any lock. It has full test coverage (100% on `oauth2.py`, 1177 passed) and no breaking changes, but has been sitting as a draft without maintainer review since May 22.\n\n## Workaround applied locally\n\nReplacing `anyio.Lock` with `asyncio.Lock` in `OAuthContext` stops the error since `asyncio.Lock` does not enforce task identity on release. This is a bandaid — it loses trio portability — but unblocks asyncio deployments. Note: this is a mechanical fix and has not been load-tested to exhaustion; the intermittent nature of the bug means full verification requires sustained concurrent load.\n\n## Request\n\nCould a maintainer review and merge PR #2660? The fix is principled and well-tested.","author":{"url":"https://github.com/EnGassa","@type":"Person","name":"EnGassa"},"datePublished":"2026-06-12T10:39:59Z","url":"https://github.com/modelcontextprotocol/python-sdk/issues/2847"}
| 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:90dfc920-85ea-54e3-b017-17d81076afb4 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A4F2:2BE220:126A1E0:189EA65:6B01239F |
| html-safe-nonce | 6a490623a5d30164bdfcbf755b9f1cc0d4ad53b37ebb6c20597d7b163c0e4796 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBNEYyOjJCRTIyMDoxMjZBMUUwOjE4OUVBNjU6NkIwMTIzOUYiLCJ2aXNpdG9yX2lkIjoiNzA2NTY3OTkyMjM3OTU4MDQ3IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 2a7d26bd93c6f846e0706542e9a2c3227b1b5509336895b2b7df8071f0668904 |
| hovercard-subject-tag | issue:4648518240 |
| 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/2847/issue_layout |
| twitter:image | https://opengraph.githubassets.com/7913069bcbacceb6e3a676eeaa43a276951d9c0c9d8143cdbacd75173a5b0d0c/modelcontextprotocol/python-sdk/issues/2847 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/7913069bcbacceb6e3a676eeaa43a276951d9c0c9d8143cdbacd75173a5b0d0c/modelcontextprotocol/python-sdk/issues/2847 |
| og:image:alt | Summary Confirmed production occurrence of the bug described in #2644 (self-closed by the original reporter before they could follow through). Filing to provide a second validated repro and draw at... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | EnGassa |
| 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