Skip to content

Distributed tests: pick MASTER_PORT outside the ephemeral range, write vision results atomically - #14334

Merged
danielhanchen merged 1 commit into
mainfrom
ci/dist-test-rendezvous-ports
Oct 20, 2026
Merged

danielhanchen merged 1 commit into
mainfrom
ci/dist-test-rendezvous-ports

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

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:

Two separate causes:

  1. Port race. Each distributed test picks MASTER_PORT with bind(("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.
  2. Half-written result. The vision harness kills a surviving rank as soon as its peer dies (the expected failure mode in the desync cases). A rank killed inside json.dump leaves an empty result.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, and bind(0) remains the fallback if a slice is exhausted.
  • The _free_port() helper in the 18 distributed test files (the FSDP2, context-parallel and GRPO scoring-head ones that set MASTER_PORT) now returns that port. Their unused socket imports are dropped.
  • test_fsdp2_native_vision_gloo.py: each rank writes its result to a temp file and os.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 old bind(0) helper all three fail.
  • ruff check passes on every touched file, and the formatter is a fixed point.
  • The changed distributed tests run on staging CI, linked below. A local run was not usable: this host's spawned ranks import a stale checkout of unsloth_zoo, which is unrelated to the change.

…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.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 20, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-20T11:16:51.836043Z c544504 Manual request
🔒 Security Review ⚠️ Failed 2026-10-20T11:14:43.532209Z c544504 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Hooray!

Reviewed commit: c544504824

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

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
danielhanchen merged commit 846cc5e into main Oct 20, 2026
@danielhanchen
danielhanchen deleted the ci/dist-test-rendezvous-ports branch October 20, 2026 12:06
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant