René's URL Explorer Experiment


Title: stdio server makes two worker-thread hops for every message it writes · Issue #3888 · modelcontextprotocol/python-sdk · GitHub

Open Graph Title: stdio server makes two worker-thread hops for every message it writes · Issue #3888 · modelcontextprotocol/python-sdk

X Title: stdio server makes two worker-thread hops for every message it writes · Issue #3888 · modelcontextprotocol/python-sdk

Description: Description stdout_writer in src/mcp/server/stdio.py does await stdout.write(json + "\n") await stdout.flush() stdout is an anyio.AsyncFile, so each of those awaits is its own anyio.to_thread.run_sync round trip. The write is no use to t...

Open Graph Description: Description stdout_writer in src/mcp/server/stdio.py does await stdout.write(json + "\n") await stdout.flush() stdout is an anyio.AsyncFile, so each of those awaits is its own anyio.to_thread.run_s...

X Description: Description stdout_writer in src/mcp/server/stdio.py does await stdout.write(json + "\n") await stdout.flush() stdout is an anyio.AsyncFile, so each of those awaits is its own anyio.to_th...

Opengraph URL: https://github.com/modelcontextprotocol/python-sdk/issues/3888

X: @github

direct link

Domain: github.com

IPv4 address: 140.82.114.4


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"stdio server makes two worker-thread hops for every message it writes","articleBody":"### Description\n\n`stdout_writer` in `src/mcp/server/stdio.py` does\n\n```python\nawait stdout.write(json + \"\\n\")\nawait stdout.flush()\n```\n\n`stdout` is an `anyio.AsyncFile`, so each of those awaits is its own `anyio.to_thread.run_sync` round trip. The write is no use to the client until the flush, and the writer handles one message at a time, so every response and notification pays for two hops where one would do. It shows up when a tool sends progress or log notifications: each one queues behind two thread round trips.\n\nHop count, standalone (counts only the writer's hops):\n\n```python\nimport io\n\nimport anyio\nimport anyio.to_thread\nfrom mcp_types import JSONRPCNotification\n\nfrom mcp.server.stdio import stdio_server\nfrom mcp.shared.message import SessionMessage\n\nhops = 0\n_run_sync = anyio.to_thread.run_sync\n\n\nasync def counting_run_sync(func, *args, **kwargs):\n    global hops\n    hops += 1\n    return await _run_sync(func, *args, **kwargs)\n\n\nasync def main() -> None:\n    global hops\n    stdin = anyio.AsyncFile(io.StringIO())  # empty: the reader hits EOF straight away\n    stdout = anyio.AsyncFile(io.StringIO())\n    async with stdio_server(stdin=stdin, stdout=stdout) as (read_stream, write_stream):\n        async with read_stream:\n            async for _ in read_stream:\n                pass\n        anyio.to_thread.run_sync = counting_run_sync  # count only the writer from here on\n        n = 10\n        for i in range(n):\n            note = JSONRPCNotification(\n                jsonrpc=\"2.0\", method=\"notifications/progress\", params={\"progressToken\": 1, \"progress\": i}\n            )\n            await write_stream.send(SessionMessage(note))\n        await write_stream.aclose()\n    print(f\"{n} messages written: {hops} worker-thread hops\")\n\n\nanyio.run(main)\n```\n\nOn `main` (77001c9) this prints `10 messages written: 20 worker-thread hops`. I'd expect 10.\n\nTiming over a real stdio subprocess (`Client` spawning the server via `StdioServerParameters`), interleaved before/after runs, median wall time:\n\n| workload | before | after | change |\n|---|---|---|---|\n| tool sending 5 progress notifications per call, 300 calls | 578 ms | 402 ms | -30% |\n| tool sending 20 progress notifications per call, 300 calls (legacy protocol) | 1326 ms | 851 ms | -36% |\n| 50 concurrent async tool calls, 1000 total | 544 ms | 422 ms | -22% |\n| plain sequential `call_tool`, 800 calls | 498 ms | 443 ms | -11% |\n\nThe machine was busy during these runs (load average around 10), so treat the absolute numbers loosely. Plain one-request/one-response traffic gains little; it matters once a request produces several messages.\n\nDoing the write and the flush in one `anyio.to_thread.run_sync` call on `stdout.wrapped` fixes it. The output bytes are identical and it's about 10 lines. One thing to note: it would break a duck-typed `stdout` that has async `write`/`flush` but no `.wrapped`, though the parameter is typed `anyio.AsyncFile[str]`. Open PR #2680 rewrites this writer for a different reason (shutdown after EOF) and would remove the double hop as a side effect. I have a branch with the small fix and a test if you want it.\n\nEnvironment: macOS 27.2 on Apple M5, Python 3.12.16, mcp at 77001c9, anyio 4.14.2.\n\n### References\n\n- `src/mcp/server/stdio.py`, `stdout_writer` inside `stdio_server`\n- #2680 (open), which replaces the same writer\n\nI used an AI assistant to help find and measure this; I ran the repro above myself.\n","author":{"url":"https://github.com/patrick-immometrica","@type":"Person","name":"patrick-immometrica"},"datePublished":"2026-11-21T11:42:45Z","url":"https://github.com/modelcontextprotocol/python-sdk/issues/3888"}

react-profiling0
react-import-mapreact,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-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:239896cd-e1c7-c162-a334-60b4f9ef5a8f
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idA86A:237AAE:8E89EA:C1E80A:6B047F35
html-safe-nonceb4e04ac466fc60622c455bffa663dd19ffaf11412fb2cebbb20ed0fe52b603db
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBODZBOjIzN0FBRTo4RTg5RUE6QzFFODBBOjZCMDQ3RjM1IiwidmlzaXRvcl9pZCI6IjU1Nzc2MzUzNzA2NjI5MTk5ODkiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ==
visitor-hmacf9fbd82106ead69fa3e60992ad4aab22e5ef65121d7167f7ff313d737dc85657
hovercard-subject-tagissue:6528347238
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/modelcontextprotocol/python-sdk/3888/issue_layout
twitter:imagehttps://opengraph.githubassets.com/482fb2ee1de4b8d0cb7156bc0a690bf577cc41001bdf2443862e269038016912/modelcontextprotocol/python-sdk/issues/3888
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/482fb2ee1de4b8d0cb7156bc0a690bf577cc41001bdf2443862e269038016912/modelcontextprotocol/python-sdk/issues/3888
og:image:altDescription stdout_writer in src/mcp/server/stdio.py does await stdout.write(json + "\n") await stdout.flush() stdout is an anyio.AsyncFile, so each of those awaits is its own anyio.to_thread.run_s...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernamepatrick-immometrica
hostnamegithub.com
expected-hostnamegithub.com
None0366ddfb372682a32e92a5d86155cb08fa83e5bbe79953a968789f2f7e24c511
turbo-cache-controlno-preview
go-importgithub.com/modelcontextprotocol/python-sdk git https://github.com/modelcontextprotocol/python-sdk.git
octolytics-dimension-user_id182288589
octolytics-dimension-user_loginmodelcontextprotocol
octolytics-dimension-repository_id862584018
octolytics-dimension-repository_nwomodelcontextprotocol/python-sdk
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id862584018
octolytics-dimension-repository_network_root_nwomodelcontextprotocol/python-sdk
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
releaseed3f2e01f6b7672780c586c8af0e72726374d7b6
ui-targetfull
theme-color#1e2327
color-schemelight dark
react-app-nameissues

Links:

Skip to contenthttps://github.com/modelcontextprotocol/python-sdk/issues/3888#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fmodelcontextprotocol%2Fpython-sdk%2Fissues%2F3888
GitHubRepository hosting, code review, and collaborationhttps://github.com/platform
CopilotAI coding agent for tasks, projects, and automated workhttps://github.com/features/copilot
ActionsCI/CD and automationhttps://github.com/features/actions
Code QualityQuality analysis, fixes, and enforcementhttps://github.com/features/code-quality
CodespacesCloud development environmentshttps://github.com/features/codespaces
Code SecurityVulnerability detection and remediationhttps://github.com/security/advanced-security/code-security
Secret ProtectionSecret scanning and leak preventionhttps://github.com/security/advanced-security/secret-protection
All Featureshttps://github.com/features
Documentationhttps://docs.github.com/en
Traininghttps://learn.github.com/skills
Certificationshttps://learn.github.com/credentials
Build with Copilothttps://github.com/features/ai
Customize Copilothttps://docs.github.com/en/copilot/how-tos/copilot-on-github/customize-copilot/customize-copilot-overview
Integrationshttps://github.com/marketplace
Trending reposhttps://github.com/trending
GitHub Sponsorshttps://github.com/open-source/sponsors
Developer articleshttps://github.com/resources/articles
ChangelogYour source for the latest features, improvements, and fixes across GitHub products, all in one place.https://github.blog/changelog/
Enterpriseshttps://github.com/enterprise
Teamhttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
See all industrieshttps://github.com/solutions/industry
Why GitHubhttps://github.com/why-github
Customer Storieshttps://github.com/customer-stories
Business insightshttps://github.com/solutions/executive-insights
Trust Centerhttps://github.com/trust-center
GitHub Advanced SecurityEnterprise-grade code securityhttps://github.com/security/advanced-security
Copilot for businessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Documentationhttps://docs.github.com/en
Resource centerhttps://github.com/resources
Bloghttps://github.blog/
Events & webinarshttps://github.com/resources/events
Community Forumhttps://github.com/orgs/community/discussions
Customer Storieshttps://github.com/customer-stories
Integrationshttps://github.com/marketplace
Customer supporthttps://help.github.com/
Traininghttps://learn.github.com/skills
GitHub Statushttps://www.githubstatus.com/
Abouthttps://github.com/about
Partnershttps://github.com/partners
Changeloghttps://github.blog/changelog/
GitHub UniverseThe event for developers, by developers — talks, workshops, and what’s next for GitHub.https://githubuniverse.com/
Pricinghttps://github.com/pricing
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fmodelcontextprotocol%2Fpython-sdk%2Fissues%2F3888
Sign uphttps://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=modelcontextprotocol%2Fpython-sdk
Reloadhttps://github.com/modelcontextprotocol/python-sdk/issues/3888
Reloadhttps://github.com/modelcontextprotocol/python-sdk/issues/3888
Reloadhttps://github.com/modelcontextprotocol/python-sdk/issues/3888
modelcontextprotocolhttps://github.com/modelcontextprotocol
python-sdkhttps://github.com/modelcontextprotocol/python-sdk
Codehttps://github.com/modelcontextprotocol/python-sdk
Issues320 (320)https://github.com/modelcontextprotocol/python-sdk/issues
Pull requests112 (112)https://github.com/modelcontextprotocol/python-sdk/pulls
Actionshttps://github.com/modelcontextprotocol/python-sdk/actions
Projectshttps://github.com/modelcontextprotocol/python-sdk/projects
Security and quality12 (12)https://github.com/modelcontextprotocol/python-sdk/security
Insightshttps://github.com/modelcontextprotocol/python-sdk/insights
https://github.com/modelcontextprotocol/python-sdk
New issuehttps://github.com/login?return_to=https://github.com/modelcontextprotocol/python-sdk/issues/3888
New issuehttps://github.com/login?return_to=https://github.com/modelcontextprotocol/python-sdk/issues/3888
stdio server makes two worker-thread hops for every message it writeshttps://github.com/modelcontextprotocol/python-sdk/issues/3888#top
enhancementRequest for a new feature that's not currently supportedhttps://github.com/modelcontextprotocol/python-sdk/issues?q=state%3Aopen%20label%3A%22enhancement%22
https://github.com/patrick-immometrica
patrick-immometricahttps://github.com/patrick-immometrica
on Nov 21, 2026https://github.com/modelcontextprotocol/python-sdk/issues/3888#issue-6528347238
77001c9https://github.com/modelcontextprotocol/python-sdk/commit/77001c96f9507be0f4d009b83210a384a5e08bda
#2680https://github.com/modelcontextprotocol/python-sdk/pull/2680
77001c9https://github.com/modelcontextprotocol/python-sdk/commit/77001c96f9507be0f4d009b83210a384a5e08bda
fix(stdio): drain responses and bound stdout shutdown after EOF #2680https://github.com/modelcontextprotocol/python-sdk/pull/2680
claudehttps://github.com/apps/claude
enhancementRequest for a new feature that's not currently supportedhttps://github.com/modelcontextprotocol/python-sdk/issues?q=state%3Aopen%20label%3A%22enhancement%22
on Nov 21, 2026https://github.com/modelcontextprotocol/python-sdk/issues/3888#event-36858895184
Sign up for freehttps://github.com/signup?return_to=https://github.com/modelcontextprotocol/python-sdk/issues/3888
Sign in to commenthttps://github.com/login?return_to=https://github.com/modelcontextprotocol/python-sdk/issues/3888
enhancementRequest for a new feature that's not currently supportedhttps://github.com/modelcontextprotocol/python-sdk/issues?q=state%3Aopen%20label%3A%22enhancement%22
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Response headers:
Servergithub.com
Content-Typetext/html; charset=utf-8
Cache-Controlmax-age=0, private, must-revalidate

Viewport: width=device-width


URLs of crawlers that visited me.