From e023730795570d2fcdcaaa32df188c1b2a2a8b92 Mon Sep 17 00:00:00 2001 From: Anthony Eid <56899983+Anthony-Eid@users.noreply.github.com> Date: Wed, 3 Jun 2026 17:58:23 -0400 Subject: [PATCH] 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 --- crates/zed/src/reliability/hang_detection/logging.rs | 7 +++---- crates/zed/src/reliability/hang_detection/telemetry.rs | 8 ++++---- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/crates/zed/src/reliability/hang_detection/logging.rs b/crates/zed/src/reliability/hang_detection/logging.rs index c518d03ba1..940199dec5 100644 --- a/crates/zed/src/reliability/hang_detection/logging.rs +++ b/crates/zed/src/reliability/hang_detection/logging.rs @@ -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 diff --git a/crates/zed/src/reliability/hang_detection/telemetry.rs b/crates/zed/src/reliability/hang_detection/telemetry.rs index 4717cebe5a..a2d8e4136a 100644 --- a/crates/zed/src/reliability/hang_detection/telemetry.rs +++ b/crates/zed/src/reliability/hang_detection/telemetry.rs @@ -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