Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(registry): HttpRegistry block_until_ready returns early when work is still pending #14694

Merged
merged 2 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions src/cargo/sources/registry/http_remote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -789,26 +789,29 @@ impl<'gctx> RegistryData for HttpRegistry<'gctx> {
}

fn block_until_ready(&mut self) -> CargoResult<()> {
trace!(target: "network",
"block_until_ready: {} transfers pending",
trace!(target: "network::HttpRegistry::block_until_ready",
"{} transfers pending",
self.downloads.pending.len()
);
self.downloads.blocking_calls += 1;

loop {
self.handle_completed_downloads()?;
self.add_sleepers()?;

let remaining_in_multi = tls::set(&self.downloads, || {
self.multi
.perform()
.context("failed to perform http requests")
})?;
trace!(target: "network", "{} transfers remaining", remaining_in_multi);

// Handles transfers performed by `self.multi` above and adds to
// `self.downloads.results`. Failed transfers get added to
// `self.downloads.sleeping` for retry.
self.handle_completed_downloads()?;
if remaining_in_multi + self.downloads.sleeping.len() as u32 == 0 {
return Ok(());
}
// Handles failed transfers in `self.downloads.sleeping` and
// re-adds them to `self.multi`.
self.add_sleepers()?;

if self.downloads.pending.is_empty() {
let delay = self.downloads.sleeping.time_to_next().unwrap();
Expand Down
60 changes: 60 additions & 0 deletions tests/testsuite/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3314,6 +3314,66 @@ Caused by:
.run();
}

#[cargo_test]
fn sparse_blocking_count() {
let fail_count = Mutex::new(0);
let _registry = RegistryBuilder::new()
.http_index()
.add_responder("/index/3/b/bar", move |req, server| {
let mut fail_count = fail_count.lock().unwrap();
if *fail_count < 1 {
*fail_count += 1;
server.internal_server_error(req)
} else {
server.index(req)
}
})
.build();

let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.0.1"
edition = "2015"
authors = []

[dependencies]
bar = ">= 0.0.0"
"#,
)
.file("src/main.rs", "fn main() {}")
.build();

Package::new("bar", "0.0.1").publish();

// Ensure we have the expected number of `block_until_ready` calls.
// The 1st (0 transfers pending), is the deliberate extra call in `ensure_loaded` for a source.
// The 2nd (1 transfers pending), is the registry `config.json`.
// the 3rd (1 transfers pending), is the package metadata for `bar`.

p.cargo("check")
.env("CARGO_LOG", "network::HttpRegistry::block_until_ready=trace")
.with_stderr_data(str![[r#"
[..] TRACE network::HttpRegistry::block_until_ready: 0 transfers pending
[UPDATING] `dummy-registry` index
[..] TRACE network::HttpRegistry::block_until_ready: 1 transfers pending
[..] TRACE network::HttpRegistry::block_until_ready: 1 transfers pending
[WARNING] spurious network error (3 tries remaining): failed to get successful HTTP response from `[..]/index/3/b/bar` ([..]), got 500
body:
internal server error
[LOCKING] 1 package to latest compatible version
[DOWNLOADING] crates ...
[DOWNLOADED] bar v0.0.1 (registry `dummy-registry`)
[CHECKING] bar v0.0.1
[CHECKING] foo v0.0.1 ([ROOT]/foo)
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s

"#]]).run();
}

#[cargo_test]
fn sparse_retry_single() {
let fail_count = Mutex::new(0);
Expand Down