Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix deadlock between snatchable_lock and trackers in Queue::write_texture #7004

Merged
merged 2 commits into from
Jan 27, 2025

Conversation

metamuffin
Copy link
Contributor

@metamuffin metamuffin commented Jan 27, 2025

Connections
Related to #5737

Description
The Device.snatchable_lock is usually acquired before Device.trackers is. Only Queue.write_texture() acquired them in reverse order, introducing deadlocks. This PR swaps the order there to align with other usages of those mutexes.

Testing
Manually checked all references to trackers mutex for snatchable_lock guards in the same scope.

Checklist

  • Run cargo fmt.
  • Run taplo format.
  • Run cargo clippy. If applicable, add:
    • --target wasm32-unknown-unknown
    • --target wasm32-unknown-emscripten
  • Run cargo xtask test to run tests. (Some tests failed here, but those fail without my change too)
  • Add change to CHANGELOG.md. See simple instructions inside file.

@metamuffin metamuffin requested a review from a team as a code owner January 27, 2025 12:53
Copy link
Contributor

@nical nical left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@metamuffin
Copy link
Contributor Author

Its actually not completely fixing the issue mentioned. Just one occurrence that I ran into.

@metamuffin
Copy link
Contributor Author

metamuffin commented Jan 27, 2025

I just noticed another easy-to-fix deadlock. I will add another commit soon. (Sorry)

@metamuffin
Copy link
Contributor Author

metamuffin commented Jan 27, 2025

I am not sure how feasible it is but I would suggest adding some internal documentation that exactly specifies in which order locks should be acquired if you need multiple. Like it was done for snatchable_lock at wgpu-core/src/device/resource.rs:88
I would just imagine an ordered list of all mutexes related or contained in Device.

The ordering that I detected from reading code that references the mutexes:
snatchable_lock, then pending_writes, then trackers

@nical nical merged commit 33e8df0 into gfx-rs:trunk Jan 27, 2025
31 checks passed
@cwfitzgerald
Copy link
Member

This should be defined by https://github.com/gfx-rs/wgpu/blob/trunk/wgpu-core/src/lock/rank.rs - I'm not sure what the current status of mutex rank checking.

DCNick3 added a commit to DCNick3/shin that referenced this pull request Feb 6, 2025
- reimplement LAYERWAIT
- fix for_layer_in_range to use inclusive [x; y] ranges instead of semi-internvals [x; y)
- fix SEWAIT racing with the first set of the status by the audio loop
- add deadlock detection to catch the elusive deadlock in wgpu (though it's probably fixed by gfx-rs/wgpu#7004)
- fix yet another dynamic buffer-related crash
DCNick3 added a commit to DCNick3/shin that referenced this pull request Feb 6, 2025
- reimplement LAYERWAIT
- fix for_layer_in_range to use inclusive [x; y] ranges instead of semi-internvals [x; y)
- fix SEWAIT racing with the first set of the status by the audio loop
- add deadlock detection to catch the elusive deadlock in wgpu (though it's probably fixed by gfx-rs/wgpu#7004)
- fix yet another dynamic buffer-related crash
@DCNick3
Copy link

DCNick3 commented Feb 22, 2025

Would it be possible to backport this as 24.0.x bugfix release?

I am reproducing this deadlock quite reliably in my engine :(. In my case it's between snatchable_lock and pending_writes but this PR takes care about it too.

@cwfitzgerald
Copy link
Member

If you could backport it (make a PR against the v24 branch) , we'll be doing a release next week and can release it

DCNick3 pushed a commit to DCNick3/wgpu that referenced this pull request Feb 25, 2025
…ture (gfx-rs#7004)

* Fix deadlock between snatchable_lock and trackers in Queue::write_texture

* Fix another deadlock in write_texture between pending_writes and snatchable_lock.
# Conflicts:
#	CHANGELOG.md
nical pushed a commit that referenced this pull request Feb 25, 2025
…ture (#7004)

* Fix deadlock between snatchable_lock and trackers in Queue::write_texture

* Fix another deadlock in write_texture between pending_writes and snatchable_lock.
# Conflicts:
#	CHANGELOG.md
cwfitzgerald pushed a commit that referenced this pull request Feb 27, 2025
…ture (#7004)

* Fix deadlock between snatchable_lock and trackers in Queue::write_texture

* Fix another deadlock in write_texture between pending_writes and snatchable_lock.
# Conflicts:
#	CHANGELOG.md
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants