Skip to content

cargo generate-lockfile panicked when --offline #13656

Closed
@weihanglo

Description

@weihanglo

Problem

Got a panic when generating lockfile:

thread 'main' panicked at src/cargo/sources/registry/download.rs:166:21:
package cache lock is not currently held, Cargo forgot to call `acquire_package_cache_lock` before we got to this stack frame

Backtrace

stack backtrace:
   0: _rust_begin_unwind
   1: core::panicking::panic_fmt
   2: <cargo::util::context::GlobalContext>::assert_package_cache_locked
   3: cargo::sources::registry::download::is_crate_downloaded
   4: <hashbrown::raw::RawIterRange<(semver::Version, cargo::sources::registry::index::MaybeIndexSummary)>>::fold_impl::<<hashbrown::map::IterMut<semver::Version, cargo::sources::registry::index::MaybeIndexSummary> as core::iter::traits::iterator::Iterator>::fold<(), core::iter::adapters::filter_map::filter_map_fold<(&semver::Version, &mut cargo::sources::registry::index::MaybeIndexSummary), &mut cargo::sources::registry::index::MaybeIndexSummary, (), <cargo::sources::registry::index::RegistryIndex>::summaries::{closure#0}, core::iter::adapters::filter_map::filter_map_fold<&mut cargo::sources::registry::index::MaybeIndexSummary, &cargo::sources::registry::index::IndexSummary, (), <cargo::sources::registry::index::RegistryIndex>::summaries::{closure#1}, core::iter::adapters::map::map_fold<&cargo::sources::registry::index::IndexSummary, cargo::sources::registry::index::IndexSummary, (), <cargo::sources::registry::index::RegistryIndex>::query_inner_with_online::{closure#0}, core::iter::traits::iterator::Iterator::for_each::call<cargo::sources::registry::index::IndexSummary, &mut dyn core::ops::function::FnMut<(cargo::sources::registry::index::IndexSummary,), Output = ()>>::{closure#0}>::{closure#0}>::{closure#0}>::{closure#0}>::{closure#0}, ()>
   5: <cargo::sources::registry::index::RegistryIndex>::query_inner
   6: <cargo::sources::registry::RegistrySource as cargo::sources::source::Source>::query
   7: <cargo::sources::replaced::ReplacedSource as cargo::sources::source::Source>::query
   8: <cargo::core::registry::PackageRegistry as cargo::core::registry::Registry>::query
   9: <cargo::core::registry::PackageRegistry as cargo::core::registry::Registry>::query_vec
  10: cargo::ops::cargo_generate_lockfile::print_lockfile_changes
  11: cargo::ops::cargo_generate_lockfile::generate_lockfile
  12: cargo::commands::generate_lockfile::exec
  13: <cargo::cli::Exec>::exec
  14: cargo::cli::main
  15: cargo::main
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace

This was introduced in #13561

Steps

cargo new foo
cd foo
cargo add empty-library
cargo +nightly generate-lockfile --offline

Possible Solution(s)

Hold the lock before querying registry even when --offline, since offline query still checks downloaded .crate tarball while querying:

// First filter summaries for `--offline`. If we're online then
// everything is a candidate, otherwise if we're offline we're only
// going to consider candidates which are actually present on disk.
//
// Note: This particular logic can cause problems with
// optional dependencies when offline. If at least 1 version
// of an optional dependency is downloaded, but that version
// does not satisfy the requirements, then resolution will
// fail. Unfortunately, whether or not something is optional
// is not known here.
.map(|s| {
if online || load.is_crate_downloaded(s.package_id()) {
s.clone()
} else {
IndexSummary::Offline(s.as_summary().clone())
}
})

Notes

No response

Version

cargo 1.79.0-nightly (a510712d0 2024-03-25)
release: 1.79.0-nightly
commit-hash: a510712d05c6c98f987af24dd73cdfafee8922e6
commit-date: 2024-03-25

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions