Skip to content

Commit

Permalink
Prevent panic in buffer mapping logic if the buffer is already destro…
Browse files Browse the repository at this point in the history
…yed.
  • Loading branch information
nical authored and teoxoy committed Nov 15, 2023
1 parent f5665f7 commit b47fe3b
Showing 1 changed file with 23 additions and 16 deletions.
39 changes: 23 additions & 16 deletions wgpu-core/src/device/life.rs
Original file line number Diff line number Diff line change
Expand Up @@ -840,21 +840,22 @@ impl<A: HalApi> LifetimeTracker<A> {

for stored in self.mapped.drain(..) {
let resource_id = stored.value;
let buf = &buffer_guard[resource_id];

let submit_index = buf.life_guard.life_count();
log::trace!(
"Mapping of {:?} at submission {:?} gets assigned to active {:?}",
resource_id,
submit_index,
self.active.iter().position(|a| a.index == submit_index)
);

self.active
.iter_mut()
.find(|a| a.index == submit_index)
.map_or(&mut self.ready_to_map, |a| &mut a.mapped)
.push(resource_id);
// The buffer may have been destroyed since the map request.
if let Ok(buf) = buffer_guard.get(resource_id.0) {
let submit_index = buf.life_guard.life_count();
log::trace!(
"Mapping of {:?} at submission {:?} gets assigned to active {:?}",
resource_id,
submit_index,
self.active.iter().position(|a| a.index == submit_index)
);

self.active
.iter_mut()
.find(|a| a.index == submit_index)
.map_or(&mut self.ready_to_map, |a| &mut a.mapped)
.push(resource_id);
}
}
}

Expand All @@ -879,7 +880,13 @@ impl<A: HalApi> LifetimeTracker<A> {
Vec::with_capacity(self.ready_to_map.len());
let mut trackers = trackers.lock();
for buffer_id in self.ready_to_map.drain(..) {
let buffer = &mut buffer_guard[buffer_id];
let buffer = match buffer_guard.get_occupied_or_destroyed_mut(buffer_id.0) {
Ok(buf) => buf,
Err(..) => {
// The buffer may have been destroyed since the map request.
continue;
}
};
if buffer.life_guard.ref_count.is_none() && trackers.buffers.remove_abandoned(buffer_id)
{
buffer.map_state = resource::BufferMapState::Idle;
Expand Down

0 comments on commit b47fe3b

Please sign in to comment.