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

buildsys: retry builds on "unexpected EOF" error #1557

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions tools/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions tools/buildsys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ exclude = ["README.md"]
[dependencies]
duct = "0.13.0"
hex = "0.4.0"
lazy_static = "1.4"
rand = { version = "0.8", default-features = false, features = ["std", "std_rng"] }
regex = "1"
reqwest = { version = "0.11.1", default-features = false, features = ["rustls-tls", "blocking"] }
serde = { version = "1.0", features = ["derive"] }
serde_plain = "0.3.0"
Expand Down
34 changes: 24 additions & 10 deletions tools/buildsys/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ pub(crate) mod error;
use error::Result;

use duct::cmd;
use lazy_static::lazy_static;
use nonzero_ext::nonzero;
use rand::Rng;
use regex::Regex;
use sha2::{Digest, Sha512};
use snafu::{ensure, OptionExt, ResultExt};
use std::env;
Expand All @@ -32,11 +34,23 @@ the bug is fixed there will be many older versions of Docker in the wild.
The failure has an exit code of 1, which is too generic to be helpful. All we
can do is check the output for the error's signature, and retry if we find it.
*/
static DOCKER_BUILD_FRONTEND_ERROR: &str = concat!(
r#"failed to solve with frontend dockerfile.v0: "#,
r#"failed to solve with frontend gateway.v0: "#,
r#"frontend grpc server closed unexpectedly"#
);
lazy_static! {
static ref DOCKER_BUILD_FRONTEND_ERROR: Regex = Regex::new(concat!(
r#"failed to solve with frontend dockerfile.v0: "#,
r#"failed to solve with frontend gateway.v0: "#,
r#"frontend grpc server closed unexpectedly"#
))
.unwrap();
}

/*
We also see sporadic CI failures with only this error message.
We use (?m) for multi-line mode so we can match the message on a line of its own without splitting
the output ourselves; we match the regexes against the whole of stdout.
*/
lazy_static! {
static ref UNEXPECTED_EOF_ERROR: Regex = Regex::new("(?m)^unexpected EOF$").unwrap();
}

static DOCKER_BUILD_MAX_ATTEMPTS: NonZeroU16 = nonzero!(10u16);

Expand Down Expand Up @@ -210,12 +224,12 @@ fn build(
let _ = docker(&rmi, Retry::No);

// Build the image, which builds the artifacts we want.
// Work around a transient, known failure case with Docker.
// Work around transient, known failure cases with Docker.
docker(
&build,
Retry::Yes {
attempts: DOCKER_BUILD_MAX_ATTEMPTS,
messages: &[DOCKER_BUILD_FRONTEND_ERROR],
messages: &[&*DOCKER_BUILD_FRONTEND_ERROR, &*UNEXPECTED_EOF_ERROR],
},
)?;

Expand All @@ -240,7 +254,7 @@ fn build(
/// Run `docker` with the specified arguments.
fn docker(args: &[String], retry: Retry) -> Result<Output> {
let mut max_attempts: u16 = 1;
let mut retry_messages: &[&str] = &[];
let mut retry_messages: &[&Regex] = &[];
if let Retry::Yes { attempts, messages } = retry {
max_attempts = attempts.into();
retry_messages = messages;
Expand All @@ -262,7 +276,7 @@ fn docker(args: &[String], retry: Retry) -> Result<Output> {
}

ensure!(
retry_messages.iter().any(|&m| stdout.contains(m)) && attempt < max_attempts,
retry_messages.iter().any(|m| m.is_match(&stdout)) && attempt < max_attempts,
error::DockerExecution {
args: &args.join(" ")
}
Expand All @@ -278,7 +292,7 @@ enum Retry<'a> {
No,
Yes {
attempts: NonZeroU16,
messages: &'a [&'a str],
messages: &'a [&'static Regex],
},
}

Expand Down