Skip to content

[c10d] Fix flight recorder attach for the default nccl name, and a completion race - #192858

Closed
tushar00jain wants to merge 3 commits into
gh/tushar00jain/17/basefrom
gh/tushar00jain/17/head
Closed

tushar00jain wants to merge 3 commits into
gh/tushar00jain/17/basefrom
gh/tushar00jain/17/head

Conversation

@tushar00jain

@tushar00jain tushar00jain commented Aug 10, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

Two defects in the flight recorder, found in review. Separate from the commits
below only because they do not fit inside the 2000 LOC review limit.

The backend string "nccl" had two sources of truth for what it builds, and only
one of them read TORCH_DIST_USE_NCCL2. _register_builtin_nccl_backend picks its
creator from that flag; _FR_SELF_RECORDING_BACKENDS hard-coded Backend.NCCL as
self-recording regardless. They agree today by luck, because the flag defaults
off and "nccl" builds stock ProcessGroupNCCL, which does record natively. The
moment the default flips, "nccl" builds nccl2 and the flight recorder skips it as
self-recording -- so the default path loses the feature entirely, and silently.
Existing tests never caught it because they all request "nccl2" explicitly.

Rather than special-case the name, the registration now records what it actually
built: the set is derived from the creator that was chosen, keyed off creator
identity rather than re-reading the env, so flipping the default is still a
one-line change to the existing ternary and there is no second site to keep in
sync. Ordering holds because _ensure_backend_registered runs well before the
attach decision, for both new_group and split_group. "nccl-legacy" is added to
the set for the same reason -- it builds stock, so the hook it was getting could
never record anything.

The second is a race. A completion is delivered with the Work, and the hook can
only map a Work to its trace entry from the post-hook, which is the one place
op_id and the Work are both visible. A fast collective can be completed by the
backend's watchdog after it is enqueued but before that post-hook runs, and the
callback was then dropped on the floor -- leaving a healthy op unretired and
reading "scheduled" forever, which is precisely the false hang signal this
feature exists to rule out.

A completion that arrives early is no longer a special case. The post-hook, once
it has registered the mapping, simply asks the Work whether it already finished
successfully and retires it if so. There is then no interval in which a
completion can be missed: if it landed before registration this catches it, and
if after, the callback finds the mapping.

Retirement is claiming rather than checking, so it happens exactly once however
the two interleave -- the lookup and both erases happen under one lock, and the
loser takes the same early return a Work that was never ours would. An earlier
version of this fix stashed early completions keyed by Work* and replayed them,
bounded three ways. That was a workaround: Work* is not a stable identity, since
the allocator can reuse the address once the original is destroyed, and a stale
entry would then report a different op as completed while it was still in
flight -- a false "completed", which is worse than the false "hang" being fixed.
The bounds only shrank the window in which the key could lie.

One consequence worth knowing: asking the Work also advances its status, so a
short healthy collective now usually retires on the issuing thread rather than
from the watchdog push, and pays one event query per op on the issue path.

Hook maps become ordered so hooks fire by id. That is what lets the race be
tested deterministically rather than with a sleep, and the flight recorder hook's
id already assumed ordering against user hooks.

Test Plan:

The race test does not sleep. A user post-hook with a lower id holds the window
open and, inside it, issues a sentinel collective behind the target on the same
stream queue; nccl2 pops that queue in order, so a retired sentinel proves the
target's completion was already delivered. It then asserts the target is not yet
retired -- i.e. the race really occurred -- and that it is retired, completed and
still carries a duration once the post-hook returns. Without the fix the last
assertion fails, since the completion fires once per Work and no second one is
coming.

The attach test asserts on what was built rather than on the env var, so it holds
either side of the default flip.

python test/distributed/test_c10d_fr_hook.py
python test/distributed/test_c10d_nccl2.py
python test/distributed/test_c10d_nccl.py NCCLTraceTest
python test/distributed/flight_recorder/test_fr_analysis.py
python test/distributed/test_c10d_hooks.py
python test/distributed/test_c10d_nan_check.py
Ran 63 tests in 323.043s  OK (skipped=9)
Ran 50 tests in 345.898s  OK (skipped=1)
Ran 61 tests in 496.705s  OK
Ran 23 tests in   1.790s  OK
Ran  2 tests in   7.803s  OK
Ran 20 tests in 102.351s  OK

The last two cover the other consumers of the hook plumbing whose map ordering
changed.

lintrunner --revision HEAD: clean.

Authored with the assistance of an AI coding agent.

[ghstack-poisoned]
@pytorch-bot

pytorch-bot Bot commented Aug 10, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

✅ You can merge normally! (2 Unrelated Failures)

As of commit bc31c34 with merge base b9410b6 (image):

BROKEN TRUNK - The following jobs failed but were present on the merge base:

👉 Rebase onto the `viable/strict` branch to avoid these failures

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

[ghstack-poisoned]
tushar00jain added a commit that referenced this pull request Aug 11, 2026
…mpletion race

Two defects in the flight recorder, found in review. Separate from the commits
below only because they do not fit inside the 2000 LOC review limit.

The backend string "nccl" had two sources of truth for what it builds, and only
one of them read TORCH_DIST_USE_NCCL2. _register_builtin_nccl_backend picks its
creator from that flag; _FR_SELF_RECORDING_BACKENDS hard-coded Backend.NCCL as
self-recording regardless. They agree today by luck, because the flag defaults
off and "nccl" builds stock ProcessGroupNCCL, which does record natively. The
moment the default flips, "nccl" builds nccl2 and the flight recorder skips it as
self-recording -- so the default path loses the feature entirely, and silently.
Existing tests never caught it because they all request "nccl2" explicitly.

Rather than special-case the name, the registration now records what it actually
built: the set is derived from the creator that was chosen, keyed off creator
identity rather than re-reading the env, so flipping the default is still a
one-line change to the existing ternary and there is no second site to keep in
sync. Ordering holds because _ensure_backend_registered runs well before the
attach decision, for both new_group and split_group. "nccl-legacy" is added to
the set for the same reason -- it builds stock, so the hook it was getting could
never record anything.

The second is a race. A completion is delivered with the Work, and the hook can
only map a Work to its trace entry from the post-hook, which is the one place
op_id and the Work are both visible. A fast collective can be completed by the
backend's watchdog after it is enqueued but before that post-hook runs, and the
callback was then dropped on the floor -- leaving a healthy op unretired and
reading "scheduled" forever, which is precisely the false hang signal this
feature exists to rule out.

A completion that arrives early is no longer a special case. The post-hook, once
it has registered the mapping, simply asks the Work whether it already finished
successfully and retires it if so. There is then no interval in which a
completion can be missed: if it landed before registration this catches it, and
if after, the callback finds the mapping.

Retirement is claiming rather than checking, so it happens exactly once however
the two interleave -- the lookup and both erases happen under one lock, and the
loser takes the same early return a Work that was never ours would. An earlier
version of this fix stashed early completions keyed by Work* and replayed them,
bounded three ways. That was a workaround: Work* is not a stable identity, since
the allocator can reuse the address once the original is destroyed, and a stale
entry would then report a *different* op as completed while it was still in
flight -- a false "completed", which is worse than the false "hang" being fixed.
The bounds only shrank the window in which the key could lie.

One consequence worth knowing: asking the Work also advances its status, so a
short healthy collective now usually retires on the issuing thread rather than
from the watchdog push, and pays one event query per op on the issue path.

Hook maps become ordered so hooks fire by id. That is what lets the race be
tested deterministically rather than with a sleep, and the flight recorder hook's
id already assumed ordering against user hooks.

Test Plan:

The race test does not sleep. A user post-hook with a lower id holds the window
open and, inside it, issues a sentinel collective behind the target on the same
stream queue; nccl2 pops that queue in order, so a retired sentinel proves the
target's completion was already delivered. It then asserts the target is not yet
retired -- i.e. the race really occurred -- and that it is retired, completed and
still carries a duration once the post-hook returns. Without the fix the last
assertion fails, since the completion fires once per Work and no second one is
coming.

The attach test asserts on what was built rather than on the env var, so it holds
either side of the default flip.

```
python test/distributed/test_c10d_fr_hook.py
python test/distributed/test_c10d_nccl2.py
python test/distributed/test_c10d_nccl.py NCCLTraceTest
python test/distributed/flight_recorder/test_fr_analysis.py
python test/distributed/test_c10d_hooks.py
python test/distributed/test_c10d_nan_check.py
```
```
Ran 63 tests in 323.043s  OK (skipped=9)
Ran 50 tests in 345.898s  OK (skipped=1)
Ran 61 tests in 496.705s  OK
Ran 23 tests in   1.790s  OK
Ran  2 tests in   7.803s  OK
Ran 20 tests in 102.351s  OK
```
The last two cover the other consumers of the hook plumbing whose map ordering
changed.

lintrunner --revision HEAD: clean.

Authored with the assistance of an AI coding agent.

ghstack-source-id: f0cba11
Pull-Request: #192858
tushar00jain added a commit that referenced this pull request Aug 11, 2026
…mpletion race

Two defects in the flight recorder, found in review. Separate from the commits
below only because they do not fit inside the 2000 LOC review limit.

The backend string "nccl" had two sources of truth for what it builds, and only
one of them read TORCH_DIST_USE_NCCL2. _register_builtin_nccl_backend picks its
creator from that flag; _FR_SELF_RECORDING_BACKENDS hard-coded Backend.NCCL as
self-recording regardless. They agree today by luck, because the flag defaults
off and "nccl" builds stock ProcessGroupNCCL, which does record natively. The
moment the default flips, "nccl" builds nccl2 and the flight recorder skips it as
self-recording -- so the default path loses the feature entirely, and silently.
Existing tests never caught it because they all request "nccl2" explicitly.

Rather than special-case the name, the registration now records what it actually
built: the set is derived from the creator that was chosen, keyed off creator
identity rather than re-reading the env, so flipping the default is still a
one-line change to the existing ternary and there is no second site to keep in
sync. Ordering holds because _ensure_backend_registered runs well before the
attach decision, for both new_group and split_group. "nccl-legacy" is added to
the set for the same reason -- it builds stock, so the hook it was getting could
never record anything.

The second is a race. A completion is delivered with the Work, and the hook can
only map a Work to its trace entry from the post-hook, which is the one place
op_id and the Work are both visible. A fast collective can be completed by the
backend's watchdog after it is enqueued but before that post-hook runs, and the
callback was then dropped on the floor -- leaving a healthy op unretired and
reading "scheduled" forever, which is precisely the false hang signal this
feature exists to rule out.

A completion that arrives early is no longer a special case. The post-hook, once
it has registered the mapping, simply asks the Work whether it already finished
successfully and retires it if so. There is then no interval in which a
completion can be missed: if it landed before registration this catches it, and
if after, the callback finds the mapping.

Retirement is claiming rather than checking, so it happens exactly once however
the two interleave -- the lookup and both erases happen under one lock, and the
loser takes the same early return a Work that was never ours would. An earlier
version of this fix stashed early completions keyed by Work* and replayed them,
bounded three ways. That was a workaround: Work* is not a stable identity, since
the allocator can reuse the address once the original is destroyed, and a stale
entry would then report a *different* op as completed while it was still in
flight -- a false "completed", which is worse than the false "hang" being fixed.
The bounds only shrank the window in which the key could lie.

One consequence worth knowing: asking the Work also advances its status, so a
short healthy collective now usually retires on the issuing thread rather than
from the watchdog push, and pays one event query per op on the issue path.

Hook maps become ordered so hooks fire by id. That is what lets the race be
tested deterministically rather than with a sleep, and the flight recorder hook's
id already assumed ordering against user hooks.

Test Plan:

The race test does not sleep. A user post-hook with a lower id holds the window
open and, inside it, issues a sentinel collective behind the target on the same
stream queue; nccl2 pops that queue in order, so a retired sentinel proves the
target's completion was already delivered. It then asserts the target is not yet
retired -- i.e. the race really occurred -- and that it is retired, completed and
still carries a duration once the post-hook returns. Without the fix the last
assertion fails, since the completion fires once per Work and no second one is
coming.

The attach test asserts on what was built rather than on the env var, so it holds
either side of the default flip.

```
python test/distributed/test_c10d_fr_hook.py
python test/distributed/test_c10d_nccl2.py
python test/distributed/test_c10d_nccl.py NCCLTraceTest
python test/distributed/flight_recorder/test_fr_analysis.py
python test/distributed/test_c10d_hooks.py
python test/distributed/test_c10d_nan_check.py
```
```
Ran 63 tests in 323.043s  OK (skipped=9)
Ran 50 tests in 345.898s  OK (skipped=1)
Ran 61 tests in 496.705s  OK
Ran 23 tests in   1.790s  OK
Ran  2 tests in   7.803s  OK
Ran 20 tests in 102.351s  OK
```
The last two cover the other consumers of the hook plumbing whose map ordering
changed.

lintrunner --revision HEAD: clean.

Authored with the assistance of an AI coding agent.

ghstack-source-id: f0cba11
Pull-Request: #192858

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

lgtm

@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 11, 2026
@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, 5, 10, lf-l-x86iavx512-45-172-t4-4)

Details for Dev Infra team Raised by workflow job

[ghstack-poisoned]
tushar00jain added a commit that referenced this pull request Aug 11, 2026
…mpletion race

Two defects in the flight recorder, found in review. Separate from the commits
below only because they do not fit inside the 2000 LOC review limit.

The backend string "nccl" had two sources of truth for what it builds, and only
one of them read TORCH_DIST_USE_NCCL2. _register_builtin_nccl_backend picks its
creator from that flag; _FR_SELF_RECORDING_BACKENDS hard-coded Backend.NCCL as
self-recording regardless. They agree today by luck, because the flag defaults
off and "nccl" builds stock ProcessGroupNCCL, which does record natively. The
moment the default flips, "nccl" builds nccl2 and the flight recorder skips it as
self-recording -- so the default path loses the feature entirely, and silently.
Existing tests never caught it because they all request "nccl2" explicitly.

Rather than special-case the name, the registration now records what it actually
built: the set is derived from the creator that was chosen, keyed off creator
identity rather than re-reading the env, so flipping the default is still a
one-line change to the existing ternary and there is no second site to keep in
sync. Ordering holds because _ensure_backend_registered runs well before the
attach decision, for both new_group and split_group. "nccl-legacy" is added to
the set for the same reason -- it builds stock, so the hook it was getting could
never record anything.

The second is a race. A completion is delivered with the Work, and the hook can
only map a Work to its trace entry from the post-hook, which is the one place
op_id and the Work are both visible. A fast collective can be completed by the
backend's watchdog after it is enqueued but before that post-hook runs, and the
callback was then dropped on the floor -- leaving a healthy op unretired and
reading "scheduled" forever, which is precisely the false hang signal this
feature exists to rule out.

A completion that arrives early is no longer a special case. The post-hook, once
it has registered the mapping, simply asks the Work whether it already finished
successfully and retires it if so. There is then no interval in which a
completion can be missed: if it landed before registration this catches it, and
if after, the callback finds the mapping.

Retirement is claiming rather than checking, so it happens exactly once however
the two interleave -- the lookup and both erases happen under one lock, and the
loser takes the same early return a Work that was never ours would. An earlier
version of this fix stashed early completions keyed by Work* and replayed them,
bounded three ways. That was a workaround: Work* is not a stable identity, since
the allocator can reuse the address once the original is destroyed, and a stale
entry would then report a *different* op as completed while it was still in
flight -- a false "completed", which is worse than the false "hang" being fixed.
The bounds only shrank the window in which the key could lie.

One consequence worth knowing: asking the Work also advances its status, so a
short healthy collective now usually retires on the issuing thread rather than
from the watchdog push, and pays one event query per op on the issue path.

Hook maps become ordered so hooks fire by id. That is what lets the race be
tested deterministically rather than with a sleep, and the flight recorder hook's
id already assumed ordering against user hooks.

Also fixes the capture guard for tensorless ops. barrier is the only collective
that reaches the hook with no tensors, so the device could not be resolved from
them, and "no device" was being read as "not capturing" -- so a barrier issued
inside a graph capture was recorded, and the completion check above then queried
an event on a capturing stream and invalidated it. Latent until that check
existed; before it, recording such an op was harmless. The guard now asks every
device the group serves rather than only the one named by the op's tensors, and
treats an unresolvable device as capturing: recording costs the user's graph,
while not recording costs one trace entry, so the two errors are not worth the
same. This affected nccl-lazy as well as nccl2, both of which support a captured
barrier.

Test Plan:

The race test does not sleep. A user post-hook with a lower id holds the window
open and, inside it, issues a sentinel collective behind the target on the same
stream queue; nccl2 pops that queue in order, so a retired sentinel proves the
target's completion was already delivered. It then asserts the target is not yet
retired -- i.e. the race really occurred -- and that it is retired, completed and
still carries a duration once the post-hook returns. Without the fix the last
assertion fails, since the completion fires once per Work and no second one is
coming.

The attach test asserts on what was built rather than on the env var, so it holds
either side of the default flip.

```
python test/distributed/test_c10d_fr_hook.py
python test/distributed/test_c10d_nccl2.py
python test/distributed/test_c10d_nccl.py NCCLTraceTest
python test/distributed/flight_recorder/test_fr_analysis.py
python test/distributed/test_c10d_hooks.py
python test/distributed/test_c10d_nan_check.py
```
```
Ran 63 tests in 323.043s  OK (skipped=9)
Ran 50 tests in 345.898s  OK (skipped=1)
Ran 61 tests in 496.705s  OK
Ran 23 tests in   1.790s  OK
Ran  2 tests in   7.803s  OK
Ran 20 tests in 102.351s  OK
```
The last two cover the other consumers of the hook plumbing whose map ordering
changed.

lintrunner --revision HEAD: clean.

Authored with the assistance of an AI coding agent.

ghstack-source-id: bbd0769
Pull-Request: #192858
@tushar00jain

Copy link
Copy Markdown
Contributor Author

@pytorchbot merge

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge failed

Reason: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self-signed certificate (_ssl.c:1017)>

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 (pr_time_benchmarks, 1, 1, mt-l-bx86iavx512-94-344-t4-8)

Details for Dev Infra team Raised by workflow job

@tushar00jain

Copy link
Copy Markdown
Contributor Author

@pytorchbot merge -i

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your change will be merged while ignoring the following 2 checks: trunk / linux-jammy-cuda13.2-py3.10-gcc11 / test (pr_time_benchmarks, 1, 1, mt-l-bx86iavx512-94-344-t4-8), trunk / linux-jammy-cuda13.0-py3.10-gcc11 / test (pr_time_benchmarks, 1, 1, mt-l-bx86iavx512-94-344-t4-8)

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

ankitbhrdwj pushed a commit to ankitbhrdwj/pytorch that referenced this pull request Aug 12, 2026
…mpletion race (pytorch#192858)

Two defects in the flight recorder, found in review. Separate from the commits
below only because they do not fit inside the 2000 LOC review limit.

The backend string "nccl" had two sources of truth for what it builds, and only
one of them read TORCH_DIST_USE_NCCL2. _register_builtin_nccl_backend picks its
creator from that flag; _FR_SELF_RECORDING_BACKENDS hard-coded Backend.NCCL as
self-recording regardless. They agree today by luck, because the flag defaults
off and "nccl" builds stock ProcessGroupNCCL, which does record natively. The
moment the default flips, "nccl" builds nccl2 and the flight recorder skips it as
self-recording -- so the default path loses the feature entirely, and silently.
Existing tests never caught it because they all request "nccl2" explicitly.

Rather than special-case the name, the registration now records what it actually
built: the set is derived from the creator that was chosen, keyed off creator
identity rather than re-reading the env, so flipping the default is still a
one-line change to the existing ternary and there is no second site to keep in
sync. Ordering holds because _ensure_backend_registered runs well before the
attach decision, for both new_group and split_group. "nccl-legacy" is added to
the set for the same reason -- it builds stock, so the hook it was getting could
never record anything.

The second is a race. A completion is delivered with the Work, and the hook can
only map a Work to its trace entry from the post-hook, which is the one place
op_id and the Work are both visible. A fast collective can be completed by the
backend's watchdog after it is enqueued but before that post-hook runs, and the
callback was then dropped on the floor -- leaving a healthy op unretired and
reading "scheduled" forever, which is precisely the false hang signal this
feature exists to rule out.

A completion that arrives early is no longer a special case. The post-hook, once
it has registered the mapping, simply asks the Work whether it already finished
successfully and retires it if so. There is then no interval in which a
completion can be missed: if it landed before registration this catches it, and
if after, the callback finds the mapping.

Retirement is claiming rather than checking, so it happens exactly once however
the two interleave -- the lookup and both erases happen under one lock, and the
loser takes the same early return a Work that was never ours would. An earlier
version of this fix stashed early completions keyed by Work* and replayed them,
bounded three ways. That was a workaround: Work* is not a stable identity, since
the allocator can reuse the address once the original is destroyed, and a stale
entry would then report a *different* op as completed while it was still in
flight -- a false "completed", which is worse than the false "hang" being fixed.
The bounds only shrank the window in which the key could lie.

One consequence worth knowing: asking the Work also advances its status, so a
short healthy collective now usually retires on the issuing thread rather than
from the watchdog push, and pays one event query per op on the issue path.

Hook maps become ordered so hooks fire by id. That is what lets the race be
tested deterministically rather than with a sleep, and the flight recorder hook's
id already assumed ordering against user hooks.

Test Plan:

The race test does not sleep. A user post-hook with a lower id holds the window
open and, inside it, issues a sentinel collective behind the target on the same
stream queue; nccl2 pops that queue in order, so a retired sentinel proves the
target's completion was already delivered. It then asserts the target is not yet
retired -- i.e. the race really occurred -- and that it is retired, completed and
still carries a duration once the post-hook returns. Without the fix the last
assertion fails, since the completion fires once per Work and no second one is
coming.

The attach test asserts on what was built rather than on the env var, so it holds
either side of the default flip.

```
python test/distributed/test_c10d_fr_hook.py
python test/distributed/test_c10d_nccl2.py
python test/distributed/test_c10d_nccl.py NCCLTraceTest
python test/distributed/flight_recorder/test_fr_analysis.py
python test/distributed/test_c10d_hooks.py
python test/distributed/test_c10d_nan_check.py
```
```
Ran 63 tests in 323.043s  OK (skipped=9)
Ran 50 tests in 345.898s  OK (skipped=1)
Ran 61 tests in 496.705s  OK
Ran 23 tests in   1.790s  OK
Ran  2 tests in   7.803s  OK
Ran 20 tests in 102.351s  OK
```
The last two cover the other consumers of the hook plumbing whose map ordering
changed.

lintrunner --revision HEAD: clean.

Authored with the assistance of an AI coding agent.
Pull Request resolved: pytorch#192858
Approved by: https://github.com/d4l3k
ghstack dependencies: pytorch#192233, pytorch#192837
gavinwang269 pushed a commit to gavinwang269/pytorch that referenced this pull request Aug 13, 2026
…mpletion race (pytorch#192858)

Two defects in the flight recorder, found in review. Separate from the commits
below only because they do not fit inside the 2000 LOC review limit.

The backend string "nccl" had two sources of truth for what it builds, and only
one of them read TORCH_DIST_USE_NCCL2. _register_builtin_nccl_backend picks its
creator from that flag; _FR_SELF_RECORDING_BACKENDS hard-coded Backend.NCCL as
self-recording regardless. They agree today by luck, because the flag defaults
off and "nccl" builds stock ProcessGroupNCCL, which does record natively. The
moment the default flips, "nccl" builds nccl2 and the flight recorder skips it as
self-recording -- so the default path loses the feature entirely, and silently.
Existing tests never caught it because they all request "nccl2" explicitly.

Rather than special-case the name, the registration now records what it actually
built: the set is derived from the creator that was chosen, keyed off creator
identity rather than re-reading the env, so flipping the default is still a
one-line change to the existing ternary and there is no second site to keep in
sync. Ordering holds because _ensure_backend_registered runs well before the
attach decision, for both new_group and split_group. "nccl-legacy" is added to
the set for the same reason -- it builds stock, so the hook it was getting could
never record anything.

The second is a race. A completion is delivered with the Work, and the hook can
only map a Work to its trace entry from the post-hook, which is the one place
op_id and the Work are both visible. A fast collective can be completed by the
backend's watchdog after it is enqueued but before that post-hook runs, and the
callback was then dropped on the floor -- leaving a healthy op unretired and
reading "scheduled" forever, which is precisely the false hang signal this
feature exists to rule out.

A completion that arrives early is no longer a special case. The post-hook, once
it has registered the mapping, simply asks the Work whether it already finished
successfully and retires it if so. There is then no interval in which a
completion can be missed: if it landed before registration this catches it, and
if after, the callback finds the mapping.

Retirement is claiming rather than checking, so it happens exactly once however
the two interleave -- the lookup and both erases happen under one lock, and the
loser takes the same early return a Work that was never ours would. An earlier
version of this fix stashed early completions keyed by Work* and replayed them,
bounded three ways. That was a workaround: Work* is not a stable identity, since
the allocator can reuse the address once the original is destroyed, and a stale
entry would then report a *different* op as completed while it was still in
flight -- a false "completed", which is worse than the false "hang" being fixed.
The bounds only shrank the window in which the key could lie.

One consequence worth knowing: asking the Work also advances its status, so a
short healthy collective now usually retires on the issuing thread rather than
from the watchdog push, and pays one event query per op on the issue path.

Hook maps become ordered so hooks fire by id. That is what lets the race be
tested deterministically rather than with a sleep, and the flight recorder hook's
id already assumed ordering against user hooks.

Test Plan:

The race test does not sleep. A user post-hook with a lower id holds the window
open and, inside it, issues a sentinel collective behind the target on the same
stream queue; nccl2 pops that queue in order, so a retired sentinel proves the
target's completion was already delivered. It then asserts the target is not yet
retired -- i.e. the race really occurred -- and that it is retired, completed and
still carries a duration once the post-hook returns. Without the fix the last
assertion fails, since the completion fires once per Work and no second one is
coming.

The attach test asserts on what was built rather than on the env var, so it holds
either side of the default flip.

```
python test/distributed/test_c10d_fr_hook.py
python test/distributed/test_c10d_nccl2.py
python test/distributed/test_c10d_nccl.py NCCLTraceTest
python test/distributed/flight_recorder/test_fr_analysis.py
python test/distributed/test_c10d_hooks.py
python test/distributed/test_c10d_nan_check.py
```
```
Ran 63 tests in 323.043s  OK (skipped=9)
Ran 50 tests in 345.898s  OK (skipped=1)
Ran 61 tests in 496.705s  OK
Ran 23 tests in   1.790s  OK
Ran  2 tests in   7.803s  OK
Ran 20 tests in 102.351s  OK
```
The last two cover the other consumers of the hook plumbing whose map ordering
changed.

lintrunner --revision HEAD: clean.

Authored with the assistance of an AI coding agent.
Pull Request resolved: pytorch#192858
Approved by: https://github.com/d4l3k
ghstack dependencies: pytorch#192233, pytorch#192837
@github-actions
github-actions Bot deleted the gh/tushar00jain/17/head branch November 3, 2026 01:44
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