diff --git a/CHANGELOG.md b/CHANGELOG.md index 4c7b86dc1..a9ccfb37f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -83,6 +83,7 @@ Bottom level categories: - Zero-initialize padding (if any) at the end of a buffer allocation. This was application-visible in rare cases on Vulkan when a shader read beyond the valid range of a vertex buffer. By @andyleiserson in [#9791](https://github.com/gfx-rs/wgpu/pull/9791). - Fix required immediate slots calculation and remove `naga::valid::FunctionInfo::immediate_slots_used`. By @beicause in [#9725](https://github.com/gfx-rs/wgpu/pull/9725). +- Fix a spurious assertion failure in `Device::maintain` when multiple threads race polling the same device. By @AdrianEddy in [#9958](https://github.com/gfx-rs/wgpu/pull/9958). - Fix `PendingSubmission` releasing its lock guards out of stacking order, which tripped `--cfg wgpu_validate_locks` on any submission. By @AdrianEddy in [#9960](https://github.com/gfx-rs/wgpu/pull/9960). - Fixed some cases where initialization tracking was not correct. By @andyleiserson in [#10002](https://github.com/gfx-rs/wgpu/pull/10002). diff --git a/wgpu-core/src/device/resource.rs b/wgpu-core/src/device/resource.rs index d9a853ab3..a6300beaa 100644 --- a/wgpu-core/src/device/resource.rs +++ b/wgpu-core/src/device/resource.rs @@ -993,8 +993,10 @@ impl Device { wgt::PollType::Poll => None, }; - // Wait for the submission index if requested. - if let Some(target_submission_index) = wait_submission_index { + // If a target submission index was specified, wait for it, and set + // `wait_succeeded` to `Some(bool)` indicating success or timeout. If + // no target was specified, set `wait_succeeded` to `None`. + let wait_succeeded = if let Some(target_submission_index) = wait_submission_index { log::trace!("Device::maintain: waiting for submission index {target_submission_index}"); let wait_timeout = match poll_type { @@ -1009,13 +1011,16 @@ impl Device { .wait(self.fence.as_ref(), target_submission_index, wait_timeout) }; - // This error match is only about `DeviceErrors`. At this stage we do not care if - // the wait succeeded or not, and the `Ok(bool)`` variant is ignored. - if let Err(e) = wait_result { - let hal_error: WaitIdleError = self.handle_hal_error(e).into(); - return (user_closures, Err(hal_error)); + match wait_result { + Ok(succeeded) => Some(succeeded), + Err(e) => { + let hal_error: WaitIdleError = self.handle_hal_error(e).into(); + return (user_closures, Err(hal_error)); + } } - } + } else { + None + }; // Get the currently finished submission index. This may be higher than the requested // wait, or it may be less than the requested wait if the wait failed. @@ -1071,10 +1076,22 @@ impl Device { // Based on the queue empty status, and the current finished submission index, determine // the result of the poll. - let result = if queue_empty { + // + // After a successful wait, `current_finished_submission` should match or exceed + // the target. But after a timeout, more work may have finished before + // `current_finished_submission` is read, so it could be on either side of the + // target, and the queue can also become empty before `Queue::maintain` + // computes `queue_empty`. + // + // We report as accurately as we can with the information available. In + // particular, when our wait timed out but `queue_empty` comes back `true`, we + // report `WaitSucceeded` if `current_finished_submission` reached the target, + // and `Timeout` if it didn't. We don't want to risk reporting `QueueEmpty` + // without actually having seen that the target submission is retired. + let result = if queue_empty && wait_succeeded != Some(false) { if let Some(wait_submission_index) = wait_submission_index { - // Assert to ensure that if we received a queue empty status, the fence shows the - // correct value. This is defensive, as this should never be hit. + // Sanity-check that we don't report `QueueEmpty` without + // reaching the target submission index. assert!( current_finished_submission >= wait_submission_index, concat!( @@ -1084,6 +1101,8 @@ impl Device { current_finished_submission, wait_submission_index, ); + } else { + // We didn't wait (passive poll), safe to report `QueueEmpty`. } Ok(wgt::PollStatus::QueueEmpty)