Skip to content

Unmapping a buffer before the mapping completes causes a panic #1238

@Imberflur

Description

@Imberflur

Description
When attempting to map buffers in LifetimeTracker::handle_mapping there is a panic if the buffer is not in the Waiting state.

                let mapping = match std::mem::replace(
                    &mut buffer.map_state,
                    resource::BufferMapState::Idle,
                ) {
                    resource::BufferMapState::Waiting(pending_mapping) => pending_mapping,
                    _ => panic!("No pending mapping."),
                };

However, calling buffer_unmap ignores this state and replaces it with the Idle state without any type of cancellation mechanism being invoked.

            resource::BufferMapState::Waiting(_) => {}

This leads to a panic the next time handle_mapping is called.

Repro steps
In the wgpu-rs capture example apply this diff and run:

diff --git a/examples/capture/main.rs b/examples/capture/main.rs
index 71157c2..522989f 100644
--- a/examples/capture/main.rs
+++ b/examples/capture/main.rs
@@ -126,6 +126,7 @@ async fn create_png(
     // Note that we're not calling `.await` here.
     let buffer_slice = output_buffer.slice(..);
     let buffer_future = buffer_slice.map_async(wgpu::MapMode::Read);
+    output_buffer.unmap();
 
     // Poll the device in a blocking manner so that our future resolves.
     // In an actual application, `device.poll(...)` should

Expected vs observed behavior
The spec states
image

Thus, I expect the Future resolves to a mapping aborted error rather than a panic when polling the device

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: apiIssues related to API surfacehelp requiredWe need community help to make this happen.type: bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions