Repository navigation
Distributed tests: pick MASTER_PORT outside the ephemeral range, write vision results atomically - #14334
Merged
Conversation
…e vision results atomically The gloo tests released a bind(0) port before rank 0 listened on it. That port is in the ephemeral range gloo's own peer connections draw from, so under xdist another test could take it first (EADDRINUSE in Repo tests (CPU, fsdp2)). tests/_rendezvous_port.py picks from below every default ephemeral range, in a per-xdist-worker slice, and the 18 distributed test files use it. The vision harness also wrote its result file in place while a surviving rank could be killed mid-write; it now writes atomically and reads an unreadable file as no result.
Member
Author
|
@codex review |
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
Codex Review: Didn't find any major issues. Hooray! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
danielhanchen
added a commit
that referenced
this pull request
Oct 28, 2026
…helper (#15679) ## Summary `Repo tests (CPU, gloo)` has failed twice with every rank dying before the test starts: ``` torch.distributed.DistNetworkError: The server socket has failed to listen on any local network address. port: 45095, useIpv6: false, code: -98, name: EADDRINUSE AssertionError: rank exit codes [1, 1] ``` - #15616: `tests/test_tp_native_gloo.py::test_sharded_loading_matches_full_load[phi3-2-None]` - #15632: `test_tp_x_fsdp2_refuses_trainable_head_norms`, port 42541, also in `tests/test_tp_native_gloo.py` #14334 fixed this race by adding `tests/_rendezvous_port.free_port()`: - It draws MASTER_PORT from below the kernel's ephemeral range (32768-60999 on Linux), in a slice per xdist worker. - So neither another worker nor an outgoing gloo connection can take the port between the test releasing it and rank 0 listening on it. That PR moved 21 tests onto the helper, but 15 distributed tests still pick their port with `bind(("127.0.0.1", 0))`, `tests/test_tp_native_gloo.py` among them. Both failing ports above are inside the ephemeral range. ## Change - **14 test files:** their local `_free_port()` now delegates to `tests._rendezvous_port.free_port()`, the same three lines the other 21 files use, and their now-unused `socket` imports are dropped: - `test_tp_native_gloo.py`, `test_pp_native_gloo.py`, `test_pp_fsdp2_gloo.py`, `test_parallel_general_gloo.py` - `test_moe_dw_accumulate.py`, `test_ddp_find_unused_default.py`, `test_ddp_comm_defaults.py` - `test_fast_inference_torchrun_agreement.py`, `test_cudnn_sdpa_grad_layout_dtensor.py` - five under `tests/python/`: block compile DDP, GRPO DDP sync, plain trainer DDP, sentence transformer DDP and FSDP2 save - **`tests/test_token_budget_batches.py`:** the inline `bind(0)` before `MASTER_PORT` now uses the helper. - **`tests/test_rendezvous_port_everywhere.py`** (new): any test outside `tests/studio` that sets up a torch rendezvous (`MASTER_PORT`, `master_port`, `init_process_group` or `TCPStore`) must either define `_free_port` through the helper or not bind port 0 at all. - Studio tests are out of scope: their ports are for HTTP servers they start themselves. - It includes self-tests for both shapes of the mistake, and a check that the helper stays below the ephemeral range for several worker ids. ## Testing - **`tests/test_rendezvous_port_everywhere.py`:** 6 passed. - **Sabotage:** with `tests/test_tp_native_gloo.py` and `tests/test_token_budget_batches.py` restored from main, the guard fails and names both files. - The 14 converted files plus `tests/test_token_budget_batches.py` and the new guard, under `-n 4`: 243 passed, 2 skipped. On main the same 14 files plus `test_token_budget_batches.py` (without the guard): 237 passed, 2 skipped.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Repo tests (CPU, fsdp2), the shard added with #14273, failed on two of the first three PRs that ran it, each time in a test unrelated to the PR:test_fsdp2_native_submesh_gloo.py::test_replicated_grads_average_over_the_model_mesh_onlytest_fsdp2_native_vision_gloo.py::test_without_dummy_vision_desyncs[lora-blocks]Two separate causes:
MASTER_PORTwithbind(("127.0.0.1", 0))and closes the socket before rank 0 listens.bind(0)returns a port from the kernel's ephemeral range (32768-60999 on Linux), which is also where gloo's own peer connections get their local ports. Those connections come from this test and from the other three xdist workers running the same kind of test, so one of them can take the port first.json.dumpleaves an emptyresult.json.<rank>, which the reader parsed and crashed on instead of taking its "no result" branch.Fix
tests/_rendezvous_port.py(new): picks a free port from 20000-31999, below every OS's default ephemeral range (Linux 32768+, macOS and Windows 49152+). Each xdist worker draws from its own 1000-port slice, so two tests running side by side cannot pick the same port either. Taken or TIME_WAIT ports are skipped, andbind(0)remains the fallback if a slice is exhausted._free_port()helper in the 18 distributed test files (the FSDP2, context-parallel and GRPO scoring-head ones that setMASTER_PORT) now returns that port. Their unusedsocketimports are dropped.test_fsdp2_native_vision_gloo.py: each rank writes its result to a temp file andos.replaces it into place, and the reader treats unreadable JSON like a missing file.Test-only; no product code changes.
Testing
tests/test_rendezvous_port.py(new, 3 tests): the port is below 32768 and inside the worker's slice, the no-xdist case uses the first slice, and a port with a listener on it is skipped. With the oldbind(0)helper all three fail.ruff checkpasses on every touched file, and the formatter is a fixed point.unsloth_zoo, which is unrelated to the change.