René's URL Explorer Experiment


Title: Rewrite TestChildProcessCleanup with socket-based deterministic liveness probe by maxisbey · Pull Request #2265 · modelcontextprotocol/python-sdk · GitHub

Open Graph Title: Rewrite TestChildProcessCleanup with socket-based deterministic liveness probe by maxisbey · Pull Request #2265 · modelcontextprotocol/python-sdk

X Title: Rewrite TestChildProcessCleanup with socket-based deterministic liveness probe by maxisbey · Pull Request #2265 · modelcontextprotocol/python-sdk

Description: Summary Closes #1775. Supersedes #2071. The three TestChildProcessCleanup tests in tests/client/test_stdio.py failed intermittently on Windows/macOS CI. The root cause was a timing-dependent test design: watching a file grow on disk as a proxy for "is the child process alive?", with fixed anyio.sleep() durations to "wait long enough" for nested Python interpreters to start. On loaded CI, "long enough" wasn't. A secondary bug compounded this: when the flaky assertion failed, proc was never terminated — the finally blocks only cleaned up tempfiles. The leaked subprocess was GC'd during a later test, triggering PytestUnraisableExceptionWarning there (observed: test_call_tool — an in-memory test that never touches subprocesses — failing with ResourceWarning: subprocess 5592 is still running). New design — socket-based, zero sleeps, zero polling Each subprocess in the tree connects a TCP socket back to a listener owned by the test. Two kernel-guaranteed blocking-I/O signals then tell us everything: await listener.accept() blocks until the subprocess connects → proves it started. No polling for "did the file grow yet?". await stream.receive(1) after _terminate_process_tree() raises anyio.EndOfStream (or anyio.BrokenResourceError on Windows, where TerminateJobObject causes an abrupt RST rather than clean FIN) because the kernel closes all file descriptors — including sockets — when a process terminates. This is a direct, OS-level proof that the child is dead. No polling for "did the file stop growing?". Every synchronization point is a blocking read that unblocks on a kernel event. The anyio.fail_after() wrappers are upper bounds for catastrophic failure (subprocess never started / survived termination), not timing assumptions. # Old: fixed sleep, indirect proxy, races against CI load await anyio.sleep(0.5) initial_size = os.path.getsize(marker_file) await anyio.sleep(0.3) assert os.path.getsize(marker_file) > initial_size # fails: assert 0 > 0 # New: blocking accept, kernel-level EOF signal with anyio.fail_after(10): stream = await sock.accept() # blocks until child connects assert await stream.receive(5) == b"alive" await _terminate_process_tree(proc) with pytest.raises((anyio.EndOfStream, anyio.BrokenResourceError)): await stream.receive(1) # child dead → kernel closed its socket Process + transport cleanup _terminate_process_tree kills the OS process group / Job Object but doesn't call process.wait() or close the anyio stdin/stdout pipe wrappers. On Windows, the resulting _WindowsSubprocessTransport / PipeHandle objects were GC'd later, triggering ResourceWarning in whatever test ran next. The SDK's production code at stdio.py:180 avoids this via async with process: — these tests call _terminate_process_tree directly, so they need equivalent cleanup. Fixed with _terminate_and_reap(proc) registered via AsyncExitStack.push_async_callback() immediately after process creation: terminates the tree (no-op if already dead) → await proc.wait() → close stdin/stdout pipes. Bounded by move_on_after(5). All three tests use AsyncExitStack for RAII-style resource management — socket listener, process, and accepted streams each have their cleanup callback pushed at acquisition, so there are no if x is not None: guards in finally blocks, and no coverage pragmas. This made the @pytest.mark.filterwarnings("ignore::ResourceWarning" if win32 ...) decorators unnecessary — removed. Also removed tempfile import and all file I/O escape_path_for_python import (scripts are now injected via {code!r} repr() literals, which eliminates all nested-quote escaping) ~30 lines of boilerplate textwrap.dedent blocks with triply-nested f-string scripts The parent_marker check from test 1, which was a no-op: NamedTemporaryFile(delete=False) creates the file, so os.path.exists() was always True Timing Test main PR #2265 Speedup test_basic_child_process_cleanup 1.91s 0.15s 12.7× test_nested_process_tree 3.41s 0.16s 21.3× test_early_parent_exit 1.70s 0.14s 12.1× Total 7.04s 0.46s 15.3× 30 runs × 4 parallel workers (flake-finder): ~2.1s, 30/30 pass. Hygiene Metric Before After anyio.sleep() calls in TestChildProcessCleanup 12 0 Coverage pragmas in TestChildProcessCleanup 5 0 filterwarnings("ignore::ResourceWarning") decorators 3 0 Diff — +180 −256 (net −76 lines) AI Disclaimer

Open Graph Description: Summary Closes #1775. Supersedes #2071. The three TestChildProcessCleanup tests in tests/client/test_stdio.py failed intermittently on Windows/macOS CI. The root cause was a timing-dependent test d...

X Description: Summary Closes #1775. Supersedes #2071. The three TestChildProcessCleanup tests in tests/client/test_stdio.py failed intermittently on Windows/macOS CI. The root cause was a timing-dependent test d...

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

X: @github

direct link

Domain: github.com

route-pattern/:user_id/:repository/pull/:id/files(.:format)
route-controllerpull_requests
route-actionfiles
fetch-noncev2:d671b026-7dc4-638f-12af-f6b9d53ac454
current-catalog-service-hashae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b
request-idE2E2:129FD3:D0AFDA:116349F:6A5B7056
html-safe-noncea7a11252761410f14a3a8f48d686170c064b77be3065bf20a7a70e19a4eee6f2
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFMkUyOjEyOUZEMzpEMEFGREE6MTE2MzQ5Rjo2QTVCNzA1NiIsInZpc2l0b3JfaWQiOiIzODUzNTAyODA5MzE3Nzk3OTc0IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0=
visitor-hmac1059ba4894ce0b17a4e3783e09793783f90d18be18d2a4e1c914f6297618f462
hovercard-subject-tagpull_request:3379020679
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/2265/files
twitter:imagehttps://avatars.githubusercontent.com/u/224885523?s=400&v=4
twitter:cardsummary_large_image
og:imagehttps://avatars.githubusercontent.com/u/224885523?s=400&v=4
og:image:altSummary Closes #1775. Supersedes #2071. The three TestChildProcessCleanup tests in tests/client/test_stdio.py failed intermittently on Windows/macOS CI. The root cause was a timing-dependent test d...
og:site_nameGitHub
og:typeobject
hostnamegithub.com
expected-hostnamegithub.com
None5290d7e14309ad1e76106a9c4237bd1041517e83ea182c8ab756752cb0c6940b
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
release9c975978430e9ad293956f2bbdaf153b1bd84a99
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/modelcontextprotocol/python-sdk/pull/2265/files#start-of-content
https://github.com/
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fmodelcontextprotocol%2Fpython-sdk%2Fpull%2F2265%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%2F2265%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/2265/files
Reloadhttps://github.com/modelcontextprotocol/python-sdk/pull/2265/files
Reloadhttps://github.com/modelcontextprotocol/python-sdk/pull/2265/files
Please reload this pagehttps://github.com/modelcontextprotocol/python-sdk/pull/2265/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 259 https://github.com/modelcontextprotocol/python-sdk/issues
Pull requests 304 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
maxisbeyhttps://github.com/maxisbey
mainhttps://github.com/modelcontextprotocol/python-sdk/tree/main
fix/flaky-child-process-cleanup-pollinghttps://github.com/modelcontextprotocol/python-sdk/tree/fix/flaky-child-process-cleanup-polling
Conversation 8 https://github.com/modelcontextprotocol/python-sdk/pull/2265
Commits 9 https://github.com/modelcontextprotocol/python-sdk/pull/2265/commits
Checks 33 https://github.com/modelcontextprotocol/python-sdk/pull/2265/checks
Files changed https://github.com/modelcontextprotocol/python-sdk/pull/2265/files
Please reload this pagehttps://github.com/modelcontextprotocol/python-sdk/pull/2265/files
Rewrite TestChildProcessCleanup with socket-based deterministic liveness probe https://github.com/modelcontextprotocol/python-sdk/pull/2265/files#top
Show all changes 9 commits https://github.com/modelcontextprotocol/python-sdk/pull/2265/files
68c6e0b Fix flaky TestChildProcessCleanup: poll for file growth, clean up pro… maxisbey Mar 10, 2026 https://github.com/modelcontextprotocol/python-sdk/pull/2265/commits/68c6e0bb9cd7af0ce5ea05e1447f6141a8d0f702
840c0db Rewrite TestChildProcessCleanup with socket-based deterministic liven… maxisbey Mar 10, 2026 https://github.com/modelcontextprotocol/python-sdk/pull/2265/commits/840c0db3cda505ae5fddc567893c8fc52dadd3f5
324bbc2 Handle BrokenResourceError in _assert_stream_closed for Windows maxisbey Mar 10, 2026 https://github.com/modelcontextprotocol/python-sdk/pull/2265/commits/324bbc27bda777a85c4dc91c5d81b253dfce16ff
160999f Reap process and close pipe transports after _terminate_process_tree maxisbey Mar 11, 2026 https://github.com/modelcontextprotocol/python-sdk/pull/2265/commits/160999f7066205f385d4f1fa712f0cfb27f8e27a
0e80918 Eliminate all coverage pragmas via AsyncExitStack + pytest.raises maxisbey Mar 11, 2026 https://github.com/modelcontextprotocol/python-sdk/pull/2265/commits/0e80918c8ed96219d8ab604ec7f3a4f4f6bbdad4
54295dd Remove now-unnecessary filterwarnings("ignore::ResourceWarning") deco… maxisbey Mar 11, 2026 https://github.com/modelcontextprotocol/python-sdk/pull/2265/commits/54295dd7e750d4018b152daef6da768de4e894a5
a070356 Guard double-terminate on returncode; make _terminate_and_reap idempo… maxisbey Mar 12, 2026 https://github.com/modelcontextprotocol/python-sdk/pull/2265/commits/a0703562708685e15b5e3f90c4bc06004983bdd7
d8c7de2 Drain stdout to EOF so _ProactorReadPipeTransport closes on Windows maxisbey Mar 12, 2026 https://github.com/modelcontextprotocol/python-sdk/pull/2265/commits/d8c7de234b2c1016e820c3d33bb674a5b36309c2
0554fe0 Add returncode property to FallbackProcess; simplify returncode guard maxisbey Mar 12, 2026 https://github.com/modelcontextprotocol/python-sdk/pull/2265/commits/0554fe0e3396f400a5fe616dee4158af87dc09ed
Clear filters https://github.com/modelcontextprotocol/python-sdk/pull/2265/files
Please reload this pagehttps://github.com/modelcontextprotocol/python-sdk/pull/2265/files
Please reload this pagehttps://github.com/modelcontextprotocol/python-sdk/pull/2265/files
utilities.py https://github.com/modelcontextprotocol/python-sdk/pull/2265/files#diff-0adc630bbecb11fd2fe8fc302a21e9afe0ebb9c3ae019a95d9bf84817cf73f61
test_stdio.py https://github.com/modelcontextprotocol/python-sdk/pull/2265/files#diff-49f66b09e4d89b29b440c955c429fe948dbcc3bbb08fffb504cf40be24382352
src/mcp/os/win32/utilities.pyhttps://github.com/modelcontextprotocol/python-sdk/pull/2265/files#diff-0adc630bbecb11fd2fe8fc302a21e9afe0ebb9c3ae019a95d9bf84817cf73f61
View file https://github.com/modelcontextprotocol/python-sdk/blob/0554fe0e3396f400a5fe616dee4158af87dc09ed/src/mcp/os/win32/utilities.py
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/modelcontextprotocol/python-sdk/pull/2265/{{ revealButtonHref }}
https://github.com/modelcontextprotocol/python-sdk/pull/2265/files#diff-0adc630bbecb11fd2fe8fc302a21e9afe0ebb9c3ae019a95d9bf84817cf73f61
https://github.com/modelcontextprotocol/python-sdk/pull/2265/files#diff-0adc630bbecb11fd2fe8fc302a21e9afe0ebb9c3ae019a95d9bf84817cf73f61
Please reload this pagehttps://github.com/modelcontextprotocol/python-sdk/pull/2265/files
Please reload this pagehttps://github.com/modelcontextprotocol/python-sdk/pull/2265/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.