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

bootstrap: improve error recovery flags to curl #129134

Merged
merged 5 commits into from
Aug 25, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
11 changes: 10 additions & 1 deletion src/bootstrap/bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ def get(base, url, path, checksums, verbose=False):
eprint("removing", temp_path)
os.unlink(temp_path)

def curl_version():
m = re.match(bytes("^curl ([0-9]+)\\.([0-9]+)", "utf8"), require(["curl", "-V"]))
if m is None:
return (0, 0)
return (int(m[1]), int(m[2]))

def download(path, url, probably_big, verbose):
for _ in range(4):
Expand Down Expand Up @@ -107,11 +112,15 @@ def _download(path, url, probably_big, verbose, exception):
# If curl is not present on Win32, we should not sys.exit
# but raise `CalledProcessError` or `OSError` instead
require(["curl", "--version"], exception=platform_is_win32())
run(["curl", option,
extra_flags = []
if curl_version() > (7, 70):
extra_flags = [ "--retry-all-errors" ]
run(["curl", option] + extra_flags + [
"-L", # Follow redirect.
"-y", "30", "-Y", "10", # timeout if speed is < 10 bytes/sec for > 30 seconds
"--connect-timeout", "30", # timeout if cannot connect within 30 seconds
"-o", path,
"--continue-at", "-",
"--retry", "3", "-SRf", url],
verbose=verbose,
exception=True, # Will raise RuntimeError on failure
Expand Down
27 changes: 27 additions & 0 deletions src/bootstrap/src/core/download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,27 @@ fn try_run(config: &Config, cmd: &mut Command) -> Result<(), ()> {
config.try_run(cmd)
}

fn extract_curl_version(out: &[u8]) -> (u16, u16) {
let out = &out[5..];
Kobzol marked this conversation as resolved.
Show resolved Hide resolved
let Some(i) = out.iter().position(|&x| x == b' ') else { return (0, 0) };
let out = &out[..i];
let Some(k) = out.iter().rev().position(|&x| x == b'.') else { return (0, 0) };
let out = &out[..out.len() - k - 1];
let Ok(s) = std::str::from_utf8(out) else { return (0, 0) };
let parts = s.split('.').collect::<Vec<_>>();
let [s_major, s_minor] = &parts[..] else { return (0, 0) };
let (Ok(major), Ok(minor)) = (s_major.parse(), s_minor.parse()) else { return (0, 0) };
(major, minor)
}

fn curl_version() -> (u16, u16) {
let mut curl = Command::new("curl");
curl.arg("-V");
let Ok(out) = curl.output() else { return (0, 0) };
let out = out.stdout;
extract_curl_version(&out)
}

/// Generic helpers that are useful anywhere in bootstrap.
impl Config {
pub fn is_verbose(&self) -> bool {
Expand Down Expand Up @@ -219,6 +240,8 @@ impl Config {
"30", // timeout if cannot connect within 30 seconds
"-o",
tempfile.to_str().unwrap(),
"--continue-at",
"-",
"--retry",
"3",
"-SRf",
Expand All @@ -229,6 +252,10 @@ impl Config {
} else {
curl.arg("--progress-bar");
}
// --retry-all-errors was added in 7.71.0, don't use it if curl is old.
if curl_version() > (7, 70) {
curl.arg("--retry-all-errors");
}
curl.arg(url);
if !self.check_run(&mut curl) {
if self.build.contains("windows-msvc") {
Expand Down
Loading