use std::path::Path;
use std::sync::mpsc::{self, Receiver, RecvTimeoutError};
use std::time::Duration;
pub const DEADLOCK_GUARD: Duration = Duration::from_secs(600);
#[track_caller]
#[must_use]
pub fn recv<T>(rx: &Receiver<T>, what: &str) -> T {
match rx.recv_timeout(DEADLOCK_GUARD) {
Ok(v) => v,
Err(RecvTimeoutError::Timeout) => hung(what),
Err(RecvTimeoutError::Disconnected) => {
panic!(
"the thread that would answer panicked without answering (its own message says \
why); waiting for {what}"
)
}
}
}
#[track_caller]
pub fn wait(rx: &Receiver<()>, what: &str) {
let () = recv(rx, what);
}
#[track_caller]
pub fn hold(rx: &Receiver<()>, what: &str) {
if let Err(RecvTimeoutError::Timeout) = rx.recv_timeout(DEADLOCK_GUARD) {
hung(what);
}
}
#[track_caller]
fn hung(what: &str) -> ! {
panic!(
"deadlock guard: no answer in {} s, which only a hang takes, not load; waiting for {what}",
DEADLOCK_GUARD.as_secs()
)
}
#[track_caller]
#[must_use]
pub fn finishes<T: Send + 'static>(what: &str, f: impl FnOnce() -> T + Send + 'static) -> T {
let (tx, rx) = mpsc::channel();
std::thread::spawn(move || {
let _ = tx.send(f());
});
recv(&rx, what)
}
pub const TIMED_WAITS: &[&str] = &[
".recv_timeout(",
".wait_timeout(",
".wait_timeout_while(",
"::park_timeout(",
];
#[must_use]
pub fn unguarded(root: &Path) -> Vec<String> {
let mut files = Vec::new();
rust_files(root, &mut files);
files.sort();
let mut found = Vec::new();
for path in files {
let text =
std::fs::read_to_string(&path).unwrap_or_else(|e| panic!("{}: {e}", path.display()));
if text.contains("DEADLOCK_GUARD") {
continue;
}
let hits: Vec<String> = text
.lines()
.enumerate()
.filter(|(_, line)| {
!line.trim_start().starts_with("//") && TIMED_WAITS.iter().any(|t| line.contains(t))
})
.map(|(i, _)| format!("{}:{}", path.display(), i + 1))
.collect();
if path.ends_with("unityrs-batch/src/progress/early.rs") {
if hits.len() != 1 {
found.push(exemption_broken(&path, hits.len(), "timed waits"));
}
continue;
}
if path.ends_with("unityrs-batch/src/sys/borrowed.rs") {
if hits.len() != 1 {
found.push(exemption_broken(&path, hits.len(), "timed waits"));
}
continue;
}
if path.ends_with("unityrs-batch/src/execution.rs") {
if hits.len() != 5 {
found.push(format!(
"{}: {} timed waits, the exemption is for two startup deadlines, two channel-priming waits and one startup-gate priming wait",
path.display(),
hits.len()
));
}
continue;
}
if path.ends_with("unityrs-batch/src/lifetime/runtimes.rs") {
if hits.len() != 2 {
found.push(exemption_broken(&path, hits.len(), "W20 startup waits"));
}
continue;
}
if path.ends_with("unityrs-batch/src/progress/live.rs") {
if hits.len() != 3 {
found.push(format!(
"{}: {} timed waits, the exemption is for two normal progress waits and one startup priming wait",
path.display(),
hits.len()
));
}
continue;
}
found.extend(hits);
}
found
}
pub(crate) fn exemption_broken(path: &Path, n: usize, what: &str) -> String {
format!("{}: {n} {what}, the exemption is for one", path.display())
}
pub(crate) fn rust_files(dir: &Path, out: &mut Vec<std::path::PathBuf>) {
for entry in std::fs::read_dir(dir).unwrap_or_else(|e| panic!("{}: {e}", dir.display())) {
let path = entry
.unwrap_or_else(|e| panic!("{}: {e}", dir.display()))
.path();
if path.is_dir() {
if path.file_name().is_none_or(|n| n != "target") {
rust_files(&path, out);
}
} else if path.extension().is_some_and(|e| e == "rs") {
out.push(path);
}
}
}
#[cfg(test)]
mod tests {
use super::{finishes, hold, recv, unguarded, wait};
#[test]
fn an_exempt_writer_with_two_waits_is_reported() {
let dir = crate::scratch::Scratch::new("guard-exemption");
let file = dir.join("unityrs-batch/src/progress/early.rs");
std::fs::create_dir_all(file.parent().unwrap()).unwrap();
std::fs::write(&file, "rx.recv_timeout(a);\nrx.recv_timeout(b);\n").unwrap();
assert_eq!(
unguarded(dir.path()),
[format!(
"{}: 2 timed waits, the exemption is for one",
file.display()
)]
);
}
#[test]
fn the_borrowed_writer_s_exemption_is_for_one_wait() {
let dir = crate::scratch::Scratch::new("guard-borrowed");
let file = dir.join("unityrs-batch/src/sys/borrowed.rs");
std::fs::create_dir_all(file.parent().unwrap()).unwrap();
for n in [1, 2] {
std::fs::write(&file, "rx.recv_timeout(a);\n".repeat(n)).unwrap();
assert_eq!(unguarded(dir.path()).len(), usize::from(n != 1));
}
}
#[test]
fn the_startup_exemption_counts_two_deadlines_and_three_priming_waits() {
let dir = crate::scratch::Scratch::new("startup-waits");
let file = dir.join("unityrs-batch/src/execution.rs");
std::fs::create_dir_all(file.parent().unwrap()).unwrap();
for n in [4, 5, 6] {
std::fs::write(&file, "rx.recv_timeout(a);\n".repeat(n)).unwrap();
let found = unguarded(dir.path());
if n == 5 {
assert_eq!(found.len(), 0);
} else {
assert_eq!(found.len(), 1);
}
}
}
#[test]
fn progress_exemption_counts_heartbeat_acknowledgment_and_startup_priming() {
let dir = crate::scratch::Scratch::new("progress-waits");
let file = dir.join("unityrs-batch/src/progress/live.rs");
std::fs::create_dir_all(file.parent().unwrap()).unwrap();
for n in [2, 3, 4] {
std::fs::write(&file, "changed.wait_timeout(state, wait);\n".repeat(n)).unwrap();
let found = unguarded(dir.path());
assert_eq!(found.len(), usize::from(n != 3));
}
}
#[test]
fn every_timed_wait_in_the_workspace_takes_the_guard() {
let crates = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("..");
let found = unguarded(&crates);
assert!(
found.is_empty(),
"timed waits with a limit of their own, which load can outlast:\n{}",
found.join("\n")
);
}
#[test]
fn an_answer_comes_back() {
assert_eq!(finishes("a sum", || 1 + 1), 2);
let (tx, rx) = std::sync::mpsc::channel();
tx.send(3).unwrap();
assert_eq!(recv(&rx, "a sent value"), 3);
}
#[test]
#[should_panic(expected = "panicked without answering")]
fn a_thread_that_panics_fails_at_once_and_says_so() {
let (tx, rx) = std::sync::mpsc::channel::<()>();
drop(tx);
wait(&rx, "a signal");
}
#[test]
fn a_hold_ends_when_the_test_lets_it_go_or_drops_the_sender() {
let (tx, rx) = std::sync::mpsc::channel();
tx.send(()).unwrap();
hold(&rx, "the test to let go");
drop(tx);
hold(&rx, "the test to drop its sender");
}
}