Skip to content

Commit 041678c

Browse files
committed
fix(publish): Block until it is in index
Originally, crates.io would block on publish requests until the publish was complete, giving `cargo publish` this behavior by extension. When crates.io switched to asynchronous publishing, this intermittently broke people's workflows when publishing multiple crates. I say interittent because it usually works until it doesn't and it is unclear why to the end user because it will be published by the time they check. In the end, callers tend to either put in timeouts (and pray), poll the server's API, or use `crates-index` crate to poll the index. This isn't sufficient because - For any new interested party, this is a pit of failure they'll fall into - crates-index has re-implemented index support incorrectly in the past, currently doesn't handle auth, doesn't support `git-cli`, etc. - None of these previous options work if we were to implement workspace-publish support (rust-lang#1169) - The new sparse registry might increase the publish times, making the delay easier to hit manually - The new sparse registry goes through CDNs so checking the server's API might not be sufficient - Once the sparse registry is available, crates-index users will find out when the package is ready in git but it might not be ready through the sparse registry because of CDNs So now `cargo` will block until it sees the package in the index. - This is checking via the index instead of server APIs in case there are propagation delays. This has the side effect of being noisy because of all of the "Updating index" messages. - This is done unconditionally because cargo used to block and that didn't seem to be a problem, blocking by default is the less error prone case, and there doesn't seem to be enough justification for a "don't block" flag. The timeout was 5min but I dropped it to 1m. Unfortunately, I don't have data from `cargo-release` to know what a reasonable timeout is, so going ahead and dropping to 60s and assuming anything more is an outage. Fixes rust-lang#9507
1 parent 41dfb26 commit 041678c

File tree

3 files changed

+76
-41
lines changed

3 files changed

+76
-41
lines changed

crates/cargo-test-support/src/compare.rs

+1
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ fn substitute_macros(input: &str) -> String {
197197
("[MIGRATING]", " Migrating"),
198198
("[EXECUTABLE]", " Executable"),
199199
("[SKIPPING]", " Skipping"),
200+
("[WAITING]", " Waiting"),
200201
];
201202
let mut result = input.to_owned();
202203
for &(pat, subst) in &macros {

src/cargo/ops/registry.rs

+64
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ use termcolor::Color::Green;
1818
use termcolor::ColorSpec;
1919

2020
use crate::core::dependency::DepKind;
21+
use crate::core::dependency::Dependency;
2122
use crate::core::manifest::ManifestMetadata;
2223
use crate::core::resolver::CliFeatures;
2324
use crate::core::source::Source;
25+
use crate::core::QueryKind;
2426
use crate::core::{Package, SourceId, Workspace};
2527
use crate::ops;
2628
use crate::ops::Packages;
@@ -183,6 +185,10 @@ pub fn publish(ws: &Workspace<'_>, opts: &PublishOpts<'_>) -> CargoResult<()> {
183185
reg_id,
184186
opts.dry_run,
185187
)?;
188+
if !opts.dry_run {
189+
let timeout = std::time::Duration::from_secs(60);
190+
wait_for_publish(opts.config, reg_id, pkg, timeout)?;
191+
}
186192

187193
Ok(())
188194
}
@@ -374,6 +380,64 @@ fn transmit(
374380
Ok(())
375381
}
376382

383+
fn wait_for_publish(
384+
config: &Config,
385+
registry_src: SourceId,
386+
pkg: &Package,
387+
timeout: std::time::Duration,
388+
) -> CargoResult<()> {
389+
let version_req = format!("={}", pkg.version());
390+
let mut source = registry_src.load(config, &HashSet::new())?;
391+
let source_description = source.describe();
392+
let query = Dependency::parse(pkg.name(), Some(&version_req), registry_src)?;
393+
394+
let now = std::time::Instant::now();
395+
let sleep_time = std::time::Duration::from_secs(1);
396+
let mut logged = false;
397+
loop {
398+
{
399+
let _lock = config.acquire_package_cache_lock()?;
400+
source.invalidate_cache();
401+
let summaries = loop {
402+
// Exact to avoid returning all for path/git
403+
match source.query_vec(&query, QueryKind::Exact) {
404+
std::task::Poll::Ready(res) => {
405+
break res?;
406+
}
407+
std::task::Poll::Pending => source.block_until_ready()?,
408+
}
409+
};
410+
if !summaries.is_empty() {
411+
break;
412+
}
413+
}
414+
415+
if timeout < now.elapsed() {
416+
config.shell().warn(format!(
417+
"timed out waiting for `{}` to be in {}",
418+
pkg.name(),
419+
source_description
420+
))?;
421+
break;
422+
}
423+
424+
if !logged {
425+
config.shell().status(
426+
"Waiting",
427+
format!(
428+
"on `{}` to propagate to {} (ctrl-c to wait asynchronously)",
429+
pkg.name(),
430+
source_description
431+
),
432+
)?;
433+
logged = true;
434+
}
435+
std::thread::sleep(sleep_time);
436+
}
437+
438+
Ok(())
439+
}
440+
377441
/// Returns the index and token from the config file for the given registry.
378442
///
379443
/// `registry` is typically the registry specified on the command-line. If

tests/testsuite/publish.rs

+11-41
Original file line numberDiff line numberDiff line change
@@ -2093,7 +2093,7 @@ fn http_api_not_noop() {
20932093
}
20942094

20952095
#[cargo_test]
2096-
fn delayed_publish_errors() {
2096+
fn wait_for_publish() {
20972097
// Counter for number of tries before the package is "published"
20982098
let arc: Arc<Mutex<u32>> = Arc::new(Mutex::new(0));
20992099
let arc2 = arc.clone();
@@ -2149,13 +2149,15 @@ Use the --token command-line flag to remove this warning.
21492149
See [..]
21502150
[PACKAGING] delay v0.0.1 ([CWD])
21512151
[UPLOADING] delay v0.0.1 ([CWD])
2152+
[UPDATING] `dummy-registry` index
2153+
[WAITING] on `delay` to propagate to `dummy-registry` index (ctrl-c to wait asynchronously)
21522154
",
21532155
)
21542156
.run();
21552157

2156-
// Check nothing has touched the responder
2158+
// Verify the responder has been pinged
21572159
let lock = arc2.lock().unwrap();
2158-
assert_eq!(*lock, 0);
2160+
assert_eq!(*lock, 2);
21592161
drop(lock);
21602162

21612163
let p = project()
@@ -2173,23 +2175,6 @@ See [..]
21732175
.file("src/main.rs", "fn main() {}")
21742176
.build();
21752177

2176-
p.cargo("build -Z sparse-registry")
2177-
.masquerade_as_nightly_cargo(&["sparse-registry"])
2178-
.with_status(101)
2179-
.with_stderr(
2180-
"\
2181-
[UPDATING] [..]
2182-
[ERROR] no matching package named `delay` found
2183-
location searched: registry `crates-io`
2184-
required by package `foo v0.0.1 ([..]/foo)`
2185-
",
2186-
)
2187-
.run();
2188-
2189-
let lock = arc2.lock().unwrap();
2190-
assert_eq!(*lock, 1);
2191-
drop(lock);
2192-
21932178
p.cargo("build -Z sparse-registry")
21942179
.masquerade_as_nightly_cargo(&["sparse-registry"])
21952180
.with_status(0)
@@ -2200,7 +2185,7 @@ required by package `foo v0.0.1 ([..]/foo)`
22002185
/// the responder twice per cargo invocation. If that ever gets changed
22012186
/// this test will need to be changed accordingly.
22022187
#[cargo_test]
2203-
fn delayed_publish_errors_underscore() {
2188+
fn wait_for_publish_underscore() {
22042189
// Counter for number of tries before the package is "published"
22052190
let arc: Arc<Mutex<u32>> = Arc::new(Mutex::new(0));
22062191
let arc2 = arc.clone();
@@ -2256,13 +2241,16 @@ Use the --token command-line flag to remove this warning.
22562241
See [..]
22572242
[PACKAGING] delay_with_underscore v0.0.1 ([CWD])
22582243
[UPLOADING] delay_with_underscore v0.0.1 ([CWD])
2244+
[UPDATING] `dummy-registry` index
2245+
[WAITING] on `delay_with_underscore` to propagate to `dummy-registry` index (ctrl-c to wait asynchronously)
22592246
",
22602247
)
22612248
.run();
22622249

2263-
// Check nothing has touched the responder
2250+
// Verify the repsponder has been pinged
22642251
let lock = arc2.lock().unwrap();
2265-
assert_eq!(*lock, 0);
2252+
// NOTE: package names with - or _ hit the responder twice per cargo invocation
2253+
assert_eq!(*lock, 3);
22662254
drop(lock);
22672255

22682256
let p = project()
@@ -2280,24 +2268,6 @@ See [..]
22802268
.file("src/main.rs", "fn main() {}")
22812269
.build();
22822270

2283-
p.cargo("build -Z sparse-registry")
2284-
.masquerade_as_nightly_cargo(&["sparse-registry"])
2285-
.with_status(101)
2286-
.with_stderr(
2287-
"\
2288-
[UPDATING] [..]
2289-
[ERROR] no matching package named `delay_with_underscore` found
2290-
location searched: registry `crates-io`
2291-
required by package `foo v0.0.1 ([..]/foo)`
2292-
",
2293-
)
2294-
.run();
2295-
2296-
let lock = arc2.lock().unwrap();
2297-
// package names with - or _ hit the responder twice per cargo invocation
2298-
assert_eq!(*lock, 2);
2299-
drop(lock);
2300-
23012271
p.cargo("build -Z sparse-registry")
23022272
.masquerade_as_nightly_cargo(&["sparse-registry"])
23032273
.with_status(0)

0 commit comments

Comments
 (0)