tokio-rs / loom

Concurrency permutation testing tool for Rust.
MIT License
2.14k stars 111 forks source link

Parallel loom tests race and cause `panicked at 'Box<Any>'` #146

Open faern opened 4 years ago

faern commented 4 years ago

If I create a new empty crate, add loom as a dependency and these two tests in src/lib.rs:

#[test]
fn test1() {
    loom::model(|| ());
}

#[test]
fn test2() {
    loom::model(|| ());
}

Then I run this in a loop with:

while RUST_BACKTRACE=full cargo test -- --nocapture; do true; done

Then it will sometimes result in the following panic:

running 2 tests
Completed in 1 iterations
Completed in 1 iterations
thread 'test2' panicked at 'Box<Any>', /Users/faern/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/src/rust/src/libstd/macros.rs:13:23
stack backtrace:
   0:        0x10f2b0c1f - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::h9b566daa2a455421
                               at /Users/faern/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/src/rust/src/libcore/cell.rs:1643
   1:        0x10f2d649e - core::fmt::write::ha1b9f49163694e4d
                               at /Users/faern/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/src/rust/src/libcore/cell.rs:1643
   2:        0x10f2ad647 - std::io::Write::write_fmt::h5bf7ecb3d7765923
                               at /Users/faern/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/src/rust/src/libcore/cell.rs:1643
   3:        0x10f2b2f8a - std::panicking::default_hook::{{closure}}::h694d41cd08e58408
                               at /Users/faern/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/src/rust/src/libcore/cell.rs:1643
   4:        0x10f2b2ccc - std::panicking::default_hook::ha6f28bd33ba2e689
                               at /Users/faern/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/src/rust/src/libcore/cell.rs:1643
   5:        0x10f2b3558 - std::panicking::rust_panic_with_hook::hb00d3c1dea212869
                               at /Users/faern/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/src/rust/src/libcore/cell.rs:1643
   6:        0x10f2db907 - std::panicking::begin_panic::hb693b834b4644a40
                               at /Users/faern/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/src/rust/src/libcore/cell.rs:1643
   7:        0x10f2525a0 - generator::yield_::raw_yield::h30b3e9da3f8fe51b
                               at /Users/faern/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/src/rust/src/libstd/macros.rs:13
   8:        0x10f252490 - generator::yield_::yield_::h11333a583174f4a8
                               at /Users/faern/.cargo/registry/src/github.com-1ecc6299db9ec823/generator-0.6.20/src/yield_.rs:106
   9:        0x10f245ae5 - loom::rt::scheduler::spawn_threads::{{closure}}::{{closure}}::hf77784205a5f75de
                               at /Users/faern/.cargo/git/checkouts/loom-1f4be9fd68e6b59b/0b092a2/src/rt/scheduler.rs:138
  10:        0x10f25858d - generator::gen_impl::GeneratorImpl<A,T>::init::{{closure}}::h925cbbe0b99895c0
                               at /Users/faern/.cargo/registry/src/github.com-1ecc6299db9ec823/generator-0.6.20/src/gen_impl.rs:154
  11:        0x10f253edb - core::ops::function::FnOnce::call_once{{vtable.shim}}::h43a9b1ff9e3ae9de
                               at /Users/faern/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/src/rust/src/libcore/ops/function.rs:232
  12:        0x10f262596 - <alloc::boxed::Box<F> as core::ops::function::FnOnce<A>>::call_once::h9921eb92259c04a7
                               at /Users/faern/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/src/rust/src/libstd/panicking.rs:0
  13:        0x10f2651b7 - generator::gen_impl::gen_init::{{closure}}::hbaf1b6aa22ef8d8a
                               at /Users/faern/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/src/rust/src/libcore/cell.rs:1643
  14:        0x10f261e95 - core::ops::function::FnOnce::call_once::h0862c3dbef3b976c
                               at /Users/faern/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/src/rust/src/libstd/panicking.rs:0
  15:        0x10f2618fa - std::panicking::try::do_call::h567763505b5fcdd6
                               at /Users/faern/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/src/rust/src/libstd/panicking.rs:331
  16:        0x10f261b0d - __rust_try
                               at /Users/faern/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/src/rust/src/libstd/panicking.rs:0
  17:        0x10f261875 - std::panicking::try::h60348e4f56614890
                               at /Users/faern/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/src/rust/src/libstd/panicking.rs:274
  18:        0x10f261dc1 - std::panic::catch_unwind::hec7f19f75e187d04
                               at /Users/faern/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/src/rust/src/libstd/panicking.rs:0
  19:        0x10f26500e - generator::gen_impl::gen_init::h5497ff06b8dbc23d
                               at /Users/faern/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/src/rust/src/libcore/cell.rs:1643
test test2 ... ok
test test1 ... ok

This panic does not happen if I add --test-threads=1 to the test run. So it seems multiple loom tests ran in parallel race and cause this.

Tested and reproduced on macOS and Linux with loom 0.3.2, 0.3.4 and latest master branch from git. Both on latest stable Rust (1.43) and a recent nightly.

As seen from the output, both tests succeed, the exit code of cargo test is still zero. So maybe this is not critical. But it is also not optimal as it confuses the reader of the test output.