Skip to content

Commit

Permalink
rt: fix flaky test test_disable_lifo_slot (#6043)
Browse files Browse the repository at this point in the history
  • Loading branch information
Darksonn committed Oct 2, 2023
1 parent 0700d6a commit 310adf7
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion tokio/tests/rt_threaded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -746,12 +746,34 @@ mod unstable {

#[test]
fn test_disable_lifo_slot() {
use std::sync::mpsc::{channel, RecvTimeoutError};

let rt = runtime::Builder::new_multi_thread()
.disable_lifo_slot()
.worker_threads(2)
.build()
.unwrap();

// Spawn a background thread to poke the runtime periodically.
//
// This is necessary because we may end up triggering the issue in:
// <https://github.com/tokio-rs/tokio/issues/4730>
//
// Spawning a task will wake up the second worker, which will then steal
// the task. However, the steal will fail if the task is in the LIFO
// slot, because the LIFO slot cannot be stolen.
//
// Note that this only happens rarely. Most of the time, this thread is
// not necessary.
let (kill_bg_thread, recv) = channel::<()>();
let handle = rt.handle().clone();
let bg_thread = std::thread::spawn(move || {
let one_sec = std::time::Duration::from_secs(1);
while recv.recv_timeout(one_sec) == Err(RecvTimeoutError::Timeout) {
handle.spawn(async {});
}
});

rt.block_on(async {
tokio::spawn(async {
// Spawn another task and block the thread until completion. If the LIFO slot
Expand All @@ -760,7 +782,10 @@ mod unstable {
})
.await
.unwrap();
})
});

drop(kill_bg_thread);
bg_thread.join().unwrap();
}

#[test]
Expand Down

0 comments on commit 310adf7

Please sign in to comment.