Skip to content

[c10d] Fix use-after-free in FlightRecorder::dump_entries - #192232

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

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

Conversation

@tushar00jain

@tushar00jain tushar00jain commented Aug 5, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

FlightRecorder::Entry holds borrowed raw EventType pointers, owned by whoever
created the work. dump_entries copied the matching entries out under mutex_ and
then ran the update_state loop -- which calls start_->query() / end_->query() --
on those copies after releasing the lock. Any thread that completed a work and
freed its events in that window left the dumping thread querying freed memory.
The window is reachable from any concurrent _dump_fr_trace / _dump_nccl_trace.

retire_id is the reason the lock matters: it is what clears an entry's event
pointers before the owner frees the events, and it does so under mutex_. Copying
the pointers out and dereferencing them outside that mutex defeats it.

The fix moves the update_state + null-out loop inside the existing mutex_ scope.
This adds no new blocking: retire_id already called update_state under the lock,
and query() is non-blocking. The genuinely blocking call, getDurationFromEvent,
still runs with the lock released.

This is a pre-existing defect rather than one introduced by the flight-recorder
work stacked on top of it, but that work makes it far easier to hit, because it
gives the recorder a second concurrent owner of entry events.

Test Plan:

Verified with the rest of the flight-recorder stack applied; NCCLTraceTest is
the suite that covers concurrent dump paths, buffer wraparound and reset:

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

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/192232

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

✅ No Failures

As of commit 28c3572 with merge base aafcd51 (image):
💚 Looks good so far! There are no failures yet. 💚

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

@kapilsh kapilsh left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM!

[ghstack-poisoned]

@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

[ghstack-poisoned]
[ghstack-poisoned]
@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 6, 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

ankitbhrdwj pushed a commit to ankitbhrdwj/pytorch that referenced this pull request Aug 12, 2026
…2232)

FlightRecorder::Entry holds *borrowed* raw EventType pointers, owned by whoever
created the work. dump_entries copied the matching entries out under mutex_ and
then ran the update_state loop -- which calls start_->query() / end_->query() --
on those copies after releasing the lock. Any thread that completed a work and
freed its events in that window left the dumping thread querying freed memory.
The window is reachable from any concurrent _dump_fr_trace / _dump_nccl_trace.

retire_id is the reason the lock matters: it is what clears an entry's event
pointers before the owner frees the events, and it does so under mutex_. Copying
the pointers out and dereferencing them outside that mutex defeats it.

The fix moves the update_state + null-out loop inside the existing mutex_ scope.
This adds no new blocking: retire_id already called update_state under the lock,
and query() is non-blocking. The genuinely blocking call, getDurationFromEvent,
still runs with the lock released.

This is a pre-existing defect rather than one introduced by the flight-recorder
work stacked on top of it, but that work makes it far easier to hit, because it
gives the recorder a second concurrent owner of entry events.

Test Plan:

Verified with the rest of the flight-recorder stack applied; NCCLTraceTest is
the suite that covers concurrent dump paths, buffer wraparound and reset:

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

OK
```

Authored with the assistance of an AI coding agent.
Pull Request resolved: pytorch#192232
Approved by: https://github.com/Skylion007, https://github.com/kapilsh, https://github.com/d4l3k
@github-actions
github-actions Bot deleted the gh/tushar00jain/13/head branch October 31, 2026 01:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

5 participants