rust-lang / rust-lang/rust-clippy
Unexpected false positive `await_holding_lock` with `future-parking_lot` crate.
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
Summary
When running clippy on async code using the future-parking-lot mutex, clippy will warn about holding the MutexGuard across an await point:
warning: this `MutexGuard` is held across an `await` point
--> components/box/src/slab/slab.rs:585:21
|
585 | let _lock = self.reserve_mutex.future_lock().await;
| ^^^^^
|
= help: consider using an async-aware `Mutex` type or ensuring the `MutexGuard` is dropped before calling await
the same code using futures_util:🔒:Mutex does not generate a clippy warning. This has been observed with version "0.3.3".
Lint Name
await_holding_lock
Reproducer
I tried this code:
[dependencies]
future-parking_lot = "0.3.3"
smol = "1.3.0"
use future_parking_lot::mutex::{Mutex, FutureLockable};
async fn do_it_slow() {
std::thread::sleep(std::time::Duration::from_secs(1));
}
async fn do_it_under_lock() {
let mutex = Mutex::new(());
let _lock = mutex.future_lock().await;
do_it_slow().await;
}
fn main() {
smol::block_on(async {
do_it_under_lock().await
});
}
I saw this happen:
warning: this `MutexGuard` is held across an `await` point
--> src/main.rs:9:9
|
9 | let _lock = mutex.future_lock().await;
| ^^^^^
|
= help: consider using an async-aware `Mutex` type or ensuring the `MutexGuard` is dropped before calling await
note: these are all the `await` points this lock is held through
--> src/main.rs:9:5
|
9 | / let _lock = mutex.future_lock().await;
10 | | do_it_slow().await;
11 | | }
| |_^
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#await_holding_lock
= note: `#[warn(clippy::await_holding_lock)]` on by default
warning: `clippy_test` (bin "clippy_test") generated 1 warning
Finished dev [unoptimized + debuginfo] target(s) in 0.03s
I expected to see this happen:
I expected the same output as if I had run this program:
[dependencies]
smol = "1.3.0"
futures-util = "0.3.26"
use futures_util::lock::Mutex;
async fn do_it_slow() {
std::thread::sleep(std::time::Duration::from_secs(1));
}
async fn do_it_under_lock() {
let mutex = Mutex::new(());
let _lock = mutex.lock().await;
do_it_slow().await;
}
fn main() {
smol::block_on(async {
do_it_under_lock().await
});
}
Version
rustc 1.66.1 (90743e729 2023-01-10)
binary: rustc
commit-hash: 90743e7298aca107ddaa0c202a4d3604e29bfeb6
commit-date: 2023-01-10
host: x86_64-unknown-linux-gnu
release: 1.66.1
LLVM version: 15.0.2
Additional Labels
No response
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running the supplied reproducer in src/main.rs with future-parking_lot 0.3.3 and smol 1.3.0, then compare it with the futures-util:🔒:Mutex example. Trace the await_holding_lock lint behavior for future_lock and confirm the completed change no longer reports this false positive while preserving the expected futures-util behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100