Skip to content

Commit 12e6389

Browse files
committed
bootstrap: improve error recovery flags to curl
alternative to #128459 fixes #110178
1 parent 899eb03 commit 12e6389

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

src/bootstrap/bootstrap.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ def get(base, url, path, checksums, verbose=False):
7979
eprint("removing", temp_path)
8080
os.unlink(temp_path)
8181

82+
def curl_version():
83+
return float(re.match(bytes("^curl ([0-9]+\\.[0-9]+)", "utf8"), require(["curl", "-V"]))[1])
8284

8385
def download(path, url, probably_big, verbose):
8486
for _ in range(4):
@@ -107,11 +109,15 @@ def _download(path, url, probably_big, verbose, exception):
107109
# If curl is not present on Win32, we should not sys.exit
108110
# but raise `CalledProcessError` or `OSError` instead
109111
require(["curl", "--version"], exception=platform_is_win32())
110-
run(["curl", option,
112+
extra_flags = []
113+
if curl_version() > 7.70:
114+
extra_flags = [ "--retry-all-errors" ]
115+
run(["curl", option] + extra_flags + [
111116
"-L", # Follow redirect.
112117
"-y", "30", "-Y", "10", # timeout if speed is < 10 bytes/sec for > 30 seconds
113118
"--connect-timeout", "30", # timeout if cannot connect within 30 seconds
114119
"-o", path,
120+
"--continue-at", "-",
115121
"--retry", "3", "-SRf", url],
116122
verbose=verbose,
117123
exception=True, # Will raise RuntimeError on failure

src/bootstrap/src/core/download.rs

+31
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,31 @@ fn try_run(config: &Config, cmd: &mut Command) -> Result<(), ()> {
2121
config.try_run(cmd)
2222
}
2323

24+
fn extract_curl_version(out: &[u8]) -> f32 {
25+
let out = &out[5..];
26+
let Some(i) = out.iter().position(|&x| x == b' ') else { return 0.0 };
27+
let out = &out[..i];
28+
let Some(k) = out.iter().rev().position(|&x| x == b'.') else { return 0.0 };
29+
let out = &out[..out.len()-k-1];
30+
std::str::from_utf8(out).unwrap().parse().unwrap_or(0.0)
31+
}
32+
33+
#[test]
34+
fn test_extract_curl_version() {
35+
assert_eq!(extract_curl_version(b"\
36+
curl 8.4.0 (x86_64-pc-linux-gnu) libcurl/8.4.0 \
37+
OpenSSL/3.0.13 zlib/1.3 brotli/1.1.0 zstd/1.5.5 libidn2/2.3.4 \
38+
libssh2/1.11.0 nghttp2/1.57.0"), 8.4);
39+
}
40+
41+
fn curl_version() -> f32 {
42+
let mut curl = Command::new("curl");
43+
curl.arg("-V");
44+
let Ok(out) = curl.output() else { return 0.0 };
45+
let out = out.stdout;
46+
extract_curl_version(&out)
47+
}
48+
2449
/// Generic helpers that are useful anywhere in bootstrap.
2550
impl Config {
2651
pub fn is_verbose(&self) -> bool {
@@ -219,6 +244,8 @@ impl Config {
219244
"30", // timeout if cannot connect within 30 seconds
220245
"-o",
221246
tempfile.to_str().unwrap(),
247+
"--continue-at",
248+
"-",
222249
"--retry",
223250
"3",
224251
"-SRf",
@@ -229,6 +256,10 @@ impl Config {
229256
} else {
230257
curl.arg("--progress-bar");
231258
}
259+
// --retry-all-errors was added in 7.71.0, don't use it if curl is old.
260+
if dbg!(curl_version()) > 7.70 {
261+
curl.arg("--retry-all-errors");
262+
}
232263
curl.arg(url);
233264
if !self.check_run(&mut curl) {
234265
if self.build.contains("windows-msvc") {

0 commit comments

Comments
 (0)