From e910f7ef7eff7fd34da03beb69028dcce5ab820f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 2 Dec 2026 06:51:44 +0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix:=20the=20runner=20waits=20ou?= =?UTF-8?q?t=20a=20forge=20restart=20while=20uploading=20artifacts,=20as?= =?UTF-8?q?=20it=20already=20did=20for=20the=20job=20result;=20a=20503=20a?= =?UTF-8?q?t=20the=20end=20no=20longer=20fails=20a=20finished=20run?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/forge-runner/src/runner.rs | 63 +++++++++++++++++++------------ 1 file changed, 38 insertions(+), 25 deletions(-) diff --git a/crates/forge-runner/src/runner.rs b/crates/forge-runner/src/runner.rs index fe172cf..4f9e1ca 100644 --- a/crates/forge-runner/src/runner.rs +++ b/crates/forge-runner/src/runner.rs @@ -24,7 +24,7 @@ use crate::update::{self, Installed, UpdateState}; use crate::workspace; const LOG_CHANNEL: usize = 64; -const FINISH_PATIENCE: Duration = Duration::from_secs(10 * 60); +const FORGE_PATIENCE: Duration = Duration::from_secs(10 * 60); const MAX_ARTIFACT_BYTES: u64 = 512 * 1024 * 1024; const LOG_FLUSH: Duration = Duration::from_secs(5); @@ -457,10 +457,12 @@ impl Runner { let found = artifacts::collect(dir, &job.artifacts, MAX_ARTIFACT_BYTES) .map_err(|e| format!("artifacts: {e}"))?; for artifact in &found { - self.client - .upload_artifact(&job.job_id, &job.job_token, &artifact.name, &artifact.path) - .await - .map_err(|e| format!("artifact {}: {e}", artifact.name))?; + patiently(&job.job_id, "artifact upload", || { + self.client + .upload_artifact(&job.job_id, &job.job_token, &artifact.name, &artifact.path) + }) + .await + .map_err(|e| format!("artifact {}: {e}", artifact.name))?; } tracing::info!(job_id = %job.job_id, count = found.len(), "artifacts uploaded"); Ok(()) @@ -474,26 +476,12 @@ impl Runner { oom_killed: oom_kills.map(|_| true), oom_kills, }; - let started = Instant::now(); - let mut attempt = 0u32; - loop { - attempt += 1; - match self.client.finish(&job.job_id, &job.job_token, &req).await { - Ok(()) => return, - Err(e) => { - tracing::warn!(job_id = %job.job_id, attempt, error = %e, "finish failed"); - let away = match &e { - ClientError::Http(_) => true, - ClientError::Status { status, .. } => status.is_server_error(), - _ => false, - }; - if !away || started.elapsed() >= FINISH_PATIENCE { - break; - } - let wait = Duration::from_secs((2 * u64::from(attempt)).min(15)); - tokio::time::sleep(wait).await; - } - } + let reported = patiently(&job.job_id, "finish", || { + self.client.finish(&job.job_id, &job.job_token, &req) + }) + .await; + if reported.is_ok() { + return; } tracing::error!( job_id = %job.job_id, @@ -502,6 +490,31 @@ impl Runner { } } +async fn patiently(job_id: &str, what: &str, mut op: F) -> Result<(), ClientError> +where + F: FnMut() -> Fut, + Fut: std::future::Future>, +{ + let started = Instant::now(); + let mut attempt = 0u32; + loop { + attempt += 1; + let Err(e) = op().await else { + return Ok(()); + }; + tracing::warn!(job_id, attempt, error = %e, "{what} failed"); + let away = match &e { + ClientError::Http(_) => true, + ClientError::Status { status, .. } => status.is_server_error(), + _ => false, + }; + if !away || started.elapsed() >= FORGE_PATIENCE { + return Err(e); + } + tokio::time::sleep(Duration::from_secs((2 * u64::from(attempt)).min(15))).await; + } +} + fn not_claiming(last: &mut Option, reason: String) { if last.as_deref() != Some(reason.as_str()) { tracing::info!("not claiming: {reason}");