From 952119de41c420a2da104928dbce6f65804385d5 Mon Sep 17 00:00:00 2001 From: Mikayla Maki Date: Tue, 19 May 2026 17:05:34 -0700 Subject: [PATCH] Fix window crash (#57203) Closes ZED-86Q Fixes this (rare) crash by making entity creation via a window context infallible. The shape of the result is a bit clunky due to the type signatures involved. The real solution is to remove AsyncWindowContext entirely and move it's fallible APIs into WindowHandle, but until then this will give you a simple starting point. 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 Release Notes: - N/A --- crates/gpui/src/app/async_context.rs | 39 ++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/crates/gpui/src/app/async_context.rs b/crates/gpui/src/app/async_context.rs index be917764f43..6c5246f33d0 100644 --- a/crates/gpui/src/app/async_context.rs +++ b/crates/gpui/src/app/async_context.rs @@ -378,12 +378,21 @@ impl AppContext for AsyncWindowContext { where T: 'static, { - // Associate the new entity with our captured window so that - // `with_window` can resolve a dispatch target before the entity has - // been rendered. - self.app - .update_window(self.window, |_, _, cx| cx.new(build_entity)) - .expect("window was unexpectedly closed") + let mut build_entity = Some(build_entity); + match self.app.update_window(self.window, |_, _, cx| { + cx.new( + build_entity + .take() + .expect("build_entity is taken exactly once"), + ) + }) { + Ok(entity) => entity, + Err(_) => self.app.new( + build_entity + .take() + .expect("update_window returned Err without invoking the closure"), + ), + } } fn reserve_entity(&mut self) -> Reservation { @@ -395,11 +404,19 @@ impl AppContext for AsyncWindowContext { reservation: Reservation, build_entity: impl FnOnce(&mut Context) -> T, ) -> Entity { - self.app - .update_window(self.window, |_, _, cx| { - cx.insert_entity(reservation, build_entity) - }) - .expect("window was unexpectedly closed") + let mut args = Some((reservation, build_entity)); + match self.app.update_window(self.window, |_, _, cx| { + let (reservation, build_entity) = args.take().expect("args are taken exactly once"); + cx.insert_entity(reservation, build_entity) + }) { + Ok(entity) => entity, + Err(_) => { + let (reservation, build_entity) = args + .take() + .expect("update_window returned Err without invoking the closure"); + self.app.insert_entity(reservation, build_entity) + } + } } fn update_entity(