Skip to content

[c10d] Only honor TORCHELASTIC_USE_AGENT_STORE at the agent's own address - #192113

Closed
tushar00jain wants to merge 4 commits into
gh/tushar00jain/10/basefrom
gh/tushar00jain/10/head
Closed

tushar00jain wants to merge 4 commits into
gh/tushar00jain/10/basefrom
gh/tushar00jain/10/head

Conversation

@tushar00jain

@tushar00jain tushar00jain commented Aug 4, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

Summary:
_create_c10d_store() began with

if _torchelastic_use_agent_store():
    return TCPStore(hostname, port, world_size, is_master=False, timeout)

so under torchrun every rank got a client-only TCPStore for whatever host and
port it had been handed. torchrun's elastic agent exports
TORCHELASTIC_USE_AGENT_STORE=True into every worker it spawns, alongside
MASTER_ADDR and MASTER_PORT, and the flag promises exactly one thing: the agent
hosts a store at that endpoint. _create_c10d_store applied it to every
endpoint. So any caller building a private store on a port of its own choosing
-- a library making a stateless group, or a script calling
init_process_group(init_method="tcp://host:port") -- ended up with a client on
every rank, no rank ever started a server, and every rank sat in connect-retry
against nothing until the store timeout expired: thirty minutes on gloo, ten on
nccl. From the outside that is an unexplained hang at startup with no error and
nothing in the logs, on a code path that works perfectly the moment the same
program is run without torchrun.

Torch already knew the address had to match. TCPStore's C++ constructor only
forgives a failed server bind when useAgentStore && masterPort == opts.port --
the same guard, one layer down. The Python layer was contradicting the C++
layer: rendezvous.py behaved as though the agent serves every address, while
TCPStore.cpp assumes it serves only its own.

Found by root-causing a hang rather than by reading. vLLM's
stateless_init_torch_distributed_process_group hangs when called from inside a
torchrun-managed process, on both the gloo and the nccl arms and identically on
stock nccl and on nccl2. Isolating it ruled out everything torchrun-shaped in
turn -- RANK and WORLD_SIZE, MASTER_PORT collisions, store key collisions --
and left one variable: popping TORCHELASTIC_USE_AGENT_STORE out of the
environment makes the identical run succeed. That pointed at
torch.distributed.rendezvous, which is where vLLM was getting its store, and
from there at this branch.

This is a stock defect. rendezvous.py is stock torch, the elastic agent that
sets the variable is stock torch, and the hang reproduces on a stock
init_process_group(init_method="tcp://...") under stock torchrun with nothing
from the migration loaded at all. No part of it requires nccl2.

It is also only fixable here, which is the whole reason it belongs in c10d
rather than downstream. vLLM has already fixed itself, independently, by
building the TCPStore directly instead of going through rendezvous -- and that
bypass is byte-for-byte the branch _create_c10d_store now takes for a private
address, so on a torch carrying this commit the two are equivalent and the
workaround is functionally redundant. It was nonetheless deliberately left in
place, because vLLM supports torch releases that will not carry this fix for a
long time and on a fixed torch it is a no-op that costs nothing. What a
downstream patch cannot close is the broader exposure: an API server started
under torchrun whose workers call
init_distributed_environment(distributed_init_method="tcp://...") reaches this
code through init_process_group, which builds its store via
_create_c10d_store, and no amount of patching a stateless-group helper reaches
that path. Every framework offering a "connect to this address" knob has the
same shape, so the guard has to live in torch.

The fix is a predicate, _agent_store_serves(hostname, port), that honors
TORCHELASTIC_USE_AGENT_STORE only when the hostname and port it is asked about
are the agent's own MASTER_ADDR and MASTER_PORT. _create_c10d_store gates on
that instead of on the raw env read, and the docstring is corrected to describe
what the code now does. _torchelastic_use_agent_store() itself stays, since it
is the raw env read and is referenced elsewhere in the ecosystem. This is
TCPStore.cpp's existing guard extended to the hostname, which the Python layer
has in hand and the C++ layer does not.

Comparing the hostname as a string is safe in both directions. Everything that
legitimately wants the agent's store arrives through env://, and
_env_rendezvous_handler reads MASTER_ADDR verbatim out of the same environment
the agent wrote it into, so for that path the comparison is exact by
construction. A caller who instead hand-writes tcp://:<MASTER_PORT>
and so trips the string mismatch does not get the old broken behaviour but the
correct one: rank 0 takes the is_master=True branch, its bind fails because the
agent already holds that port, and TCPStore.cpp's port-only guard forgives the
failure and turns it into a client -- which is precisely what the old code did
directly. The degradation is into a slower path, not into a broken one.

Test Plan:
Two regression tests in test_store.py::RendezvousTCPTest, next to the other
dist.rendezvous("tcp://...") tests and in the same create_tcp_url() /
common.find_free_port() idiom. They are a matched pair on purpose, so that the
fix cannot quietly degrade into deleting the feature:
test_agent_store_ignored_for_other_address sets the agent environment but
points it at a different port and requires the private store to come up and
round-trip a key, while test_agent_store_honored_for_master_address sets it
matching, with nothing listening, and requires a DistNetworkError rather than
some rank silently starting a server.

python test/distributed/test_store.py -v -k agent_store
test_agent_store (LibUvTCPStoreTest.test_agent_store) ... ok
test_agent_store_honored_for_master_address (RendezvousTCPTest...) ... ok
test_agent_store_ignored_for_other_address (RendezvousTCPTest...) ... ok
test_agent_store (TCPStoreTest.test_agent_store) ... ok
Ran 4 tests in 4.175s
OK

Pre-fix the same selection is 199.719 s and FAILED (errors=1), with
test_agent_store_ignored_for_other_address raising DistNetworkError: The client
socket has timed out after 10000ms while trying to connect to (localhost,
37515). Neither new test is decorated with @retry_on_connect_failures, and that
is deliberate: that helper's default connect_errors=(ADDRESS_IN_USE) is missing
its trailing comma, so it is a plain string rather than a tuple, the membership
test iterates it character by character, and consequently almost any
RuntimeError matches and is retried ten times, each attempt paying its full
timeout. That is what turned the genuine pre-fix failure here into a roughly
190-second hang with the real error buried behind "Failing after 10 retries".
It is a pre-existing wart in common_utils.py whose blast radius covers every
test currently using the decorator, so it is left untouched here and filed
separately.

End-to-end under real torchrun, driven by triage/bug14_private_store_torchrun.py
(stock torch, no vLLM), which can monkeypatch the predicate back to its legacy
form with --legacy so both behaviours are observable on one binary. Two modes:
rendezvous (what vLLM's stateless helper did) and init_pg
(init_process_group(init_method="tcp://..."), the path vLLM cannot bypass):

torchrun --nproc_per_node=2 --master_port=29502 \
  triage/bug14_private_store_torchrun.py <mode> --port 29783 \
  --store-timeout 15 [--legacy]
rendezvous --legacy : [rank1] RAISED after 26.1s: DistNetworkError
rendezvous (fixed)  : [rank0] OK in 0.19s / [rank1] OK in 0.01s,
                      allreduce -> 2.0
init_pg    --legacy : [rank1] RAISED after 20.8s: DistNetworkError
init_pg    (fixed)  : [rank0] OK in 0.05s / [rank1] OK in 0.04s,
                      allreduce -> 2.0

With stock timeouts rather than the repro's fifteen seconds the legacy arms
never finish at all. A four-rank env:// run confirms the genuine agent-store
path is untouched: _agent_store_serves reports True on every rank and the
allreduce returns 4.0.

Verified on 4x H100, CUDA 13.0, NCCL 2.29.7, against a freshly rebuilt HEAD:
the pre-existing install predated the commits already landed in this stack, so
unmodified HEAD was rebuilt first to make the before-numbers honest
measurements rather than comparisons against a stale binary. 879 tests across
six suites with zero failures, with only the skips those suites already had:

test_c10d_nccl.py    Ran 285 tests in 1659.524s  OK (skipped=15)
test_c10d_gloo.py    Ran 289 tests in 1089.249s  OK (skipped=4)
test_c10d_nccl2.py   Ran 20 tests in 102.125s    OK
test_c10d_common.py  Ran 72 tests in 117.529s    OK
test_store.py        Ran 140 tests in 23.000s    OK (skipped=12)
test_fake_pg.py      Ran 73 tests in 1.543s      OK

Authored with the assistance of an AI coding agent.

[ghstack-poisoned]
@pytorch-bot pytorch-bot Bot added the ciflow/torchtitan Run TorchTitan integration tests label Aug 4, 2026
@pytorch-bot

pytorch-bot Bot commented Aug 4, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/192113

Note: Links to docs will display an error until the docs builds have been completed.

✅ No Failures

As of commit 9b00d83 with merge base f3849a4 (image):
💚 Looks good so far! There are no failures yet. 💚

This comment was automatically generated by Dr. CI and updates every 15 minutes.

[ghstack-poisoned]
@tushar00jain
tushar00jain requested a review from d4l3k August 4, 2026 23:52

@d4l3k d4l3k left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-approving due to switching to ghstack from sl

@tushar00jain

Copy link
Copy Markdown
Contributor Author

@pytorchbot merge

@pytorch-bot pytorch-bot Bot added the ciflow/trunk Trigger trunk jobs on your pull request label Aug 5, 2026
@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge failed

Reason: This PR needs a release notes: label
If your changes are user facing and intended to be a part of release notes, please use a label starting with release notes:.

If not, please add the topic: not user facing label.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "topic: not user facing"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

Details for Dev Infra team Raised by workflow job

@tushar00jain

Copy link
Copy Markdown
Contributor Author

@pytorchbot merge

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge failed

Reason: 1 jobs have failed, first few of them are: trunk / linux-jammy-cuda13.0-py3.10-gcc11 / test (distributed, 10, 10, mt-l-x86iavx512-45-172-t4-4)

Details for Dev Infra team Raised by workflow job

[ghstack-poisoned]
@tushar00jain

Copy link
Copy Markdown
Contributor Author

@pytorchbot merge

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge failed

Reason: 1 jobs have failed, first few of them are: Check mergeability of ghstack PR / ghstack-mergeability-check

Details for Dev Infra team Raised by workflow job

[ghstack-poisoned]
@tushar00jain

Copy link
Copy Markdown
Contributor Author

@pytorchbot merge

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

@github-actions
github-actions Bot deleted the gh/tushar00jain/10/head branch October 31, 2026 01:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/torchtitan Run TorchTitan integration tests ciflow/trunk Trigger trunk jobs on your pull request Merged release notes: distributed (c10d) release notes category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants