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(