Fix hang detection crash when foreground stats are missing (#58464)

Register the foreground thread with the GPUI profiler before starting
hang detection, so foreground task statistics are available when the
hang detection thread samples profiler data.

cc: @yara-blue I choose to leave the expect statements because it seems
like an invariant that we should uphold. Let me know if you would rather
have early returns and telemetry if they're hit

Self-Review Checklist:

- [x] I've reviewed my own diff for quality, security, and reliability
- [x] Unsafe blocks (if any) have justifying comments
- [x] The content is consistent with the [UI/UX
checklist](https://github.com/zed-industries/zed/blob/main/CONTRIBUTING.md#uiux-checklist)
- [x] Tests cover the new/changed behavior
- [x] Performance impact has been considered and is acceptable

Closes FR-58

Release Notes:

- Fixed a crash in hang detection when foreground thread statistics were
missing.

---------

Co-authored-by: Yara <git@yara.blue>
This commit is contained in:
Anthony EidandYara authored and GitHub committed 2026-06-03 21:58:23 +00:00
1 parent 590928affc
commit e023730795
2 files changed
+7 -8

No files matched your search

@@ -76,10 +76,9 @@ impl Reporter {
task_stats: &[gpui::ThreadTaskStatistics],
) -> ReportMade {
let foreground = self.foreground_thread;
let foreground = task_stats
.iter()
.find(|t| t.thread_id == foreground)
.expect("main thread should be in all statistics");
let Some(foreground) = task_stats.iter().find(|t| t.thread_id == foreground) else {
return false; // during startup the foreground may not yet have statistics
};
let hangs: Vec<_> = foreground
.stats
@@ -251,10 +251,10 @@ impl Reporter {
fn process_foreground(&mut self, task_stats: &[gpui::ThreadTaskStatistics]) {
let foreground_thread = self.foreground_thread;
let foreground = task_stats
.iter()
.find(|t| t.thread_id == foreground_thread)
.expect("main thread should be in all statistics");
let Some(foreground) = task_stats.iter().find(|t| t.thread_id == foreground_thread) else {
// during startup foreground thread might not have statistics yet
return;
};
for hang in foreground
.stats