Title: Heavy sync init work (e.g. local ML models) starves stdio initialize/tool calls even with threadpool offload -- Windows GIL contention · Issue #3089 · modelcontextprotocol/python-sdk · GitHub
Open Graph Title: Heavy sync init work (e.g. local ML models) starves stdio initialize/tool calls even with threadpool offload -- Windows GIL contention · Issue #3089 · modelcontextprotocol/python-sdk
X Title: Heavy sync init work (e.g. local ML models) starves stdio initialize/tool calls even with threadpool offload -- Windows GIL contention · Issue #3089 · modelcontextprotocol/python-sdk
Description: Environment mcp 1.28.1 (Python SDK, mcp.server.fastmcp.FastMCP) stdio transport, Windows 11 Blocking work: sentence-transformers / torch (local embedding model), but this generalizes to any CPU/GIL-bound library load (onnxruntime, local ...
Open Graph Description: Environment mcp 1.28.1 (Python SDK, mcp.server.fastmcp.FastMCP) stdio transport, Windows 11 Blocking work: sentence-transformers / torch (local embedding model), but this generalizes to any CPU/GIL...
X Description: Environment mcp 1.28.1 (Python SDK, mcp.server.fastmcp.FastMCP) stdio transport, Windows 11 Blocking work: sentence-transformers / torch (local embedding model), but this generalizes to any CPU/GIL...
Opengraph URL: https://github.com/modelcontextprotocol/python-sdk/issues/3089
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Heavy sync init work (e.g. local ML models) starves stdio initialize/tool calls even with threadpool offload -- Windows GIL contention","articleBody":"### Environment\n- `mcp` 1.28.1 (Python SDK, `mcp.server.fastmcp.FastMCP`)\n- stdio transport, Windows 11\n- Blocking work: `sentence-transformers` / torch (local embedding model), but this\n generalizes to any CPU/GIL-bound library load (onnxruntime, local LLM inference, etc.)\n\n### Summary\nRelated to #1839 / #1909, but a distinct failure mode that threadpool offload does not\nfix: **GIL-bound CPU work** (loading a torch model) contends with the anyio stdio\ntransport's Windows pipe-reader thread for the GIL, regardless of where it runs in-process.\nThis makes several reasonable-looking designs fail in ways that are easy to ship and hard\nto reproduce in a quick manual test.\n\n### Measurements\nSame model load (`sentence-transformers/all-MiniLM-L6-v2`), only the location changes:\n\n| Where the load runs | `initialize` | First tool call |\n|----------------------------------------------------|:---:|:---:|\n| Standalone script, no event loop | — | ~8s |\n| Synchronously before `mcp.run()` (\"warm on start\") | **blocks 8–70s** | instant |\n| In a background thread, started before `mcp.run()` | delayed | **deadlocks** |\n| Lazily on first call, offloaded via `anyio.to_thread` / threadpool | instant | **~70s** |\n| In a **separate subprocess**, talked to over a pipe | ~1s | ~8–9s |\n\nOnly the subprocess isolation keeps both connection time and first-call time bounded.\nThe variance (8s → 70s) is driven by contention between the model-loading thread and\nanyio's Windows stdio reader thread — both fighting for the GIL.\n\n### Why this matters beyond my case\nIt's tempting to \"fix\" a slow first tool call by warming eagerly before `mcp.run()`.\nThat instead moves the stall onto the `initialize` handshake — which is worse, because a\nclient that doesn't get the handshake in time just drops the server with no visible\nerror. That's a silent failure (tools missing, no exception) that's *intermittent*\n(fine when warm/cached, broken on a cold start), so it's easy to ship and hard to catch\nin CI or a quick manual check.\n\n### Suggested fix / ask\nNot asking for an SDK-level fix necessarily — `run_in_threadpool` from #1909 is the right\nanswer for I/O-bound blocking calls. But it'd help other implementers to:\n1. Note in the docs/guidance for `FastMCP` that CPU/GIL-bound initialization (local model\n loads, etc.) should NOT be threadpool-offloaded in-process — it should run in a\n separate subprocess — since threadpool offload does not release the GIL contention\n the way it does for I/O-bound blocking calls.\n2. Consider whether `initialize` should have an explicit, documented deadline/backpressure\n behavior so implementers know exactly how much startup latency is safe.\n\n---\n**Disclaimer:** I'm not a Python/asyncio expert — this diagnosis, the measurements, and\nthis write-up were produced by Claude Code (Anthropic's coding agent) while it was\nbuilding a local memory/retrieval MCP server for me and debugging why it intermittently\nfailed to connect. I'm filing it because the finding looked substantive and reproducible,\nbut I likely can't answer deep follow-up questions about the internals myself.\n","author":{"url":"https://github.com/scotthibbs","@type":"Person","name":"scotthibbs"},"datePublished":"2026-07-12T18:08:58.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":2},"url":"https://github.com/3089/python-sdk/issues/3089"}
| 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:b82188ae-c734-d86d-eb42-1035bf0727b1 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D8D8:17B883:1AD363B:243FA6E:6A598367 |
| html-safe-nonce | beb2aad6dea59f948f168d92b13cdab052cc9f8afdee8601eaa0d88adba36d75 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEOEQ4OjE3Qjg4MzoxQUQzNjNCOjI0M0ZBNkU6NkE1OTgzNjciLCJ2aXNpdG9yX2lkIjoiNjE5OTkwMTM1NjQ2MTQ5MTA0NyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 5fc38438ae5656d95d9283b6aacbd1584bb6cecaa341a2100070298d0d57777d |
| hovercard-subject-tag | issue:4868174044 |
| 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/3089/issue_layout |
| twitter:image | https://opengraph.githubassets.com/580ccfd9f2741ca62a8b861bdb8bbbb6c0565504e11c6f10339b19f6ce92bc03/modelcontextprotocol/python-sdk/issues/3089 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/580ccfd9f2741ca62a8b861bdb8bbbb6c0565504e11c6f10339b19f6ce92bc03/modelcontextprotocol/python-sdk/issues/3089 |
| og:image:alt | Environment mcp 1.28.1 (Python SDK, mcp.server.fastmcp.FastMCP) stdio transport, Windows 11 Blocking work: sentence-transformers / torch (local embedding model), but this generalizes to any CPU/GIL... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | scotthibbs |
| hostname | github.com |
| expected-hostname | github.com |
| None | a540949572872b935b393b36db38922db390ae71c859537d741b8f3eb7e545b5 |
| 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 | 624bb50a7497aa346bef8cc3743af408a9ea10ca |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width