Repository navigation
Reset stale BaseProxy connections on reused MultiProcessShared ports and clean up ML test children - #41668
Reset stale BaseProxy connections on reused MultiProcessShared ports and clean up ML test children#41668damccorm wants to merge 2 commits into
Conversation
|
/gemini review |
There was a problem hiding this comment.
📋 Review Summary
This PR cleanly resolves the leaked MultiProcessShared child processes across ML tests by adding an autouse=True pytest fixture in sdks/python/apache_beam/ml/conftest.py and scoping multiprocessing.active_children() checks in MultiProcessSharedSpawnProcessTest to newly spawned PIDs. The teardown logic properly escalates from terminate() to kill() with bounded join timeouts.
🔍 General Feedback
- In
sdks/python/apache_beam/utils/multi_process_shared_test.py, a few earlier tests inMultiProcessSharedSpawnProcessTest(test_process_exits_on_unsafe_hard_delete,test_process_exits_on_unsafe_hard_delete_with_manager, andtest_zombie_reaping_on_acquire) also select the server process frommultiprocessing.active_children()without filtering out pre-existing PIDs; applying the samebefore_pidspattern there (or insetUp) could further harden the suite if other tests outsideapache_beam/mlever leak child processes.
💡 Tip: Comment /gemini review on this pull request to trigger a new Gemini code review.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #41668 +/- ##
============================================
+ Coverage 58.85% 59.45% +0.59%
- Complexity 2315 5156 +2841
============================================
Files 1135 1471 +336
Lines 170507 187820 +17313
Branches 1499 3828 +2329
============================================
+ Hits 100351 111666 +11315
- Misses 67608 72745 +5137
- Partials 2548 3409 +861
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
…and harden PID filters
|
/gemini review |
There was a problem hiding this comment.
📋 Review Summary
This PR effectively resolves two issues caused by MultiProcessShared(spawn_process=True): stale per-thread BaseProxy sockets on recycled ephemeral ports and leaked child processes across ML test suites. The BaseProxy._address_to_local cache eviction in multi_process_shared.py, the autouse=True cleanup fixture in sdks/python/apache_beam/ml/conftest.py, and the accompanying regression tests in multi_process_shared_test.py are all well-structured, thread-safe, and thoroughly tested.
🔍 General Feedback
- The server identity tracking via
(tag, st_ino, st_mtime_ns, st_ctime_ns)and synchronized eviction ofBaseProxy._address_to_localcleanly handle port reuse across server creations, dead server recovery, andunsafe_hard_delete(). - Filtering
multiprocessing.active_children()againstbefore_pidsacross allMultiProcessSharedSpawnProcessTestcases nicely isolates the test assertions from any external child processes.
💡 Tip: Comment /gemini review on this pull request to trigger a new Gemini code review.
Note: this PR was auto-generated by tooling. Before moving it out of draft, the author has done a full review of the PR, specifically evaluating it on the following criteria:
Full details of the methodology of generation and review can be found here - https://docs.google.com/document/d/14lj7ECVov4T4EdwmIzNlWrRGE3lLDMOwX5xUMGzaLoc/edit?usp=sharing
If you have concerns with the PR or with the underlying methodology used to generate this, please comment below - they will be addressed by the author not the tooling :)
Summary
BaseProxy._address_to_localconnections insdks/python/apache_beam/utils/multi_process_shared.pywheneverMultiProcessSharedconnects to a newly created server (or a different serveraddress_fileidentity) on a recycled(host, port), as well as onunsafe_hard_delete()and dead-server cleanup.autouse=Truepytest fixture (cleanup_spawned_children) insdks/python/apache_beam/ml/conftest.pythat snapshotsmultiprocessing.active_children()before each ML test and terminates/joins any newly spawned live child processes on teardown.test_reused_server_address_resets_stale_proxy_connection) and filtermultiprocessing.active_children()against pre-existing PIDs inMultiProcessSharedSpawnProcessTest(sdks/python/apache_beam/utils/multi_process_shared_test.py).Production Impact
MultiProcessShared(spawn_process=True)— includingRunInferencewithshare_model_across_processes()(enabled by default for large models since Spawn MultiProcessShared RunInference models in dedicated processes (#40673) #40816),ModelManagermodel eviction/reloading, automatic side-input model refresh, and timeout garbage collection (unsafe_hard_delete()).multiprocessing.managers.BaseProxycaches per-thread TCP connections in a process-global_address_to_localdict keyed solely by(host, port), andBaseProxy._decrefonly closestls.connectionon the thread that happens to finalize the last proxy (leaving stale dead sockets attached to other long-lived SDK worker threads indefinitely). When a spawnedMultiProcessSharedserver process exits (e.g., viaunsafe_hard_delete(), model eviction, or crash) and the OS later assigns that recycled ephemeral port to a new server,connect()/acquire()succeed over fresh sockets, but the first RPC call (e.g.,model.predict(...)orget_valid_tag(...)) on a worker thread that previously talked to the old server sends on the stale socket and immediately fails the bundle withRuntimeError: EOFErrorinsidemultiprocessing/managers.py(conn.recv()).MultiProcessSharedtracks the(tag, st_ino, st_mtime_ns, st_ctime_ns)of each connectedaddress_fileper(host, port)and evictsBaseProxy._address_to_local[addr]whenever a newly spawned or distinct server occupies that address (or whenunsafe_hard_delete()/ failed connect removes a dead server), forcing proxies for the new server to establish fresh per-thread connections.Root Cause
PR #40816 (
027f6191a92) enabledspawn_process=TrueforMultiProcessSharedmodels inRunInferenceand addedactive_children()teardown only toRunInferenceBaseTest.tearDown(). This exposed two separate issues:BaseProxy._address_to_localconnections on recycled ephemeral ports: Terminating spawned server processes after each test (or viaunsafe_hard_delete()in production) released their loopback ephemeral ports back to the OS (32768..46883odd ports on Linux), while Prism's reusedLoopbackWorkerthread retained closed sockets inBaseProxy._address_to_local[(host, port)]. Once ~25–30 servers had been cycled on a worker, birthday-paradox port reuse caused healthy tests (test_run_inference_impl_simple_examples_multi_process_shared_multi_copy,test_run_inference_impl_with_keyed_examples_many_mhs_max_models_hint,test_run_inference_unpicklable_data_multi_process_shared, andtest_run_inference_impl_with_maybe_keyed_examples_multi_process_shared) to hit a stale socket onmodel.predict()/get_valid_tag()and fail with silentEOFErrorinmultiprocessing/managers.py.SpawnProcesschildren across non-RunInferenceBaseTestML suites:TFRunInferenceTest,SkLearnRunInferenceTest,VLLMInferenceTest, and other ML tests never terminated their spawnedMultiProcessSharedchild processes until worker exit, leaving 8 leakedSpawnProcesschildren that brokeMultiProcessSharedSpawnProcessTest.test_unsafe_hard_delete_missing_tag_is_noopon Windows 3.13.Recent Occurrences
5 occurrences on
masterin the last 24 hours:2026-11-25) —PreCommit Python ML tests with ML deps installed(3.12test_run_inference_impl_simple_examples_multi_process_shared_multi_copy& 3.13test_run_inference_impl_with_keyed_examples_many_mhs_max_models_hintEOFErrorinmultiprocessing/managers.py:828)2026-11-25) —Python tests(Windows 3.13MultiProcessSharedSpawnProcessTest.test_unsafe_hard_delete_missing_tag_is_noopfound 8 leakedSpawnProcesschildren)2026-11-25) —PreCommit Python ML tests with ML deps installed(3.12test_run_inference_impl_simple_examples_multi_process_shared_multi_copyEOFErrorinmultiprocessing/managers.py:828)2026-11-25) —PreCommit Python Coverage(3.11test_run_inference_unpicklable_data_multi_process_sharedEOFErroronget_valid_taginmultiprocessing/managers.py:822)2026-11-25) —PreCommit Python ML tests with ML deps installed(3.13test_run_inference_impl_with_maybe_keyed_examples_multi_process_sharedEOFErrorinmultiprocessing/connection.py:404)Verification
yapf==0.43.0 --style setup.cfgand syntax withpy_compile.EOFErrordeterministically on a reused worker thread before the fix, and verifiedtest_reused_server_address_resets_stale_proxy_connectionpasses with the fix.pytest -v apache_beam/utils/multi_process_shared_test.py apache_beam/ml/inference/vllm_inference_test.pylocally (34/34 passed) and both previously failingRunInferenceBaseTestcases (test_run_inference_impl_simple_examples_multi_process_shared_multi_copyandtest_run_inference_impl_with_keyed_examples_many_mhs_max_models_hint, 3/3 passed).beam_PreCommit_Python_ML.ymlworkflow run on branch:45645069959(all jobs passed)