Make sure to unset current context in wgl Surface::configure/present #5087
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Connections
None known
Description
When re-building pipelines in background threads on the wgl backend we encounted this panic:
which is produced by the unwrap when making a context current fails here
wgpu/wgpu-hal/src/gles/wgl.rs
Line 76 in 7774f31
I tracked this down to Surface::configure and Surface::present not unsetting the current context after setting it even though they release the associated lock used for synchronizing this.
To fix this I took advantage of the existing guard type and added a new locking + make current method (called
lock_with_dc
that allows passing in the DC that Surface::configure/Surface::present were previously passing directly tomake_current
.Note that with this guard type, if unmake_current fails it will panic. I wasn't completely sure if this was appropriate for Surface::configure/present since they return errors instead of panicking when make_current fails. However, if unmake_current fails it seems like later calls to make_current (if called on a different thread) would also fail causing
AdapterContext::lock
to panic anyway? I'm not really familiar with what would cause unmake_current to fail though and what the behavior would be in terms of whether make_current can succeed later after such a failure.Note, another small behavior change is that this uses the same
.try_lock_for(Duration::from_secs(CONTEXT_LOCK_TIMEOUT_SECS))
pattern instead of just a.lock()
.Testing
To reproduce this I spawned an extra thread in the examples
framework.rs
that calledAdapter::get_texture_format_features
in a loop. This consistently produced panics when trying to make the gl context current to that thread. After this change, no panics were observed with this testing setup. Additionally, the original issue when re-building pipelines seems to be resolved.Checklist
cargo fmt
.cargo clippy
. If applicable, add:--target wasm32-unknown-unknown
--target wasm32-unknown-emscripten
cargo xtask test
to run tests.CHANGELOG.md
. See simple instructions inside file.