diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 661dbe9c4..23019cb41 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -204,6 +204,16 @@ jobs: echo "EOF" >> "$GITHUB_OUTPUT" cp dist-manifest.json "$BUILD_MANIFEST_NAME" + # The generated installer downloads the archive with a single `curl` + # (https://github.com/axodotdev/cargo-dist/pull/2311). GitHub Releases can answer HTTP 618 + # for an expired signed link, which only --retry-all-errors retries. The grep fails the + # release if the template's download line changes. + # TODO(COD-4523): drop this step and `allow-dirty` once installers download from our CDN. + - name: Retry the installer's archive download + run: | + installer=target/distrib/codspeed-runner-installer.sh + sed -i 's/curl -sSfL /curl -sSfL --retry 5 --retry-all-errors /' "$installer" + grep -qF 'curl -sSfL --retry 5 --retry-all-errors "$1" -o "$2"' "$installer" - name: "Upload artifacts" uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a with: diff --git a/dist-workspace.toml b/dist-workspace.toml index 3d544b63d..b397c11d2 100644 --- a/dist-workspace.toml +++ b/dist-workspace.toml @@ -25,8 +25,12 @@ install-updater = false precise-builds = true # `pr-run-mode = "upload"` would otherwise turn on a rust-cache step in every build job cache-builds = false +# release.yml patches the generated installer (step "Retry the installer's archive download"), +# so `dist generate` must not overwrite it. To regenerate release.yml, drop this line, run +# `dist generate`, then restore that step and this line. +allow-dirty = ["ci"] -# Pins for the generated release.yml, which dependabot skips: bump them here, then run `dist generate` +# Pins for release.yml, which dependabot skips: bump them here, then regenerate it (see `allow-dirty`) [dist.github-action-commits] "actions/checkout" = "3d3c42e5aac5ba805825da76410c181273ba90b1" # v7.0.1 "actions/upload-artifact" = "043fb46d1a93c77aae656e7c1c64a875d1fc6a0a" # v7.0.1 diff --git a/src/cli/run/helpers/download_file.rs b/src/cli/run/helpers/download_file.rs index 11a1fa65c..cedcb55fe 100644 --- a/src/cli/run/helpers/download_file.rs +++ b/src/cli/run/helpers/download_file.rs @@ -1,8 +1,6 @@ use crate::binary_pins::PinnedBinary; use crate::{prelude::*, request_client::DOWNLOAD_CLIENT}; -use reqwest_retry::{ - RetryDecision, RetryPolicy, Retryable, default_on_request_success, policies::ExponentialBackoff, -}; +use reqwest_retry::{RetryDecision, RetryPolicy, policies::ExponentialBackoff}; use std::path::Path; use std::time::SystemTime; @@ -36,14 +34,13 @@ async fn download_file(url: &Url, path: &Path) -> Result<(), AttemptError> { .get(url.clone()) .send() .await - .map_err(|e| AttemptError::Transient(anyhow!("Failed to download file: {e}")))?; + .map_err(|e| AttemptError::Transient(anyhow!(e)))?; - if !response.status().is_success() { - let error = anyhow!("Failed to download file: {}", response.status()); - return Err(match default_on_request_success(&response) { - Some(Retryable::Transient) => AttemptError::Transient(error), - _ => AttemptError::Fatal(error), - }); + // Any error status is retried: GitHub Releases answers HTTP 618 for an expired signed link, + // and a new request to the release URL gets a fresh one. + let status = response.status(); + if !status.is_success() { + return Err(AttemptError::Transient(anyhow!("HTTP {status}"))); } let content = response @@ -108,7 +105,17 @@ async fn download_and_verify(url: &Url, expected_sha256: &str, path: &Path) -> R tokio::time::sleep(wait).await; n_past_retries += 1; } - RetryDecision::DoNotRetry => return Err(error), + RetryDecision::DoNotRetry => { + let hint = if url.host_str() == Some("github.com") { + "\nThis file is hosted on GitHub Releases. Re-run the job, and check https://www.githubstatus.com if it keeps failing." + } else { + "" + }; + bail!( + "Failed to download {url} after {} attempts: {error:#}.{hint}", + n_past_retries + 1 + ); + } } } } @@ -263,8 +270,11 @@ mod tests { .await .expect_err("persistent truncated bodies should fail the download"); + let prefix = format!("Failed to download {url} after {attempts} attempts: "); assert!( - error.to_string().contains("Failed to read response"), + error + .to_string() + .starts_with(&(prefix + "Failed to read response")), "unexpected error: {error}" ); assert_eq!(request_count.load(Ordering::SeqCst), attempts); @@ -287,19 +297,20 @@ mod tests { } #[tokio::test] - async fn does_not_retry_client_errors() { - let (url, request_count) = spawn_scripted_server(vec![ScriptedResponse::Status(404)]); + async fn retries_any_error_status_and_recovers() { + let (url, request_count) = spawn_scripted_server(vec![ + ScriptedResponse::Status(618), + ScriptedResponse::Status(404), + ScriptedResponse::Body(GOOD_BODY), + ]); let file = NamedTempFile::new().unwrap(); - let error = download_and_verify(&url, &sha256::digest(GOOD_BODY), file.path()) + download_and_verify(&url, &sha256::digest(GOOD_BODY), file.path()) .await - .expect_err("a 404 should fail the download"); + .expect("download should recover from error statuses"); - assert!( - error.to_string().contains("404"), - "unexpected error: {error}" - ); - assert_eq!(request_count.load(Ordering::SeqCst), 1); + assert_eq!(std::fs::read(file.path()).unwrap(), GOOD_BODY); + assert_eq!(request_count.load(Ordering::SeqCst), 3); } #[tokio::test] diff --git a/src/executor/helpers/introspected_golang/go.sh b/src/executor/helpers/introspected_golang/go.sh index 09dad5093..a4f7a58d8 100755 --- a/src/executor/helpers/introspected_golang/go.sh +++ b/src/executor/helpers/introspected_golang/go.sh @@ -69,16 +69,26 @@ install_go_runner() { } trap cleanup_go_runner_installer RETURN - curl -fsSL "$download_url" -o "$installer_path" - - local actual_sha256 - actual_sha256=$(sha256_file "$installer_path") - if [ "$actual_sha256" != "$expected_sha256" ]; then - echo "ERROR: Hash mismatch for $download_url: expected $expected_sha256, got $actual_sha256" >&2 - exit 1 - fi - - bash "$installer_path" --quiet + # GitHub Releases fails transiently (5xx, HTTP 618 for an expired signed link). The installer + # downloads the archive with a single curl, so the whole install is retried. + local attempt actual_sha256 + for attempt in 1 2 3 4 5 6; do + if curl -fsSL "$download_url" -o "$installer_path"; then + actual_sha256=$(sha256_file "$installer_path") + if [ "$actual_sha256" != "$expected_sha256" ]; then + echo "ERROR: Hash mismatch for $download_url: expected $expected_sha256, got $actual_sha256" >&2 + exit 1 + fi + bash "$installer_path" --quiet && return + fi + if [ "$attempt" -lt 6 ]; then + echo "WARNING: Attempt $attempt/6 to install codspeed-go-runner v$version failed. Retrying in $((2 ** (attempt - 1)))s." >&2 + sleep $((2 ** (attempt - 1))) + fi + done + echo "ERROR: Failed to install codspeed-go-runner v$version after 6 attempts." >&2 + echo "It is hosted on GitHub Releases. Re-run the job, and check https://www.githubstatus.com if it keeps failing." >&2 + exit 1 }