mirror of
https://github.com/tokio-rs/tokio.git
synced 2026-09-07 00:00:08 +02:00
rt: fix threaded scheduler shutdown deadlock (#2074)
Previously, if an IO event was received during the runtime shutdown process, it was possible to enter a deadlock. This was due to the scheduler shutdown logic not expecting tasks to get scheduled once the worker was in the shutdown process. This patch fixes the deadlock by checking the queues for new tasks after each call to park. If a new task is received, it is forcefully shutdown. Fixes #2061
This commit is contained in:
@@ -63,65 +63,6 @@ fn acquire_mutex_in_drop() {
|
||||
drop(rt);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn wake_while_rt_is_dropping() {
|
||||
use tokio::task;
|
||||
|
||||
struct OnDrop<F: FnMut()>(F);
|
||||
|
||||
impl<F: FnMut()> Drop for OnDrop<F> {
|
||||
fn drop(&mut self) {
|
||||
(self.0)()
|
||||
}
|
||||
}
|
||||
|
||||
let (tx1, rx1) = oneshot::channel();
|
||||
let (tx2, rx2) = oneshot::channel();
|
||||
let (tx3, rx3) = oneshot::channel();
|
||||
|
||||
let mut rt = rt();
|
||||
|
||||
let h1 = rt.handle().clone();
|
||||
|
||||
rt.handle().spawn(async move {
|
||||
// Ensure a waker gets stored in oneshot 1.
|
||||
let _ = rx1.await;
|
||||
tx3.send(()).unwrap();
|
||||
});
|
||||
|
||||
rt.handle().spawn(async move {
|
||||
// When this task is dropped, we'll be "closing remotes".
|
||||
// We spawn a new task that owns the `tx1`, to move its Drop
|
||||
// out of here.
|
||||
//
|
||||
// Importantly, the oneshot 1 has a waker already stored, so
|
||||
// the eventual drop here will try to re-schedule again.
|
||||
let mut opt_tx1 = Some(tx1);
|
||||
let _d = OnDrop(move || {
|
||||
let tx1 = opt_tx1.take().unwrap();
|
||||
h1.spawn(async move {
|
||||
tx1.send(()).unwrap();
|
||||
});
|
||||
});
|
||||
let _ = rx2.await;
|
||||
});
|
||||
|
||||
rt.handle().spawn(async move {
|
||||
let _ = rx3.await;
|
||||
// We'll never get here, but once task 3 drops, this will
|
||||
// force task 2 to re-schedule since it's waiting on oneshot 2.
|
||||
tx2.send(()).unwrap();
|
||||
});
|
||||
|
||||
// Tick the loop
|
||||
rt.block_on(async {
|
||||
task::yield_now().await;
|
||||
});
|
||||
|
||||
// Drop the rt
|
||||
drop(rt);
|
||||
}
|
||||
|
||||
fn rt() -> Runtime {
|
||||
tokio::runtime::Builder::new()
|
||||
.basic_scheduler()
|
||||
|
||||
Reference in New Issue
Block a user