Skip to content

Commit

Permalink
fix: Fix deadlock introduced in 0b71083
Browse files Browse the repository at this point in the history
  • Loading branch information
Jules-Bertholet authored May 29, 2023
1 parent 0b71083 commit 90b1a30
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/rwlock/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -539,12 +539,14 @@ impl<'a> Future for RawUpgrade<'a> {
}

impl<'a> Drop for RawUpgrade<'a> {
#[inline]
fn drop(&mut self) {
if let Some(lock) = self.lock {
// Unset `WRITER_BIT`.
lock.state.fetch_and(!WRITER_BIT, Ordering::SeqCst);
// Trigger the "no writer" event.
lock.no_writer.notify(1);
// SAFETY: we are dropping the future that would give us a write lock,
// so we don't need said lock anymore.
unsafe {
lock.write_unlock();
}
}
}
}
29 changes: 29 additions & 0 deletions tests/rwlock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,35 @@ fn yields_when_contended() {
);
}

#[test]
fn cancellation() {
future::block_on(async {
let rw = RwLock::new(());

drop(rw.read());

drop(rw.upgradable_read());

drop(rw.write());

let read = rw.read().await;
drop(read);

let upgradable_read = rw.upgradable_read().await;
drop(upgradable_read);

let write = rw.write().await;
drop(write);

let upgradable_read = rw.upgradable_read().await;
drop(RwLockUpgradableReadGuard::upgrade(upgradable_read));

let upgradable_read = rw.upgradable_read().await;
let write = RwLockUpgradableReadGuard::upgrade(upgradable_read).await;
drop(write);
});
}

// We are testing that this compiles.
fn _covariance_test<'g>(guard: RwLockReadGuard<'g, &'static ()>) {
let _: RwLockReadGuard<'g, &'g ()> = guard;
Expand Down

0 comments on commit 90b1a30

Please sign in to comment.