René's URL Explorer Experiment


Title: Don't close real process stdio in stdio_server (#1933) by steps-re · Pull Request #3090 · modelcontextprotocol/python-sdk · GitHub

Open Graph Title: Don't close real process stdio in stdio_server (#1933) by steps-re · Pull Request #3090 · modelcontextprotocol/python-sdk

X Title: Don't close real process stdio in stdio_server (#1933) by steps-re · Pull Request #3090 · modelcontextprotocol/python-sdk

Description: The bug transport="stdio" closes the real process stdin/stdout. Any code that runs after the server exits raises: ValueError: I/O operation on closed file. A minimal reproduction is just a print() after run(transport="stdio") returns. Root cause src/mcp/server/stdio.py re-wraps sys.stdin.buffer / sys.stdout.buffer in a TextIOWrapper to force UTF-8: stdin = anyio.wrap_file(TextIOWrapper(sys.stdin.buffer, encoding="utf-8", errors="replace")) stdout = anyio.wrap_file(TextIOWrapper(sys.stdout.buffer, encoding="utf-8")) The comment says these purposely avoid a context manager so the standard handles stay open. But a TextIOWrapper closes the buffer it wraps in its __del__ finalizer. Once the wrapper is garbage-collected, sys.stdin.buffer / sys.stdout.buffer are closed, so the real sys.stdin / sys.stdout are dead and every later read or write fails. The wrapper defeats the very thing the comment intends. The fix Dup the underlying fd and wrap the copy, so the wrapper only ever closes the duplicate, never the real handle. os.dup is Windows-safe. The dup'd wrappers are closed on exit so their fds don't leak. binary = open(os.dup(std.fileno()), mode + "b", closefd=True) wrapper = TextIOWrapper(binary, encoding="utf-8", errors=errors) When sys.std* has no real fd (pytest capture, embedded interpreters, injected in-memory streams), it falls back to wrapping .buffer directly and detach()es the wrapper on exit, which severs it from the buffer without closing it, so the finalizer can no longer close the real handle either. This keeps the existing in-memory / monkeypatched test paths working. This matches the approach the issue author proposed. Test Adds a regression test in tests/server/test_stdio.py: after async with stdio_server(): ... exits, the real sys.stdin / sys.stdout buffers must not be closed, and a write to stdout after exit must succeed. It fails on main (AssertionError: stdio_server closed the real stdin buffer) and passes with the fix. All stdio tests pass (pytest tests/ -k stdio → 51 passed, 5 Windows-only skipped), including the real-subprocess round-trip interaction test. ruff check, ruff format --check, and pyright are clean on the changed files. Fixes #1933 This was developed with AI assistance (Claude Code). I understand and have tested the change; happy to walk through any part.

Open Graph Description: The bug transport="stdio" closes the real process stdin/stdout. Any code that runs after the server exits raises: ValueError: I/O operation on closed file. A minimal reproduction is just...

X Description: The bug transport="stdio" closes the real process stdin/stdout. Any code that runs after the server exits raises: ValueError: I/O operation on closed file. A minimal reproduction...

Opengraph URL: https://github.com/modelcontextprotocol/python-sdk/pull/3090

X: @github

direct link

Domain: github.com

route-pattern/:user_id/:repository/pull/:id/files(.:format)
route-controllerpull_requests
route-actionfiles
fetch-noncev2:9e905453-6045-72a1-577d-6a46eda1e503
current-catalog-service-hashae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b
request-idCD8E:389F43:11DC616:17D2BFC:6A5AAF64
html-safe-noncef2af5361a0e9eeb33b2d6580195412241fe17a5690ac2e4da2bf554ee28c7934
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDRDhFOjM4OUY0MzoxMURDNjE2OjE3RDJCRkM6NkE1QUFGNjQiLCJ2aXNpdG9yX2lkIjoiMjgyMTY2MDIzMzgzMTE5ODU2NCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9
visitor-hmacdc7673d03cda0b991b086b0d651277a2a567bbc3f55af9008b34d903e6d47adb
hovercard-subject-tagpull_request:4046815685
github-keyboard-shortcutsrepository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///pull_requests/show/files
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/modelcontextprotocol/python-sdk/pull/3090/files
twitter:imagehttps://avatars.githubusercontent.com/u/91017100?s=400&v=4
twitter:cardsummary_large_image
og:imagehttps://avatars.githubusercontent.com/u/91017100?s=400&v=4
og:image:altThe bug transport="stdio" closes the real process stdin/stdout. Any code that runs after the server exits raises: ValueError: I/O operation on closed file. A minimal reproduction is just...
og:site_nameGitHub
og:typeobject
hostnamegithub.com
expected-hostnamegithub.com
None21c53da6db0c40484841ba32fe19ecf485e7e734334faab487cd59d884d2bb33
turbo-cache-controlno-preview
diff-viewunified
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 full-width
disable-turbotrue
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release6e85f20f54e1147d8162094880590864abefff6b
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/modelcontextprotocol/python-sdk/pull/3090/files#start-of-content
https://github.com/
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fmodelcontextprotocol%2Fpython-sdk%2Fpull%2F3090%2Ffiles
GitHub CopilotWrite better code with AIhttps://github.com/features/copilot
GitHub Copilot appDirect agents from issue to mergehttps://github.com/features/ai/github-app
MCP RegistryNewIntegrate external toolshttps://github.com/mcp
ActionsAutomate any workflowhttps://github.com/features/actions
CodespacesInstant dev environmentshttps://github.com/features/codespaces
IssuesPlan and track workhttps://github.com/features/issues
Code ReviewManage code changeshttps://github.com/features/code-review
GitHub Advanced SecurityFind and fix vulnerabilitieshttps://github.com/security/advanced-security
Code securitySecure your code as you buildhttps://github.com/security/advanced-security/code-security
Secret protectionStop leaks before they starthttps://github.com/security/advanced-security/secret-protection
Why GitHubhttps://github.com/why-github
Documentationhttps://docs.github.com
Bloghttps://github.blog
Changeloghttps://github.blog/changelog
Marketplacehttps://github.com/marketplace
View all featureshttps://github.com/features
Enterpriseshttps://github.com/enterprise
Small and medium teamshttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
App Modernizationhttps://github.com/solutions/use-case/app-modernization
DevSecOpshttps://github.com/solutions/use-case/devsecops
DevOpshttps://github.com/solutions/use-case/devops
CI/CDhttps://github.com/solutions/use-case/ci-cd
View all use caseshttps://github.com/solutions/use-case
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
View all industrieshttps://github.com/solutions/industry
View all solutionshttps://github.com/solutions
AIhttps://github.com/resources/articles?topic=ai
Software Developmenthttps://github.com/resources/articles?topic=software-development
DevOpshttps://github.com/resources/articles?topic=devops
Securityhttps://github.com/resources/articles?topic=security
View all topicshttps://github.com/resources/articles
Customer storieshttps://github.com/customer-stories
Events & webinarshttps://github.com/resources/events
Ebooks & reportshttps://github.com/resources/whitepapers
Business insightshttps://github.com/solutions/executive-insights
GitHub Skillshttps://skills.github.com
Documentationhttps://docs.github.com
Customer supporthttps://support.github.com
Community forumhttps://github.com/orgs/community/discussions
Trust centerhttps://github.com/trust-center
Partnershttps://github.com/partners
View all resourceshttps://github.com/resources
GitHub SponsorsFund open source developershttps://github.com/open-source/sponsors
Security Labhttps://securitylab.github.com
Maintainer Communityhttps://maintainers.github.com
Acceleratorhttps://github.com/open-source/accelerator
GitHub Starshttps://stars.github.com
Archive Programhttps://archiveprogram.github.com
Topicshttps://github.com/topics
Trendinghttps://github.com/trending
Collectionshttps://github.com/collections
Enterprise platformAI-powered developer platformhttps://github.com/enterprise
GitHub Advanced SecurityEnterprise-grade security featureshttps://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
Pricinghttps://github.com/pricing
Search syntax tipshttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
documentationhttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fmodelcontextprotocol%2Fpython-sdk%2Fpull%2F3090%2Ffiles
Sign up https://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fpull_requests%2Fshow%2Ffiles&source=header-repo&source_repo=modelcontextprotocol%2Fpython-sdk
Reloadhttps://github.com/modelcontextprotocol/python-sdk/pull/3090/files
Reloadhttps://github.com/modelcontextprotocol/python-sdk/pull/3090/files
Reloadhttps://github.com/modelcontextprotocol/python-sdk/pull/3090/files
Please reload this pagehttps://github.com/modelcontextprotocol/python-sdk/pull/3090/files
modelcontextprotocol https://github.com/modelcontextprotocol
python-sdkhttps://github.com/modelcontextprotocol/python-sdk
Notifications https://github.com/login?return_to=%2Fmodelcontextprotocol%2Fpython-sdk
Fork 3.7k https://github.com/login?return_to=%2Fmodelcontextprotocol%2Fpython-sdk
Star 23.6k https://github.com/login?return_to=%2Fmodelcontextprotocol%2Fpython-sdk
Code https://github.com/modelcontextprotocol/python-sdk
Issues 257 https://github.com/modelcontextprotocol/python-sdk/issues
Pull requests 303 https://github.com/modelcontextprotocol/python-sdk/pulls
Actions https://github.com/modelcontextprotocol/python-sdk/actions
Projects https://github.com/modelcontextprotocol/python-sdk/projects
Models https://github.com/modelcontextprotocol/python-sdk/models
Security and quality 6 https://github.com/modelcontextprotocol/python-sdk/security
Insights https://github.com/modelcontextprotocol/python-sdk/pulse
Code https://github.com/modelcontextprotocol/python-sdk
Issues https://github.com/modelcontextprotocol/python-sdk/issues
Pull requests https://github.com/modelcontextprotocol/python-sdk/pulls
Actions https://github.com/modelcontextprotocol/python-sdk/actions
Projects https://github.com/modelcontextprotocol/python-sdk/projects
Models https://github.com/modelcontextprotocol/python-sdk/models
Security and quality https://github.com/modelcontextprotocol/python-sdk/security
Insights https://github.com/modelcontextprotocol/python-sdk/pulse
Sign up for GitHub https://github.com/signup?return_to=%2Fmodelcontextprotocol%2Fpython-sdk%2Fissues%2Fnew%2Fchoose
terms of servicehttps://docs.github.com/terms
privacy statementhttps://docs.github.com/privacy
Sign inhttps://github.com/login?return_to=%2Fmodelcontextprotocol%2Fpython-sdk%2Fissues%2Fnew%2Fchoose
steps-rehttps://github.com/steps-re
modelcontextprotocol:mainhttps://github.com/modelcontextprotocol/python-sdk/tree/main
steps-re:fix/stdio-preserve-real-std-handleshttps://github.com/steps-re/python-sdk/tree/fix/stdio-preserve-real-std-handles
Conversation 4 https://github.com/modelcontextprotocol/python-sdk/pull/3090
Commits 3 https://github.com/modelcontextprotocol/python-sdk/pull/3090/commits
Checks 29 https://github.com/modelcontextprotocol/python-sdk/pull/3090/checks
Files changed 2 https://github.com/modelcontextprotocol/python-sdk/pull/3090/files
Don't close real process stdio in stdio_server (#1933) https://github.com/modelcontextprotocol/python-sdk/pull/3090/files#top
Show all changes 3 commits https://github.com/modelcontextprotocol/python-sdk/pull/3090/files
0e48b3e fix(server): don't close real process stdio in stdio_server (#1933) steps-re Jul 13, 2026 https://github.com/modelcontextprotocol/python-sdk/pull/3090/commits/0e48b3e7a0401c1f08737dff5d52c0838917f298
9a57007 fix(stdio): handle bufferless in-memory text streams in fallback steps-re Jul 13, 2026 https://github.com/modelcontextprotocol/python-sdk/pull/3090/commits/9a57007148a2f1eceff98621aa4ee94caa2c90fa
b04f535 test(stdio): close streams explicitly to satisfy 100% branch coverage steps-re Jul 13, 2026 https://github.com/modelcontextprotocol/python-sdk/pull/3090/commits/b04f535460516d564b91ad1cae7cbfc46311df48
Clear filters https://github.com/modelcontextprotocol/python-sdk/pull/3090/files
Please reload this pagehttps://github.com/modelcontextprotocol/python-sdk/pull/3090/files
Please reload this pagehttps://github.com/modelcontextprotocol/python-sdk/pull/3090/files
stdio.py https://github.com/modelcontextprotocol/python-sdk/pull/3090/files#diff-19a44750677a645222700b9984d14682f626518ae045cf909c4daf665cb3264e
test_stdio.py https://github.com/modelcontextprotocol/python-sdk/pull/3090/files#diff-3e62be39a5c1d8b8dfe922d017c0fae7b023fa1ffac872a0e046f13ef8ca899a
src/mcp/server/stdio.pyhttps://github.com/modelcontextprotocol/python-sdk/pull/3090/files#diff-19a44750677a645222700b9984d14682f626518ae045cf909c4daf665cb3264e
View file https://github.com/modelcontextprotocol/python-sdk/blob/b04f535460516d564b91ad1cae7cbfc46311df48/src/mcp/server/stdio.py
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/modelcontextprotocol/python-sdk/pull/3090/{{ revealButtonHref }}
https://github.com/modelcontextprotocol/python-sdk/pull/3090/files#diff-19a44750677a645222700b9984d14682f626518ae045cf909c4daf665cb3264e
https://github.com/modelcontextprotocol/python-sdk/pull/3090/files#diff-19a44750677a645222700b9984d14682f626518ae045cf909c4daf665cb3264e
Please reload this pagehttps://github.com/modelcontextprotocol/python-sdk/pull/3090/files
https://github.com/modelcontextprotocol/python-sdk/pull/3090/files#diff-19a44750677a645222700b9984d14682f626518ae045cf909c4daf665cb3264e
https://github.com/modelcontextprotocol/python-sdk/pull/3090/files#diff-19a44750677a645222700b9984d14682f626518ae045cf909c4daf665cb3264e
tests/server/test_stdio.pyhttps://github.com/modelcontextprotocol/python-sdk/pull/3090/files#diff-3e62be39a5c1d8b8dfe922d017c0fae7b023fa1ffac872a0e046f13ef8ca899a
View file https://github.com/modelcontextprotocol/python-sdk/blob/b04f535460516d564b91ad1cae7cbfc46311df48/tests/server/test_stdio.py
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/modelcontextprotocol/python-sdk/pull/3090/{{ revealButtonHref }}
https://github.com/modelcontextprotocol/python-sdk/pull/3090/files#diff-3e62be39a5c1d8b8dfe922d017c0fae7b023fa1ffac872a0e046f13ef8ca899a
https://github.com/modelcontextprotocol/python-sdk/pull/3090/files#diff-3e62be39a5c1d8b8dfe922d017c0fae7b023fa1ffac872a0e046f13ef8ca899a
https://github.com/modelcontextprotocol/python-sdk/pull/3090/files#diff-3e62be39a5c1d8b8dfe922d017c0fae7b023fa1ffac872a0e046f13ef8ca899a
Please reload this pagehttps://github.com/modelcontextprotocol/python-sdk/pull/3090/files
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

Viewport: width=device-width


URLs of crawlers that visited me.