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

Improve error message when execution STATE is unavailable #242

Merged
merged 1 commit into from
Nov 24, 2021

Conversation

jamesbornholt
Copy link
Contributor

A mistake we sometimes make when working on a large code base that uses Loom for testing is messing up our conditional imports, such that either a non-Loom-controlled std::thread is constructed or a Loom synchronization primitive is accessed from outside the context of a Loom model.

Here's a simple example distilled from one such mistake I made just now:

#[test]
fn thing() {
    loom::model(|| {
        let lock = Arc::new(Mutex::new(0));

        let thd = {
            let lock = lock.clone();
            // oops, wrong `thread`, not controlled by loom
            std::thread::spawn(move || {
                *lock.lock().unwrap() += 1;
            })
        };

        thd.join().unwrap();

        let lock = Arc::try_unwrap(lock).unwrap().into_inner().unwrap();
        assert_eq!(lock, 1);
    })
}

(Usually, the thread spawn happens in some other piece of code, so the problem is less easy to spot).

When this test fails, it gives a fairly opaque error message from Loom's internals:

thread '<unnamed>' panicked at 'cannot access a scoped thread local variable without calling `set` first', /Users/bornholt/.cargo/registry/src/jackfan.us.kg-1ecc6299db9ec823/scoped-tls-1.0.0/src/lib.rs:168:9
stack backtrace:
   0: std::panicking::begin_panic
             at /rustc/09c42c45858d5f3aedfa670698275303a3d19afa/library/std/src/panicking.rs:543:12
   1: scoped_tls::ScopedKey<T>::with
             at /Users/bornholt/.cargo/registry/src/jackfan.us.kg-1ecc6299db9ec823/scoped-tls-1.0.0/src/lib.rs:168:9
   2: loom::rt::scheduler::Scheduler::with_execution
             at ./src/rt/scheduler.rs:48:9
   3: loom::rt::execution
             at ./src/rt/mod.rs:156:5

This change just gives a more suggestive error message about what's going wrong:

thread '<unnamed>' panicked at 'cannot access Loom execution state from outside a Loom model. are you accessing a synchronization primitive from outside a Loom test (a call to `model` or `check`)?', src/rt/scheduler.rs:127:13
stack backtrace:
   0: std::panicking::begin_panic
             at /rustc/09c42c45858d5f3aedfa670698275303a3d19afa/library/std/src/panicking.rs:543:12
   1: loom::rt::scheduler::Scheduler::with_state
             at ./src/rt/scheduler.rs:127:13
   2: loom::rt::scheduler::Scheduler::with_execution
             at ./src/rt/scheduler.rs:48:9
   3: loom::rt::execution
             at ./src/rt/mod.rs:156:5

Copy link
Member

@hawkw hawkw left a comment

Choose a reason for hiding this comment

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

looks good to me! one minor suggestion

src/rt/scheduler.rs Outdated Show resolved Hide resolved
This path can trigger if user code accesses synchronization primitives
from outside a Loom execution (e.g., create a `Mutex` before calling
`check()`). Currently it fails with an opaque error message about a
scoped thread local being unavailable. This change adds a more helpful
error message.
@Darksonn Darksonn merged commit e5d99d9 into tokio-rs:master Nov 24, 2021
hawkw added a commit that referenced this pull request Dec 3, 2021
# 0.5.4 (December 3, 2021)

### Added

- cell: Add `ConstPtr` and `MutPtr` RAII guards to `UnsafeCell` (#219)

### Changed

- Improve error message when execution state is unavailable (such as
  when running outside of `loom::model`) (#242)
hawkw added a commit that referenced this pull request Dec 3, 2021
# 0.5.4 (December 3, 2021)

### Added

- cell: Add `ConstPtr` and `MutPtr` RAII guards to `UnsafeCell` (#219)

### Changed

- Improve error message when execution state is unavailable (such as
  when running outside of `loom::model`) (#242)
hawkw added a commit that referenced this pull request Dec 3, 2021
# 0.5.4 (December 3, 2021)

### Added

- cell: Add `ConstPtr` and `MutPtr` RAII guards to `UnsafeCell` (#219)

### Changed

- Improve error message when execution state is unavailable (such as
  when running outside of `loom::model`) (#242)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants