From a3c5b6f06ba5c4981cf0cc7ca4afcd9b39562b07 Mon Sep 17 00:00:00 2001 From: Gerald Pinder Date: Tue, 14 May 2024 10:14:04 -0400 Subject: [PATCH] fix: Create lock on docker setup to prevent race conditions --- src/drivers/docker_driver.rs | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/drivers/docker_driver.rs b/src/drivers/docker_driver.rs index d561850e..395f343d 100644 --- a/src/drivers/docker_driver.rs +++ b/src/drivers/docker_driver.rs @@ -1,11 +1,13 @@ use std::{ env, process::{Command, Stdio}, + sync::Mutex, }; -use anyhow::{bail, Result}; +use anyhow::{anyhow, bail, Result}; use blue_build_utils::constants::{BB_BUILDKIT_CACHE_GHA, CONTAINER_FILE, SKOPEO_IMAGE}; use log::{info, trace, warn}; +use once_cell::sync::Lazy; use semver::Version; use serde::Deserialize; @@ -29,6 +31,8 @@ struct DockerVersionJson { pub client: DockerVerisonJsonClient, } +static DOCKER_SETUP: Lazy> = Lazy::new(|| Mutex::new(false)); + #[derive(Debug)] pub struct DockerDriver; @@ -36,6 +40,15 @@ impl DockerDriver { fn setup() -> Result<()> { trace!("DockerDriver::setup()"); + let mut lock = DOCKER_SETUP + .lock() + .map_err(|e| anyhow!("Failed to lock DOCKER_SETUP: {e}"))?; + + if *lock { + drop(lock); + return Ok(()); + } + trace!("docker buildx ls --format={}", "{{.Name}}"); let ls_out = Command::new("docker") .arg("buildx") @@ -61,10 +74,13 @@ impl DockerDriver { .arg("--name=bluebuild") .output()?; - if !create_out.status.success() { + if create_out.status.success() { + *lock = true; + } else { bail!("{}", String::from_utf8_lossy(&create_out.stderr)); } } + drop(lock); Ok(()) } }