Skip to content

Commit caacef2

Browse files
committed
Retry downloads on network failure
Fixes #1280
1 parent fb788e3 commit caacef2

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

build_system/prepare.rs

+17-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::build_system::rustc_info::get_default_sysroot;
88
use super::build_sysroot::{BUILD_SYSROOT, ORIG_BUILD_SYSROOT, SYSROOT_RUSTC_VERSION, SYSROOT_SRC};
99
use super::path::{Dirs, RelPath};
1010
use super::rustc_info::{get_file_name, get_rustc_version};
11-
use super::utils::{copy_dir_recursively, spawn_and_wait, Compiler};
11+
use super::utils::{copy_dir_recursively, retry_spawn_and_wait, spawn_and_wait, Compiler};
1212

1313
pub(crate) fn prepare(dirs: &Dirs) {
1414
if RelPath::DOWNLOAD.to_path(dirs).exists() {
@@ -140,8 +140,22 @@ fn clone_repo_shallow_github(dirs: &Dirs, download_dir: &Path, user: &str, repo:
140140

141141
// Download zip archive
142142
let mut download_cmd = Command::new("curl");
143-
download_cmd.arg("--location").arg("--output").arg(&archive_file).arg(archive_url);
144-
spawn_and_wait(download_cmd);
143+
download_cmd
144+
.arg("--max-time")
145+
.arg("600")
146+
.arg("-y")
147+
.arg("30")
148+
.arg("-Y")
149+
.arg("10")
150+
.arg("--connect-timeout")
151+
.arg("30")
152+
.arg("--continue-at")
153+
.arg("-")
154+
.arg("--location")
155+
.arg("--output")
156+
.arg(&archive_file)
157+
.arg(archive_url);
158+
retry_spawn_and_wait(5, download_cmd);
145159

146160
// Unpack tar archive
147161
let mut unpack_cmd = Command::new("tar");

build_system/utils.rs

+16
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,22 @@ pub(crate) fn spawn_and_wait(mut cmd: Command) {
188188
}
189189
}
190190

191+
// Based on the retry function in rust's src/ci/shared.sh
192+
#[track_caller]
193+
pub(crate) fn retry_spawn_and_wait(tries: u64, mut cmd: Command) {
194+
for i in 1..tries+1 {
195+
if i != 1 {
196+
println!("Command failed. Attempt {i}/{tries}:");
197+
}
198+
if cmd.spawn().unwrap().wait().unwrap().success() {
199+
return;
200+
}
201+
std::thread::sleep(std::time::Duration::from_secs(i * 5));
202+
}
203+
println!("The command has failed after {tries} attempts.");
204+
process::exit(1);
205+
}
206+
191207
#[track_caller]
192208
pub(crate) fn spawn_and_wait_with_input(mut cmd: Command, input: String) -> String {
193209
let mut child = cmd

0 commit comments

Comments
 (0)