diff --git a/crates/gpu/src/context.rs b/crates/gpu/src/context.rs index bc6f5fb7f..20333b889 100644 --- a/crates/gpu/src/context.rs +++ b/crates/gpu/src/context.rs @@ -1717,6 +1717,11 @@ impl GpuContext { self.pool.lock().map(|p| p.retained()).unwrap_or_default() } + #[cfg(test)] + pub(crate) fn pool_open_encoders(&self) -> usize { + self.pool.lock().unwrap().open.len() + } + /// Drop the working textures kept for reuse (a render is done). Textures still waiting for /// an open encoder and those of live `Enc`s are unaffected. pub fn trim_pool(&self) { @@ -2020,8 +2025,11 @@ impl<'g> Enc<'g> { &mut self .enc .get_or_insert_with(|| { + // Register only after creation succeeds: a driver panic must not leave an + // open pool id without an encoder that can close it. + let enc = g.device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some("effectcraft") }); let id = g.pool.lock().map(|mut p| p.open()).unwrap_or(0); - (g.device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some("effectcraft") }), id) + (enc, id) }) .0 } @@ -2170,8 +2178,15 @@ impl<'g> Enc<'g> { /// Submit recorded work. pub fn submit(&mut self) { + let queue = &self.g.queue; + self.submit_with(|enc| queue.submit([enc.finish()])); + } + + /// Closing the pool record must also happen when finishing or submitting commands panics. + /// Otherwise the removed encoder can never release the textures waiting behind its id. + pub(crate) fn submit_with(&mut self, submit: impl FnOnce(wgpu::CommandEncoder) -> wgpu::SubmissionIndex) { if let Some((enc, id)) = self.enc.take() { - self.last_submit = Some(self.g.queue.submit([enc.finish()])); + self.last_submit = self.g.readbacks.run_device(|| submit(enc)).ok(); if let Ok(mut p) = self.g.pool.lock() { p.close(id); } @@ -2200,7 +2215,13 @@ impl<'g> Enc<'g> { if let Ok(mut p) = self.g.pool.lock() { p.flush_owned(self.owner); } - let idx = self.last_submit.take().unwrap_or_else(|| self.g.queue.submit([])); + let idx = match self.last_submit.take() { + Some(idx) => idx, + None => { + let Ok(idx) = self.g.readbacks.run_device(|| self.g.queue.submit([])) else { return }; + idx + } + }; self.g.poll_wait(idx, self.g.memcap.wait_timeout()); self.g.memcap.record_wait(self.g.allocated_bytes(), self.g.kept_bytes(self.owner, None)); } diff --git a/crates/gpu/src/tests.rs b/crates/gpu/src/tests.rs index 433128248..22058f7fb 100644 --- a/crates/gpu/src/tests.rs +++ b/crates/gpu/src/tests.rs @@ -1132,6 +1132,28 @@ fn within_memory_releases_caches_when_already_retired() { retired_attempt_releases_kept_resources(true); } +/// #4079: a failed submission must not strand an open pool record or reuse the failed device. +#[test] +fn a_submission_panic_closes_the_encoder_and_retires_the_frame() { + hold_gpu_lock(); + let made = Gpu::try_headless(); + let Some(g) = require_gpu("submission failure cleanup", made.as_ref()) else { return }; + let result = g.within_memory(|| { + let mut enc = crate::context::Enc::new(&g.ctx); + let img = enc.image(16, 16); + enc.display(&img); + assert!(g.ctx.pool_open_encoders() > 0); + enc.submit_with(|_| panic!("synthetic submission failure")); + assert_eq!(g.ctx.pool_open_encoders(), 0, "a removed encoder cannot close itself in Drop"); + img + }); + assert!(matches!(result, Err(crate::FrameError::Retired(_)))); + assert_eq!(g.ctx.pool_retained_bytes(), 0, "CPU fallback releases the failed frame's textures"); + let called = std::cell::Cell::new(false); + assert!(g.within_memory(|| called.set(true)).is_err()); + assert!(!called.get(), "later frames must not reuse the failed context"); +} + #[test] fn within_memory_releases_caches_when_retired_during_work() { retired_attempt_releases_kept_resources(false);