Skip to content

[c10d][nccl2] Dump the flight recorder trace when a collective fails - #192234

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

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

Conversation

@tushar00jain

@tushar00jain tushar00jain commented Aug 5, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

Completes the flight recorder story for nccl2: a hang or an async error now
leaves a post-mortem on disk instead of nothing. Recording arrived with the hook;
this adds the trigger, and there is exactly one of them.

The trigger is the abort hook. FlightRecorderHook registers one in attach() and
writes the trace from it, which is what Hooks.hpp already describes an abort hook
as being for -- "called before aborting when a collective times out or fails.
This allows users to capture debug information before the abort." nccl2 therefore
contains no flight recorder code at all: no include, no dump call. It runs
runAbortHooks() and any backend that does the same gets dumps for free.

nccl2 runs those hooks where a failure is detected -- checkWorkQueue() on
TIMEDOUT and on ERROR, and the watchdog's ncclCommGetAsyncError branch -- not
only from the teardown paths that already ran them. abortProcess() returns
before running any hook unless TORCH_NCCL_ASYNC_ERROR_HANDLING asks for a
tear-down and reconfigure is off, so relying on it would produce nothing in
exactly the configurations where the process survives the failure and the trace
is worth the most. A consequence worth knowing when reading the call site: abort
hooks can now run when no abort follows.

Because the teardown paths still run the hooks, one failure can invoke the hook
more than once. That is deliberately NOT deduplicated at the call site, and
Hooks.hpp now documents the resulting contract (make abort hooks idempotent). An
invocation-level gate would be actively harmful: the thread that detects the
failure is normally the watchdog, but the thread that reaches ::abort() is the
next synchronous collective on the main thread. With a skip-if-already-ran gate
the main thread would see "already dumped", return immediately, and terminate the
process while the watchdog was still writing. The one-shot belongs inside the
hook, where it is a mutex held across the dump and the loser blocks until the
trace is on disk rather than skipping. That wait is bounded -- the dump only
calls cudaEventQuery, never a device sync.

Whether a backend supports abort hooks is now a capability query,
Backend::supportsAbortHooks(), following the existing supportsWindow() /
supportsReconfigure() convention, rather than catching the "does not support
registerAbortHook" exception. Attaching the hook to a gloo group must not throw,
but a throw out of registerAbortHook should still surface as the real error it
is, and pattern-matching an exception message cannot tell those apart.

Stack traces are off for this dump. Entry::getTraceback symbolizes and may need
the GIL, and this runs either on the watchdog thread or on a rank about to abort,
where blocking on the GIL would lose the very post-mortem being written. Stock
ProcessGroupNCCL disables them on its abort path for the same reason.

Gated on TORCH_FR_DUMP_ON_TIMEOUT with TORCH_NCCL_DUMP_ON_TIMEOUT as an alias,
matching how the recorder already handles TORCH_FR_BUFFER_SIZE /
TORCH_NCCL_TRACE_BUFFER_SIZE, so existing job configs keep working. Default true,
as in stock.

Also fixes a data race this change makes reachable: abortHooks_ was an unguarded
map, mutated by register/unregister while the watchdog iterated it in
runAbortHooks(). Nothing in tree registered an abort hook before, so it could not
be hit; now a FlightRecorderHook unregisters from its destructor, i.e. whenever
Python drops the handle, concurrently with a watchdog that may be firing. The
hooks are snapshotted under a new mutex and run outside it -- running them under
the lock would deadlock against remove(), which holds the hook's own mutex while
unregistering.

This diverges from torchcomms deliberately. Its flight recorder abort hook does
not dump -- it flips a health-check flag and sleeps 15s so an external poller can
scrape the process over the control plane -- despite its README saying otherwise.
That needs infrastructure a pytorch user does not have, so the hook writes the
trace itself.

Test Plan:

The dump-on-timeout test runs with TORCH_NCCL_ASYNC_ERROR_HANDLING=0, i.e. the
configuration where the teardown paths run no hook at all, and asserts the
on-disk pickle contains the hung collective:

python test/distributed/test_c10d_nccl2.py -v
Ran 38 tests in 213.334s

OK
[rank0] Operation timed out after 5164 ms
[rank0] FlightRecorderHook.cpp:308] FlightRecorderHook: dumping trace on collective failure

Also drilled the default configuration, where the watchdog does terminate the
process, under torchrun with 2 ranks and a 5s timeout, rank 0 hanging on an
all_reduce nobody joins:

torchrun --nproc-per-node=2 <drill: rank 0 hangs on an all_reduce>
rank : 0  exitcode : -6 (SIGABRT)
-rw-r--r-- 1 980 /tmp/frdrill/trace_0

0 c10d:all_reduce [[1024]] state=started   retired=True timeout_ms=5000 frames=None
1 c10d:all_reduce [[4096]] state=scheduled retired=True timeout_ms=5000 frames=None

The hung op is present and never started on the device, and frames=None confirms
stack traces stayed off.

Attaching to a backend without abort-hook support must still work:

python test/distributed/test_c10d_fr_hook.py -v
Ran 30 tests in 148.442s

OK

Regression check on the backend using the CUDAEvent instantiation of the same
recorder:

python test/distributed/test_c10d_nccl.py NCCLTraceTest -v
Ran 61 tests in 499.049s

OK

Authored with the assistance of an AI coding agent.

[ghstack-poisoned]
@pytorch-bot

pytorch-bot Bot commented Aug 5, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

✅ No Failures

As of commit 83db236 with merge base dac77b5 (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 added a commit that referenced this pull request Aug 5, 2026
Completes the flight recorder story for nccl2: a hang or an async error now
leaves a post-mortem on disk instead of nothing. Recording arrived with the
hook; this adds the trigger.

The dump fires where the failure is *detected* -- in checkWorkQueue(), on both
TIMEDOUT and ERROR, and in the watchdog's ncclCommGetAsyncError branch -- rather
than from an abort hook. abortProcess() returns before running any hook unless
TORCH_NCCL_ASYNC_ERROR_HANDLING asks for a tear-down and reconfigure is off, so
an abort-hook trigger would produce nothing in exactly the configurations where
the process survives the failure and the trace is worth the most. checkWorkQueue
is reached both from the watchdog and from checkAndAbortIfTimedOutOrError on the
next synchronous collective, so both routes are covered, and it runs before the
callers reach abortProcess() -> ::abort(). DebugInfoWriter flushes and closes
before returning, so nothing is buffered when the process dies.

The one-shot is a mutex held across the dump, not a compare-and-swap. The thread
that detects the timeout is normally the watchdog, but the thread that calls
::abort() is the next synchronous collective on the main thread; with a bare CAS
the main thread loses, returns immediately and terminates the process while the
watchdog is still writing. Holding the lock makes the loser wait until the trace
is on disk. The wait is bounded -- the dump only calls cudaEventQuery, never a
device sync.

Stack traces are disabled for this dump. Entry::getTraceback symbolizes and may
need the GIL, and this runs either on the watchdog thread or on a rank about to
abort, where blocking on the GIL would lose the very post-mortem being written.
Stock ProcessGroupNCCL disables them on its abort path for the same reason.

Gated on TORCH_NCCL_DUMP_ON_TIMEOUT, matching stock's name and its default of
true. Goes through the exported try_dump_fr_trace_file rather than
FlightRecorder<c10::Event>::get(): nccl2 links into libtorch_cuda while the hook
and recorder are in libtorch_cpu, and get()'s function-local static is not
exported, so calling it here would read a private, empty recorder and silently
never dump. If no hook ever attached (a mixed cpu:gloo,cuda:nccl2 group, or the
buffer size set to 0) the recorder has no rank and nothing was recorded, so this
logs and returns rather than writing a file named after rank -1.

This diverges from torchcomms deliberately. Its flight recorder abort hook does
not dump -- it flips a health-check flag and sleeps 15s so an external poller can
scrape the process over the control plane -- despite its README saying otherwise.
That needs infrastructure a pytorch user does not have, so we follow stock
ProcessGroupNCCL and write the trace ourselves.

Test Plan:

New test provokes a real timeout (rank 0 all-reduces on a 5s subgroup nobody
else joins) and asserts on the artifact, including that the file does not exist
beforehand and that the entry's timeout_ms pins it to the group that timed out:

```
python test/distributed/test_c10d_nccl2.py -v
```
```
Ran 27 tests in 337.595s

FAILED (errors=1, skipped=2)
```
The one error is a pre-existing failure unrelated to this change,
ProcessGroupNCCL2MemPoolTest::test_register_mem_pool (120s watchdog timeout,
exit -6); it reproduces with TORCH_FR_BUFFER_SIZE=0, which makes the attach a
no-op. The two skips are its siblings, skipped after it takes the class down.
The new ProcessGroupNCCL2DumpOnTimeoutTest passed.

Also drilled the default configuration, where the watchdog really does terminate
the process, under torchrun with 2 ranks and a 5s timeout:

```
torchrun --nproc-per-node=2 <drill: rank 0 hangs on a 4242-element all_reduce>
```
```
rank : 0  exitcode : -6 (SIGABRT)
-rw-r--r-- 1 979 /tmp/frdrill/trace_0

top-level keys: ['comm_lib_version', 'entries', 'pg_config', 'pg_status', 'version']
{'profiling_name': 'c10d:all_reduce', 'input_sizes': [[4]],    'state': 'started',   'timeout_ms': 5000}
{'profiling_name': 'c10d:all_reduce', 'input_sizes': [[4242]], 'state': 'scheduled', 'timeout_ms': 5000}
```
The hung collective is in the trace and the file survived the SIGABRT.

```
python test/distributed/test_c10d_fr_hook.py -v
python test/distributed/test_c10d_nccl.py NCCLTraceTest -v
```
```
Ran 28 tests in 119.732s  OK
Ran 61 tests in 481.514s  OK
```

Authored with the assistance of an AI coding agent.


ghstack-source-id: 64b465d
Pull-Request: #192234
@tushar00jain
tushar00jain marked this pull request as draft August 5, 2026 17:57
[ghstack-poisoned]
tushar00jain added a commit that referenced this pull request Aug 5, 2026
Completes the flight recorder story for nccl2: a hang or an async error now
leaves a post-mortem on disk instead of nothing. Recording arrived with the hook;
this adds the trigger, and there is exactly one of them.

The trigger is the abort hook. FlightRecorderHook registers one in attach() and
writes the trace from it, which is what Hooks.hpp already describes an abort hook
as being for -- "called before aborting when a collective times out or fails.
This allows users to capture debug information before the abort." nccl2 therefore
contains no flight recorder code at all: no include, no dump call. It runs
runAbortHooks() and any backend that does the same gets dumps for free.

nccl2 runs those hooks where a failure is *detected* -- checkWorkQueue() on
TIMEDOUT and on ERROR, and the watchdog's ncclCommGetAsyncError branch -- not
only from the teardown paths that already ran them. abortProcess() returns
before running any hook unless TORCH_NCCL_ASYNC_ERROR_HANDLING asks for a
tear-down and reconfigure is off, so relying on it would produce nothing in
exactly the configurations where the process survives the failure and the trace
is worth the most. A consequence worth knowing when reading the call site: abort
hooks can now run when no abort follows.

Because the teardown paths still run the hooks, one failure can invoke the hook
more than once. That is deliberately NOT deduplicated at the call site, and
Hooks.hpp now documents the resulting contract (make abort hooks idempotent). An
invocation-level gate would be actively harmful: the thread that detects the
failure is normally the watchdog, but the thread that reaches ::abort() is the
next synchronous collective on the main thread. With a skip-if-already-ran gate
the main thread would see "already dumped", return immediately, and terminate the
process while the watchdog was still writing. The one-shot belongs inside the
hook, where it is a mutex held across the dump and the loser blocks until the
trace is on disk rather than skipping. That wait is bounded -- the dump only
calls cudaEventQuery, never a device sync.

Whether a backend supports abort hooks is now a capability query,
Backend::supportsAbortHooks(), following the existing supportsWindow() /
supportsReconfigure() convention, rather than catching the "does not support
registerAbortHook" exception. Attaching the hook to a gloo group must not throw,
but a throw out of registerAbortHook should still surface as the real error it
is, and pattern-matching an exception message cannot tell those apart.

Stack traces are off for this dump. Entry::getTraceback symbolizes and may need
the GIL, and this runs either on the watchdog thread or on a rank about to abort,
where blocking on the GIL would lose the very post-mortem being written. Stock
ProcessGroupNCCL disables them on its abort path for the same reason.

Gated on TORCH_FR_DUMP_ON_TIMEOUT with TORCH_NCCL_DUMP_ON_TIMEOUT as an alias,
matching how the recorder already handles TORCH_FR_BUFFER_SIZE /
TORCH_NCCL_TRACE_BUFFER_SIZE, so existing job configs keep working. Default true,
as in stock.

Also fixes a data race this change makes reachable: abortHooks_ was an unguarded
map, mutated by register/unregister while the watchdog iterated it in
runAbortHooks(). Nothing in tree registered an abort hook before, so it could not
be hit; now a FlightRecorderHook unregisters from its destructor, i.e. whenever
Python drops the handle, concurrently with a watchdog that may be firing. The
hooks are snapshotted under a new mutex and run outside it -- running them under
the lock would deadlock against remove(), which holds the hook's own mutex while
unregistering.

This diverges from torchcomms deliberately. Its flight recorder abort hook does
not dump -- it flips a health-check flag and sleeps 15s so an external poller can
scrape the process over the control plane -- despite its README saying otherwise.
That needs infrastructure a pytorch user does not have, so the hook writes the
trace itself.

Test Plan:

The dump-on-timeout test runs with TORCH_NCCL_ASYNC_ERROR_HANDLING=0, i.e. the
configuration where the teardown paths run no hook at all, and asserts the
on-disk pickle contains the hung collective:

```
python test/distributed/test_c10d_nccl2.py -v
```
```
Ran 38 tests in 213.334s

OK
```
```
[rank0] Operation timed out after 5164 ms
[rank0] FlightRecorderHook.cpp:308] FlightRecorderHook: dumping trace on collective failure
```

Also drilled the default configuration, where the watchdog does terminate the
process, under torchrun with 2 ranks and a 5s timeout, rank 0 hanging on an
all_reduce nobody joins:

```
torchrun --nproc-per-node=2 <drill: rank 0 hangs on an all_reduce>
```
```
rank : 0  exitcode : -6 (SIGABRT)
-rw-r--r-- 1 980 /tmp/frdrill/trace_0

0 c10d:all_reduce [[1024]] state=started   retired=True timeout_ms=5000 frames=None
1 c10d:all_reduce [[4096]] state=scheduled retired=True timeout_ms=5000 frames=None
```
The hung op is present and never started on the device, and frames=None confirms
stack traces stayed off.

Attaching to a backend without abort-hook support must still work:

```
python test/distributed/test_c10d_fr_hook.py -v
```
```
Ran 30 tests in 148.442s

OK
```

Regression check on the backend using the CUDAEvent instantiation of the same
recorder:

```
python test/distributed/test_c10d_nccl.py NCCLTraceTest -v
```
```
Ran 61 tests in 499.049s

OK
```

Authored with the assistance of an AI coding agent.

ghstack-source-id: dba5a00
Pull-Request: #192234
tushar00jain added a commit that referenced this pull request Aug 5, 2026
Completes the flight recorder story for nccl2: a hang or an async error now
leaves a post-mortem on disk instead of nothing. Recording arrived with the hook;
this adds the trigger, and there is exactly one of them.

The trigger is the abort hook. FlightRecorderHook registers one in attach() and
writes the trace from it, which is what Hooks.hpp already describes an abort hook
as being for -- "called before aborting when a collective times out or fails.
This allows users to capture debug information before the abort." nccl2 therefore
contains no flight recorder code at all: no include, no dump call. It runs
runAbortHooks() and any backend that does the same gets dumps for free.

nccl2 runs those hooks where a failure is *detected* -- checkWorkQueue() on
TIMEDOUT and on ERROR, and the watchdog's ncclCommGetAsyncError branch -- not
only from the teardown paths that already ran them. abortProcess() returns
before running any hook unless TORCH_NCCL_ASYNC_ERROR_HANDLING asks for a
tear-down and reconfigure is off, so relying on it would produce nothing in
exactly the configurations where the process survives the failure and the trace
is worth the most. A consequence worth knowing when reading the call site: abort
hooks can now run when no abort follows.

Because the teardown paths still run the hooks, one failure can invoke the hook
more than once. That is deliberately NOT deduplicated at the call site, and
Hooks.hpp now documents the resulting contract (make abort hooks idempotent). An
invocation-level gate would be actively harmful: the thread that detects the
failure is normally the watchdog, but the thread that reaches ::abort() is the
next synchronous collective on the main thread. With a skip-if-already-ran gate
the main thread would see "already dumped", return immediately, and terminate the
process while the watchdog was still writing. The one-shot belongs inside the
hook, where it is a mutex held across the dump and the loser blocks until the
trace is on disk rather than skipping. That wait is bounded -- the dump only
calls cudaEventQuery, never a device sync.

Whether a backend supports abort hooks is now a capability query,
Backend::supportsAbortHooks(), following the existing supportsWindow() /
supportsReconfigure() convention, rather than catching the "does not support
registerAbortHook" exception. Attaching the hook to a gloo group must not throw,
but a throw out of registerAbortHook should still surface as the real error it
is, and pattern-matching an exception message cannot tell those apart.

Stack traces are off for this dump. Entry::getTraceback symbolizes and may need
the GIL, and this runs either on the watchdog thread or on a rank about to abort,
where blocking on the GIL would lose the very post-mortem being written. Stock
ProcessGroupNCCL disables them on its abort path for the same reason.

Gated on TORCH_FR_DUMP_ON_TIMEOUT with TORCH_NCCL_DUMP_ON_TIMEOUT as an alias,
matching how the recorder already handles TORCH_FR_BUFFER_SIZE /
TORCH_NCCL_TRACE_BUFFER_SIZE, so existing job configs keep working. Default true,
as in stock.

Also fixes a data race this change makes reachable: abortHooks_ was an unguarded
map, mutated by register/unregister while the watchdog iterated it in
runAbortHooks(). Nothing in tree registered an abort hook before, so it could not
be hit; now a FlightRecorderHook unregisters from its destructor, i.e. whenever
Python drops the handle, concurrently with a watchdog that may be firing. The
hooks are snapshotted under a new mutex and run outside it -- running them under
the lock would deadlock against remove(), which holds the hook's own mutex while
unregistering.

This diverges from torchcomms deliberately. Its flight recorder abort hook does
not dump -- it flips a health-check flag and sleeps 15s so an external poller can
scrape the process over the control plane -- despite its README saying otherwise.
That needs infrastructure a pytorch user does not have, so the hook writes the
trace itself.

Test Plan:

The dump-on-timeout test runs with TORCH_NCCL_ASYNC_ERROR_HANDLING=0, i.e. the
configuration where the teardown paths run no hook at all, and asserts the
on-disk pickle contains the hung collective:

```
python test/distributed/test_c10d_nccl2.py -v
```
```
Ran 38 tests in 213.334s

OK
```
```
[rank0] Operation timed out after 5164 ms
[rank0] FlightRecorderHook.cpp:308] FlightRecorderHook: dumping trace on collective failure
```

Also drilled the default configuration, where the watchdog does terminate the
process, under torchrun with 2 ranks and a 5s timeout, rank 0 hanging on an
all_reduce nobody joins:

```
torchrun --nproc-per-node=2 <drill: rank 0 hangs on an all_reduce>
```
```
rank : 0  exitcode : -6 (SIGABRT)
-rw-r--r-- 1 980 /tmp/frdrill/trace_0

0 c10d:all_reduce [[1024]] state=started   retired=True timeout_ms=5000 frames=None
1 c10d:all_reduce [[4096]] state=scheduled retired=True timeout_ms=5000 frames=None
```
The hung op is present and never started on the device, and frames=None confirms
stack traces stayed off.

Attaching to a backend without abort-hook support must still work:

```
python test/distributed/test_c10d_fr_hook.py -v
```
```
Ran 30 tests in 148.442s

OK
```

Regression check on the backend using the CUDAEvent instantiation of the same
recorder:

```
python test/distributed/test_c10d_nccl.py NCCLTraceTest -v
```
```
Ran 61 tests in 499.049s

OK
```

Authored with the assistance of an AI coding agent.

ghstack-source-id: dba5a00
Pull-Request: #192234
@github-actions
github-actions Bot deleted the gh/tushar00jain/15/head branch October 31, 2026 01:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant