From 2508e37a5ee8c0ac13d07585a7db6fac4d8dae8f Mon Sep 17 00:00:00 2001 From: John Billings Date: Wed, 2 Sep 2026 16:16:36 -0700 Subject: [PATCH] fix(core): Free prepared ids that are never assigned (#10184) --- CHANGELOG.md | 1 + wgpu-core/src/registry.rs | 27 ++++++++++++++++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8ecd74f93..c6c4d1004 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -47,6 +47,7 @@ Bottom level categories: #### General - Explicitly implement `Send` and `Sync` in `wgpu-core` to reduce the risk of `recursion_limit` overflow. By @nazar-pc in [#9953](https://github.com/gfx-rs/wgpu/pull/9953) +- Fix a texture id leak when surface acquire returns no texture. By @jnb in [#10184](https://github.com/gfx-rs/wgpu/pull/10184). ## v30.0.1 (2026-08-21) diff --git a/wgpu-core/src/registry.rs b/wgpu-core/src/registry.rs index 6bc954f6b..bb67f13ef 100644 --- a/wgpu-core/src/registry.rs +++ b/wgpu-core/src/registry.rs @@ -63,6 +63,7 @@ impl Registry { #[must_use] pub(crate) struct FutureId<'a, T: StorageItem> { id: Id, + identity: &'a IdentityManager, data: &'a RwLock>, } @@ -73,7 +74,17 @@ impl FutureId<'_, T> { pub fn assign(self, value: T) -> Id { let mut data = self.data.write(); data.insert(self.id, value); - self.id + // Assigned ids are freed by `Registry::remove`, not `Drop`. + let id = self.id; + core::mem::forget(self); + id + } +} + +/// Free the id if it was never assigned, so that its index can be reused. +impl Drop for FutureId<'_, T> { + fn drop(&mut self) { + self.identity.free(self.id); } } @@ -87,6 +98,7 @@ impl Registry { } None => self.identity.process(), }, + identity: &self.identity, data: &self.storage, } } @@ -169,4 +181,17 @@ mod tests { } }) } + + #[test] + fn dropped_ids_are_reused() { + let registry = Registry::new(); + + let fid = registry.prepare(None); + let index = fid.id.unzip().0; + drop(fid); + + let fid = registry.prepare(None); + assert_eq!(fid.id.unzip().0, index); + fid.assign(Arc::new(TestData)); + } }