Skip to content
v0.1.1latest release34 skills2 categories4 sub-categories79 tested scripts157 reference docsContact us

runtime_panic.rs

Helper script for rust-borrow-checker-error-explainer · 158 lines

Programming / Rustv1.0.0Updated OCT 21 20267 references5 scripts
scripts/runtime_panic.rs
//! Explain runtime borrow failures in test or program output: RefCell `already borrowed`
//! panics and Mutex `PoisonError`s, linking each poisoned lock to the panic that poisoned it.
//!
//! Build: rustc -O --edition 2021 scripts/runtime_panic.rs -o /tmp/runtime_panic
//! Usage: runtime_panic test-output.txt | -   [--json] [--strict]
//! Exit:  0 done, 1 --strict and a panic is neither borrow-related nor a poisoning root cause,
//!        2 usage error or empty input.

mod catalog;
mod common;

use catalog::{tier_name, Pattern, RUNTIME_PATTERNS};
use common::{die, json_str, read_input};

const USAGE: &str = "usage: runtime_panic <FILE|-> [--json] [--strict]

Find panics in `cargo test` / program output and explain the borrow-related ones:
  BCK024 refcell-double-borrow  already borrowed: BorrowMutError / already mutably borrowed: BorrowError
  BCK025 mutex-poisoned         PoisonError from lock().unwrap(), linked to the earlier panic that
                                poisoned the lock (the real bug)
Understands the `panicked at FILE:L:C:` format (Rust 1.73+) and the older
`panicked at 'MSG', FILE:L:C` format.

  cargo test 2>&1 | runtime_panic -

options:
  --json    machine-readable report
  --strict  exit 1 if a panic is unrelated to borrowing (not a poisoning root cause either)
  -h, --help";

struct Panic {
    thread: String,
    location: String,
    message: String,
    test: String,
    pattern: Option<&'static Pattern>,
    root_of: Option<usize>, // index of the poisoned panic this one caused
    root: Option<usize>,    // for a PoisonError: index of the panic that poisoned the lock
}

fn parse(text: &str) -> Vec<Panic> {
    let lines: Vec<&str> = text.lines().collect();
    let mut out: Vec<Panic> = Vec::new();
    let mut test = String::new();
    for (i, line) in lines.iter().enumerate() {
        if let Some(name) = line.strip_prefix("---- ").and_then(|l| l.strip_suffix(" stdout ----")) {
            test = name.to_string();
            continue;
        }
        let Some(rest) = line.trim_start().strip_prefix("thread '") else { continue };
        let Some((thread, after)) = rest.split_once("' panicked at ") else { continue };
        let (location, message) = if let Some(old) = after.strip_prefix('\'') {
            // old format: 'MSG', FILE:L:C
            match old.rsplit_once("', ") {
                Some((msg, loc)) => (loc.to_string(), msg.to_string()),
                None => continue,
            }
        } else {
            // 1.73+: FILE:L:C:  then the message on the following line(s)
            let msg: Vec<&str> = lines[i + 1..]
                .iter()
                .take_while(|l| !l.trim().is_empty() && !l.starts_with("note:") && !l.trim_start().starts_with("thread '"))
                .copied()
                .collect();
            (after.trim_end_matches(':').to_string(), msg.join("\n"))
        };
        let lower = message.to_lowercase();
        let pattern = RUNTIME_PATTERNS.iter().find(|p| p.any.iter().any(|n| lower.contains(n)));
        out.push(Panic { thread: thread.into(), location, message, test: test.clone(), pattern, root_of: None, root: None });
    }
    // Link each PoisonError to the closest earlier panic that is not itself a poisoning symptom.
    for i in 0..out.len() {
        if out[i].pattern.map(|p| p.id) == Some("BCK025") {
            if let Some(j) = (0..i).rev().find(|&j| out[j].pattern.map(|p| p.id) != Some("BCK025")) {
                out[i].root = Some(j);
                out[j].root_of = Some(i);
            }
        }
    }
    out
}

fn print_text(panics: &[Panic]) {
    for (i, p) in panics.iter().enumerate() {
        let test = if p.test.is_empty() { String::new() } else { format!(" (test {})", p.test) };
        println!("#{} panic in thread '{}' at {}{}", i + 1, p.thread, p.location, test);
        println!("  message : {}", p.message.replace('\n', "\n            "));
        if let Some(pat) = p.pattern {
            println!("  pattern : {} {}: {}", pat.id, pat.name, pat.why);
            if let Some(r) = p.root {
                let root = &panics[r];
                println!("  root    : #{} thread '{}' at {}: {}", r + 1, root.thread, root.location, root.message);
            }
            for (k, f) in pat.fixes.iter().enumerate() {
                let prefix = if k == 0 { "  fixes   :" } else { "           " };
                println!("{prefix} {}. [{}] {}", k + 1, tier_name(f.tier), f.text);
            }
        } else if let Some(v) = p.root_of {
            println!("  role    : root cause: this panic happened while a lock was held and poisoned it (see #{})", v + 1);
        } else {
            println!("  not a borrow/ownership panic");
        }
        println!();
    }
    let borrow = panics.iter().filter(|p| p.pattern.is_some()).count();
    let roots = panics.iter().filter(|p| p.root_of.is_some()).count();
    if panics.is_empty() {
        println!("No panics found.");
    } else {
        println!(
            "{} panic(s): {} borrow-related, {} poisoning root cause(s), {} other.",
            panics.len(), borrow, roots, panics.len() - borrow - roots
        );
    }
}

fn print_json(panics: &[Panic]) {
    let items: Vec<String> = panics
        .iter()
        .map(|p| {
            let opt = |v: Option<usize>| v.map_or("null".to_string(), |n| (n + 1).to_string());
            format!(
                "{{\"thread\":{},\"location\":{},\"message\":{},\"test\":{},\"pattern\":{},\"root\":{},\"root_of\":{}}}",
                json_str(&p.thread), json_str(&p.location), json_str(&p.message), json_str(&p.test),
                p.pattern.map_or("null".to_string(), |x| json_str(x.id)), opt(p.root), opt(p.root_of)
            )
        })
        .collect();
    println!("{{\"panics\":[{}]}}", items.join(","));
}

fn main() {
    let args: Vec<String> = std::env::args().skip(1).collect();
    if args.iter().any(|a| a == "-h" || a == "--help") {
        println!("{USAGE}");
        return;
    }
    let json = args.iter().any(|a| a == "--json");
    let strict = args.iter().any(|a| a == "--strict");
    if let Some(bad) = args.iter().find(|a| a.starts_with("--") && !["--json", "--strict"].contains(&a.as_str())) {
        die(&format!("unknown option {bad}\n\n{USAGE}"));
    }
    let paths: Vec<&String> = args.iter().filter(|a| !a.starts_with("--")).collect();
    let [path] = paths.as_slice() else { die(&format!("expected one input file or -\n\n{USAGE}")) };
    let text = read_input(path);
    if text.trim().is_empty() {
        die("input is empty; capture it with `cargo test 2>&1 | tee test.txt`");
    }
    let panics = parse(&text);
    if json {
        print_json(&panics);
    } else {
        print_text(&panics);
    }
    let other = panics.iter().filter(|p| p.pattern.is_none() && p.root_of.is_none()).count();
    std::process::exit(if strict && other > 0 { 1 } else { 0 });
}
More in Rust

rust-cargo-workspace-designer

Design, review and fix Cargo workspaces: when to split a crate (compile time, boundaries, publishing, proc-macros), crates/* vs apps/ + crates/ layouts, virtual manifest vs root package, members/exclude/default-members, resolver 2 vs 3 (MSRV-aware, edition 2024), [workspace.package] and [workspace.dependencies] inheritance, [workspace.lints], root-only profiles (lto, codegen-units), additive features with dep: and weak ?/ syntax, feature unification pitfalls, internal path + version deps, publish = false, Cargo.lock and MSRV policy, xtask, CI with --locked, nextest, rust-cache, sccache, cargo-hack/deny/machete/udeps/semver-checks, and cargo build --timings. Ships a workspace auditor and crate-graph tool.

rust-error-handling-thiserror-anyhow-designer

Design and review Rust error handling with thiserror 2 and anyhow 1: library vs application split, typed error enums with #[from], #[source], #[error(transparent)] and #[non_exhaustive], ErrorKind patterns, context via .context()/.with_context(), bail!/ensure!, source() chains, {:#} and {:?} reports, RUST_BACKTRACE/RUST_LIB_BACKTRACE, Box<dyn Error + Send + Sync>, main -> anyhow::Result or ExitCode, unwrap/expect/panic policy, Option to Result, collecting iterators of Results, axum IntoResponse mapping, retryable errors, tracing, async Send bounds, testing with matches! and downcast_ref, no_std core::error::Error, and snafu, eyre/color-eyre, miette, error-stack.

rust-lifetime-annotation-advisor

Decide when Rust lifetime annotations are needed and which ones: the three elision rules, structs holding references (struct Parser<'a>), owned vs borrowed (String, &str, Cow<'a, str>, Arc<str>), one vs several lifetimes, 'a: 'b bounds, T: 'static misconceptions, Box<dyn Trait + 'a> default object lifetimes, impl Trait + '_ and edition 2024 capture rules with use<..>, variance and PhantomData, HRTB for<'a> Fn(&'a T), GATs and lending iterators, async fn and tokio::spawn 'static, self-referential structs (ouroboros, self_cell, indices, arenas), Rc/Arc to escape lifetime soup. Ships std-only Rust checkers for redundant, missing, unused and over-restrictive lifetimes.

rust-tokio-async-service-builder

Build and review production async Rust services on Tokio 1.x and axum 0.8: runtime choice (#[tokio::main] multi_thread vs current_thread, Builder), axum routing with /{id} paths, State, extractors, IntoResponse error types, axum::serve with_graceful_shutdown, tower/tower-http middleware (TraceLayer, TimeoutLayer, CorsLayer, ConcurrencyLimitLayer, RequestBodyLimitLayer), Arc<AppState>, std vs tokio Mutex, JoinSet, TaskTracker, CancellationToken, select! cancel safety, bounded mpsc, oneshot, broadcast, watch, actors, Semaphore, timeouts, retries, spawn_blocking, sqlx PgPool, reqwest Client reuse, tracing, tests with start_paused and oneshot, Dockerfile. Fixes 'future cannot be sent between threads safely', E0373, Rc or MutexGuard held across await, 'Cannot start a runtime from within a runtime', 'Path segments must not start with :'. Ships std-only Rust scanners and a compiling axum 0.8 scaffold.

rust-trait-design-reviewer

Review and design Rust traits and generic APIs: generics vs impl Trait vs dyn Trait (static vs dynamic dispatch), dyn compatibility / object safety (generic methods, Self returns, where Self: Sized, error[E0038] not dyn compatible / cannot be made into an object), associated types vs generic parameters, blanket impls, coherence and orphan rule (E0117, E0119, E0210), newtype, extension, sealed and marker traits, supertraits and trait upcasting (1.86), async fn in traits (1.75) and the Send bound problem (trait_variant, async-trait), RPITIT, GATs, From/Into/TryFrom/AsRef/Borrow, std trait checklist (Debug, Clone, PartialEq, Hash, Default, Display, Error, Send/Sync, serde feature), #[must_use], #[non_exhaustive], semver hazards, Deref abuse, builders, typestate, PhantomData, Box<dyn Error + Send + Sync>, Fn traits, mockall.

rust-unsafe-block-auditor

Audit unsafe Rust for soundness: inventory every unsafe block, unsafe fn, unsafe impl/trait, extern block and unsafe attribute, check // SAFETY: comments and # Safety docs, and classify each as sound, needs-justification or likely UB, proposing safe alternatives first. Covers the five unsafe superpowers, aliasing (Stacked/Tree Borrows), invalid values, MaybeUninit vs mem::zeroed/uninitialized, transmute, slice::from_raw_parts, Vec::set_len/from_raw_parts, get_unchecked, CString::as_ptr dangling, unsafe impl Send/Sync, Pin projection, FFI (extern "C", repr(C), C-unwind, Box::into_raw/from_raw), strict provenance, static mut and &raw, edition 2024 (unsafe extern, #[unsafe(no_mangle)], unsafe_op_in_unsafe_fn, env::set_var), Miri, sanitizers, cargo-fuzz, loom, cargo-geiger and clippy safety lints.