Skip to content

Commit eb1396a

Browse files
committed
fix shell borrow state debug asserts
1 parent 4407507 commit eb1396a

File tree

3 files changed

+15
-9
lines changed

3 files changed

+15
-9
lines changed

src/cargo/core/registry.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -788,11 +788,9 @@ impl<'gctx> Registry for PackageRegistry<'gctx> {
788788

789789
#[tracing::instrument(skip_all)]
790790
fn block_until_ready(&mut self) -> CargoResult<()> {
791-
if cfg!(debug_assertions) {
792-
// Force borrow to catch invalid borrows, regardless of which source is used and how it
793-
// happens to behave this time
794-
self.gctx.shell().verbosity();
795-
}
791+
// Force borrow to catch invalid borrows, regardless of which source is used and how it
792+
// happens to behave this time
793+
self.gctx.debug_assert_shell_not_borrowed();
796794
for (source_id, source) in self.sources.sources_mut() {
797795
source
798796
.block_until_ready()

src/cargo/util/context/mod.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,16 @@ impl GlobalContext {
413413
self.shell.lock().unwrap()
414414
}
415415

416+
/// If debug assertions are enabled, checks that `shell` is not borrowed
417+
pub fn debug_assert_shell_not_borrowed(&self) {
418+
if cfg!(debug_assertions) {
419+
match self.shell.try_lock() {
420+
Ok(_) | Err(std::sync::TryLockError::Poisoned(_)) => (),
421+
Err(std::sync::TryLockError::WouldBlock) => panic!("shell is borrowed!"),
422+
}
423+
}
424+
}
425+
416426
/// Gets the path to the `rustdoc` executable.
417427
pub fn rustdoc(&self) -> CargoResult<&Path> {
418428
self.rustdoc

src/cargo/util/flock.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -392,10 +392,8 @@ fn acquire(
392392
lock_try: &dyn Fn() -> io::Result<()>,
393393
lock_block: &dyn Fn() -> io::Result<()>,
394394
) -> CargoResult<()> {
395-
if cfg!(debug_assertions) {
396-
// Force borrow to catch invalid borrows outside of contention situations
397-
gctx.shell().verbosity();
398-
}
395+
// Force borrow to catch invalid borrows outside of contention situations
396+
gctx.debug_assert_shell_not_borrowed();
399397
if try_acquire(path, lock_try)? {
400398
return Ok(());
401399
}

0 commit comments

Comments
 (0)