Skip to content

Commit

Permalink
Fix a SIGSEGV issue caused by the polling thread being the final owni…
Browse files Browse the repository at this point in the history
…ng device thread on some platforms
  • Loading branch information
Joeoc2001 committed Dec 31, 2023
1 parent e6f5418 commit 9510a52
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/async_device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ impl AsyncDevice {
pub(crate) fn new(device: Arc<Device>) -> Self {
Self {
#[cfg(not(target_arch = "wasm32"))]
poll_loop: Arc::new(crate::wgpu_future::PollLoop::new(device.clone())),
poll_loop: Arc::new(crate::wgpu_future::PollLoop::new(Arc::downgrade(&device))),
device,
}
}
Expand Down
9 changes: 6 additions & 3 deletions src/wgpu_future.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::future::Future;
use std::ops::DerefMut;
use std::pin::Pin;
use std::sync::{Arc, Mutex};
use std::sync::{Arc, Mutex, Weak};
use std::task::{Context, Poll, Waker};
use wgpu::Maintain;

Expand Down Expand Up @@ -30,7 +30,7 @@ pub(crate) struct PollLoop {

#[cfg(not(target_arch = "wasm32"))]
impl PollLoop {
pub(crate) fn new(device: Arc<wgpu::Device>) -> Self {
pub(crate) fn new(device: Weak<wgpu::Device>) -> Self {
let has_work = Arc::new(AtomicUsize::new(0));
let is_done = Arc::new(AtomicBool::new(false));
let locally_has_work = has_work.clone();
Expand All @@ -41,7 +41,10 @@ impl PollLoop {
handle: std::thread::spawn(move || {
while !locally_is_done.load(Ordering::Acquire) {
while locally_has_work.load(Ordering::Acquire) != 0 {
device.poll(Maintain::Wait);
match device.upgrade() {
None => return, // If all other references to the device are dropped, don't keep hold of the device here
Some(device) => device.poll(Maintain::Wait),
};
}

std::thread::park();
Expand Down

0 comments on commit 9510a52

Please sign in to comment.