Skip to content

Commit f1c1396

Browse files
committed
fix(generate-lockfile): hold lock before querying index
1 parent 5587af7 commit f1c1396

File tree

3 files changed

+6
-8
lines changed

3 files changed

+6
-8
lines changed

src/cargo/ops/cargo_generate_lockfile.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,12 +166,17 @@ pub fn update_lockfile(ws: &Workspace<'_>, opts: &UpdateOptions<'_>) -> CargoRes
166166
Ok(())
167167
}
168168

169+
/// Prints lockfile change statuses.
170+
///
171+
/// This would acquire the package-cache lock, as it may update the index to
172+
/// show users latest available versions.
169173
pub fn print_lockfile_changes(
170174
gctx: &GlobalContext,
171175
previous_resolve: Option<&Resolve>,
172176
resolve: &Resolve,
173177
registry: &mut PackageRegistry<'_>,
174178
) -> CargoResult<()> {
179+
let _lock = gctx.acquire_package_cache_lock(CacheLockMode::DownloadExclusive)?;
175180
if let Some(previous_resolve) = previous_resolve {
176181
print_lockfile_sync(gctx, previous_resolve, resolve, registry)
177182
} else {

src/cargo/ops/resolve.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -256,11 +256,6 @@ fn resolve_with_registry<'gctx>(
256256
false
257257
};
258258
if print {
259-
// We only want one Cargo at a time resolving a crate graph since this can
260-
// involve a lot of frobbing of the global caches.
261-
let _lock = ws
262-
.gctx()
263-
.acquire_package_cache_lock(CacheLockMode::DownloadExclusive)?;
264259
ops::print_lockfile_changes(ws.gctx(), prev.as_ref(), &resolve, registry)?;
265260
}
266261
Ok(resolve)

tests/testsuite/generate_lockfile.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -266,11 +266,9 @@ fn generate_lockfile_holds_lock_and_offline() {
266266
.run();
267267

268268
p.cargo("generate-lockfile --offline")
269-
.with_status(101)
270269
.with_stderr_contains(
271270
"\
272-
[..]thread 'main' panicked[..]
273-
[..]package cache lock is not currently held[..]
271+
[LOCKING] 2 packages
274272
",
275273
)
276274
.run();

0 commit comments

Comments
 (0)