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 the infinite parking of the main thread after shutdown #395

Merged
merged 3 commits into from
Jul 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 20 additions & 17 deletions rust_crate/src/interface_os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ use std::cell::RefCell;
use std::future::Future;
use std::pin::Pin;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex, OnceLock};
use std::sync::{Arc, Condvar, Mutex, OnceLock};
use std::task::{Context, Poll, Waker};
use std::thread;
use std::thread::{current, park, Thread};
use tokio::runtime::Builder;

static DART_ISOLATE: Mutex<Option<Isolate>> = Mutex::new(None);
Expand Down Expand Up @@ -173,8 +172,9 @@ pub fn send_rust_signal_real(

struct ShutdownSender {
should_shutdown: Arc<AtomicBool>,
did_shutdown: Arc<AtomicBool>,
waker: Arc<Mutex<Option<Waker>>>,
did_shutdown: Arc<Mutex<bool>>,
is_done: Arc<Condvar>,
}

impl Drop for ShutdownSender {
Expand All @@ -185,9 +185,12 @@ impl Drop for ShutdownSender {
waker.wake();
}
}
while !self.did_shutdown.load(Ordering::SeqCst) {
// Dropping the sender is always done on the main thread.
park();
while let Ok(guard) = self.did_shutdown.lock() {
if *guard {
break;
} else {
let _unused = self.is_done.wait(guard);
}
}
}
}
Expand All @@ -213,39 +216,39 @@ impl Future for ShutdownReceiver {

type ChannelTuple = (ShutdownSender, ShutdownReceiver, ShutdownReporter);
fn shutdown_channel() -> ChannelTuple {
// This code assumes that
// this function is being called from the main thread.
let main_thread = current();

let should_shutdown = Arc::new(AtomicBool::new(false));
let is_done = Arc::new(AtomicBool::new(false));
let waker = Arc::new(Mutex::new(None));
let did_shutdown = Arc::new(Mutex::new(false));
let is_done = Arc::new(Condvar::new());

let sender = ShutdownSender {
should_shutdown: should_shutdown.clone(),
waker: waker.clone(),
did_shutdown: is_done.clone(),
did_shutdown: did_shutdown.clone(),
is_done: is_done.clone(),
};
let receiver = ShutdownReceiver {
should_shutdown,
waker,
};
let reporter = ShutdownReporter {
did_shutdown,
is_done,
main_thread,
};

(sender, receiver, reporter)
}

struct ShutdownReporter {
is_done: Arc<AtomicBool>,
main_thread: Thread,
did_shutdown: Arc<Mutex<bool>>,
is_done: Arc<Condvar>,
}

impl Drop for ShutdownReporter {
fn drop(&mut self) {
self.is_done.store(true, Ordering::SeqCst);
self.main_thread.unpark();
if let Ok(mut guard) = self.did_shutdown.lock() {
*guard = true;
}
self.is_done.notify_all();
}
}
Loading