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 1 commit
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
Next Next commit
Fix the infinite parking during shutdown
  • Loading branch information
temeddix committed Jul 7, 2024
commit 4c0c2c4e42988723905975b199304e2a6f085853
36 changes: 22 additions & 14 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 @@ -78,6 +77,7 @@ where
tokio_runtime.block_on(shutdown_receiver);
// Dropping the tokio runtime makes it shut down.
drop(tokio_runtime);
println!("DROPPED RUNTIME (TEMP)");
// After dropping the runtime, tell the main thread to stop waiting.
drop(shutdown_reporter);
});
Expand Down Expand Up @@ -173,8 +173,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,10 +186,14 @@ 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);
}
}
println!("END (TEMP)");
}
}

Expand All @@ -215,37 +220,40 @@ 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();
}
}