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

Fixed deadlock in injection_queue_depth_multi_thread test #6916

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
66 changes: 39 additions & 27 deletions tokio/tests/rt_metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
#![warn(rust_2018_idioms)]
#![cfg(all(feature = "full", not(target_os = "wasi"), target_has_atomic = "64"))]

use std::sync::{Arc, Barrier};
use std::sync::mpsc;
use std::time::Duration;
use tokio::runtime::Runtime;

#[test]
Expand Down Expand Up @@ -68,36 +69,47 @@ fn injection_queue_depth_multi_thread() {
let rt = threaded();
let metrics = rt.metrics();

let barrier1 = Arc::new(Barrier::new(3));
let barrier2 = Arc::new(Barrier::new(3));

// Spawn a task per runtime worker to block it.
for _ in 0..2 {
let barrier1 = barrier1.clone();
let barrier2 = barrier2.clone();
rt.spawn(async move {
barrier1.wait();
barrier2.wait();
});
}

barrier1.wait();

let mut fail: Option<String> = None;
for i in 0..10 {
let depth = metrics.injection_queue_depth();
if i != depth {
fail = Some(format!("{i} is not equal to {depth}"));
break;
'next_try: for _ in 0..10 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We usually do this kind of retrying by defining two functions, to separate out the concerns.

let (tx, rx) = mpsc::channel();

// Dropping _blocking_tasks will cause the blocking tasks to finish.
let _blocking_tasks: Vec<_> = (0..2)
.map(|_| {
let tx = tx.clone();
let (task, barrier) = mpsc::channel::<()>();

// Spawn a task per runtime worker to block it.
rt.spawn(async move {
tx.send(()).unwrap();
barrier.recv().ok();
});

task
})
.collect();

// Make sure both tasks are blocking the runtime so that we can
// deterministically fill up the injection queue with pending tasks.
//
// If this times out, we deadlocked the runtime (which can happen and is
// expected behaviour) and retry the test until we manage to either not
// deadlock the runtime or exhaust all tries, the latter causing the
// test to fail.
for _ in 0..2 {
if rx.recv_timeout(Duration::from_secs(1)).is_err() {
continue 'next_try;
}
}
rt.spawn(async {});
}

barrier2.wait();
for i in 0..10 {
assert_eq!(i, metrics.injection_queue_depth());
rt.spawn(async {});
}

if let Some(fail) = fail {
panic!("{fail}");
return;
}

panic!("runtime deadlocked each try");
}

fn current_thread() -> Runtime {
Expand Down
Loading