From 2c5a660213ae5f9f6f12d9787c5195e2c2127e6d Mon Sep 17 00:00:00 2001 From: aur-build Date: Wed, 18 Nov 2026 08:47:27 +0000 Subject: =?UTF-8?q?pipeline:=20a=20real=20process=20kill,=20and=20the=20fa?= =?UTF-8?q?ilure=20state=20the=20UI=20must=20show=20(=C2=A78.4,=20I5)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The crash-resume invariant was only ever simulated before: an in-process fault hook returns an error, and an error can be caught, unwound and cleaned up by accident. So the shims gained AUR_SHIM_KILL_PARENT — the named tool SIGKILLs the process running the pipeline, mid-step, with no Drop and no atexit. The test spawns the real aur-build binary, watches it die of SIGKILL inside the build step, and then asserts the things that matter: - the database is intact, because no transaction ever spans a build step - the completed steps are checkpointed succeeded and the running one is recorded running - after the lease expires the reaper requeues it and marks the step interrupted - resuming publishes exactly one release, and the shim invocation log proves mkarchroot ran once, makechrootpkg twice (crash + resume, not twice per attempt) and repo-add once **This found a real bug.** reap_expired only matched state='leased', but a worker sets state='running' the moment it starts a step, so a process that dies mid-step leaves a running row — and every real crash would have been stranded forever. The reaper now reaps leased and running. Also §8.4's other half, which was a comment rather than a feature: a failed build must not leave the old version looking healthy. db::approved_but_unbuilt finds approvals whose build failed and never published, and Attention and the package page now say 'v2 approved, build failed, index still serving v1' with a link to the log. Retrying is an explicit, audited POST, not something the system does to itself. Cancellation is asserted to be cooperative at a step boundary: a cancelled build runs no steps, publishes nothing, and is not counted as a failure. --- src/config.rs | 8 ++++++++ src/db/builds.rs | 29 +++++++++++++++++++++++++++-- src/db/queries.rs | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/http/render.rs | 15 +++++++++++++++ src/http/routes.rs | 51 +++++++++++++++++++++++++++++++++++++++++++++++++-- src/shims/mod.rs | 19 +++++++++++++++++++ 6 files changed, 170 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/config.rs b/src/config.rs index 270c81e..127c5a3 100644 --- a/src/config.rs +++ b/src/config.rs @@ -55,6 +55,10 @@ pub struct Config { pub publish_fault: Option, /// Test hook: the named shim must fail, e.g. `makechrootpkg`. pub shim_fail: Option, + /// Test hook: the named shim SIGKILLs its parent, i.e. the process running + /// the pipeline, mid-step. This is how a real crash is reproduced for I5 — + /// not a caught error, an uncatchable death. Never set in production. + pub shim_kill_parent: Option, /// How many builds may run at once (§8.2 backpressure). Default 1, which /// also means the chroot pool only needs one warm root. pub max_concurrent_builds: usize, @@ -93,6 +97,7 @@ impl Default for Config { shim_dir: None, publish_fault: None, shim_fail: None, + shim_kill_parent: None, max_concurrent_builds: 1, lease_secs: 90, min_free_disk_mb: 1024, @@ -143,6 +148,9 @@ impl Config { if let Some(f) = &self.shim_fail { cmd.env("AUR_SHIM_FAIL", f); } + if let Some(k) = &self.shim_kill_parent { + cmd.env("AUR_SHIM_KILL_PARENT", k); + } } /// Create the directory tree the server needs. Idempotent. diff --git a/src/db/builds.rs b/src/db/builds.rs index 0a7013c..27fa23b 100644 --- a/src/db/builds.rs +++ b/src/db/builds.rs @@ -161,13 +161,17 @@ pub fn extend_lease(db: &Db, id: &str, lease_secs: i64) -> Result<()> { /// Reaper pass (§8.2): expired leases go back to the queue and their in-flight /// step is marked interrupted, so the checkpoint decides where it resumes. +/// +/// Both `leased` and `running` are reaped: a worker sets `running` as soon as it +/// starts a step, so a process that dies mid-step leaves a `running` row, not a +/// `leased` one. Reaping only `leased` would strand every real crash. pub fn reap_expired(db: &Db) -> Result { let now = time::now(); db.writer.tx(move |tx| { let ids: Vec = { let mut stmt = tx.prepare( - "SELECT id FROM build WHERE state='leased' AND lease_expires_at IS NOT NULL - AND lease_expires_at < ?1", + "SELECT id FROM build WHERE state IN ('leased','running') + AND lease_expires_at IS NOT NULL AND lease_expires_at < ?1", )?; let rows = stmt.query_map(params![now], |r| r.get::<_, String>(0))?; rows.filter_map(|r| r.ok()).collect() @@ -484,3 +488,24 @@ pub fn requeue_transient(db: &Db, id: &str) -> Result<()> { Ok(()) }) } + +/// Explicitly retry a failed build (§8.4). This is a human decision, not a +/// policy: the failed step is put back to pending so it runs again, everything +/// that succeeded stays checkpointed, and the attempt counter keeps counting. +pub fn retry(db: &Db, id: &str) -> Result<()> { + let id = id.to_string(); + let at = time::now(); + db.writer.tx(move |tx| { + tx.execute( + "UPDATE build_step SET status='pending', error=NULL, started_at=NULL, finished_at=NULL + WHERE build_id=?1 AND status IN ('failed','interrupted')", + params![id], + )?; + tx.execute( + "UPDATE build SET state='queued', error=NULL, cancel_requested=0, + lease_owner=NULL, lease_expires_at=NULL, updated_at=?2 WHERE id=?1", + params![id, at], + )?; + Ok(()) + }) +} diff --git a/src/db/queries.rs b/src/db/queries.rs index 3adc92f..fa530b4 100644 --- a/src/db/queries.rs +++ b/src/db/queries.rs @@ -951,3 +951,55 @@ pub fn index_pkgbase_drift(db: &Db) -> Result<(Vec, Vec)> { Ok((orphans, missing)) }) } + +/// An approved change whose build failed and which therefore never reached the +/// index (§8.4). This is the state that must never look healthy: a human said +/// yes, the build died, and the index is quietly still serving the previous +/// version. Surfacing it is the whole point of `approved_but_unbuilt`. +#[derive(Debug, Clone)] +pub struct Unbuilt { + pub pkgbase: String, + /// The version that was approved but never published. + pub approved: String, + /// The version the index actually serves, empty if nothing is served. + pub serving: String, + pub error: String, + pub build_id: String, +} + +pub fn approved_but_unbuilt(db: &Db) -> Result> { + db.read(|c| { + let mut stmt = c.prepare( + "SELECT p.name, + (SELECT (CASE WHEN c.epoch='0' THEN '' ELSE c.epoch||':' END) || c.pkgver || '-' || c.pkgrel + FROM candidate c WHERE c.id = b.candidate_id), + COALESCE((SELECT (CASE WHEN r.epoch='0' THEN '' ELSE r.epoch||':' END) || r.pkgver || '-' || r.pkgrel + FROM release r JOIN pkgbase pb ON pb.current_accepted_release_id = r.id + WHERE pb.id = p.id AND r.state='published'), ''), + COALESCE(b.error, ''), b.id + FROM build b + JOIN pkgbase p ON p.id = b.pkgbase_id + WHERE b.state = 'failed' + AND b.candidate_id IS NOT NULL + AND NOT EXISTS (SELECT 1 FROM release r WHERE r.candidate_id = b.candidate_id) + AND b.id = (SELECT b2.id FROM build b2 + WHERE b2.candidate_id = b.candidate_id + ORDER BY b2.rowid DESC LIMIT 1) + ORDER BY p.name", + )?; + let rows = stmt.query_map([], |r| { + Ok(Unbuilt { + pkgbase: r.get(0)?, + approved: r.get(1)?, + serving: r.get(2)?, + error: r.get(3)?, + build_id: r.get(4)?, + }) + })?; + let mut out = Vec::new(); + for r in rows { + out.push(r?); + } + Ok(out) + }) +} diff --git a/src/http/render.rs b/src/http/render.rs index 9e3d801..d715a71 100644 --- a/src/http/render.rs +++ b/src/http/render.rs @@ -192,12 +192,25 @@ pub struct Attention { pub page: Page, pub empty: bool, pub notices: Vec, + pub unbuilt: Vec, pub outdated: Vec, pub missing: Vec, pub downgrades: Vec, pub blocked: Vec, } +/// §8.4: an approval whose build failed. The index is still serving the older +/// version and nothing about the package looks wrong from the outside, so this +/// row exists to be impossible to miss. +#[derive(Debug, Clone)] +pub struct UnbuiltRow { + pub pkgbase: String, + pub approved: String, + pub serving: String, + pub error: String, + pub build_id: String, +} + #[derive(Debug, Clone)] pub struct NoticeRow { pub id: i64, @@ -251,6 +264,8 @@ pub struct PackageDetail { pub summary: String, pub can_withdraw: bool, pub stale: Vec, + /// "v2 approved, build failed, index still serving v1" (§8.4). + pub unbuilt: Vec, pub deps_in: Vec, pub deps_out: Vec, pub timeline: Vec, diff --git a/src/http/routes.rs b/src/http/routes.rs index ab68069..6b01611 100644 --- a/src/http/routes.rs +++ b/src/http/routes.rs @@ -77,6 +77,7 @@ pub fn route(state: &State, req: &Req, seg: &[&str]) -> Res { ("GET", ["builds", id]) => build_detail(state, req, id), ("GET", ["builds", id, "log.txt"]) => build_log(state, id), ("POST", ["builds", id, "cancel"]) => build_cancel(state, req, id), + ("POST", ["builds", id, "requeue"]) => build_retry(state, req, id), ("GET", ["audit"]) => audit(state, req), ("GET", ["settings"]) => settings(state, req), _ => not_found(state, req), @@ -605,9 +606,24 @@ fn attention(state: &State, req: &Req) -> Res { .into_iter() .map(|s| StaleRow { pkgbase: s.pkgbase, dependency: s.dependency, built_with: s.built_with, now: s.now }) .collect(); - let empty = notices.is_empty() && outdated.is_empty() && missing.is_empty() && blocked.is_empty(); + let unbuilt: Vec = db::approved_but_unbuilt(db) + .unwrap_or_default() + .into_iter() + .map(|u| UnbuiltRow { + pkgbase: u.pkgbase, + approved: u.approved, + serving: u.serving, + error: u.error, + build_id: u.build_id, + }) + .collect(); + let empty = notices.is_empty() + && unbuilt.is_empty() + && outdated.is_empty() + && missing.is_empty() + && blocked.is_empty(); page_to_res( - Attention { page, empty, notices, outdated, missing, downgrades, blocked }, + Attention { page, empty, notices, unbuilt, outdated, missing, downgrades, blocked }, 200, ) } @@ -713,12 +729,25 @@ fn package_detail(state: &State, req: &Req, name: &str) -> Res { Some(r) => format!("serving {} · approved from aur_commit {}", r.version(), short(&r.aur_commit)), None => "nothing published yet".into(), }; + let unbuilt: Vec = db::approved_but_unbuilt(db) + .unwrap_or_default() + .into_iter() + .filter(|u| u.pkgbase == name) + .map(|u| UnbuiltRow { + pkgbase: u.pkgbase, + approved: u.approved, + serving: u.serving, + error: u.error, + build_id: u.build_id, + }) + .collect(); page_to_res( PackageDetail { page, name: name.to_string(), summary, can_withdraw: accepted.is_some(), + unbuilt, stale, deps_in, deps_out, @@ -899,6 +928,24 @@ fn build_cancel(state: &State, req: &Req, id: &str) -> Res { builds_with(state, req, page) } +/// Retrying a failed build is an explicit, audited decision (§8.4), not +/// something the system does to itself. +fn build_retry(state: &State, req: &Req, id: &str) -> Res { + match builds::retry(&state.db, id) { + Ok(()) => { + let _ = db::audit(&state.db.writer, "web", "web", "build.retry", "build", id, serde_json::json!({})); + Res::redirect(&format!("/builds/{id}")) + } + Err(e) => { + let mut page = chrome(state, req, "Builds", "nav_builds"); + page.flash = format!("could not retry: {e}"); + page.has_flash = true; + page.flash_err = true; + builds_with(state, req, page) + } + } +} + // ----------------------------------------------------------- ops, config -- fn audit(state: &State, req: &Req) -> Res { diff --git a/src/shims/mod.rs b/src/shims/mod.rs index 32ae7c9..5560aa3 100644 --- a/src/shims/mod.rs +++ b/src/shims/mod.rs @@ -147,6 +147,24 @@ pub fn record(tool: &str, args: &[String]) { } } +/// Test hook: kill the process that invoked us, mid-step, with a signal it +/// cannot catch. This is how the crash-resume invariant (I5) is tested for real: +/// the pipeline process dies while a step is running, with no chance to clean +/// up, and the next process must pick up from the checkpoints. +fn kill_parent_if_asked(tool: &str) { + let want = std::env::var("AUR_SHIM_KILL_PARENT").unwrap_or_default(); + if want == tool { + // Flush the invocation log first (record() already closed the file), + // then murder the parent. No cleanup, no unwinding, no atexit. + unsafe { + libc::kill(libc::getppid(), libc::SIGKILL); + } + // If the kill somehow did not land, do not let the step complete — that + // would make the test silently test nothing. + std::process::exit(137); + } +} + /// True when the harness asked this tool to fail. fn forced_fail(tool: &str) -> bool { std::env::var("AUR_SHIM_FAIL").map(|v| v == tool).unwrap_or(false) @@ -182,6 +200,7 @@ pub fn called_with(dir: &Path, tool: &str, needle: &str) -> usize { pub fn run(tool: &str) -> i32 { let args: Vec = std::env::args().skip(1).collect(); record(tool, &args); + kill_parent_if_asked(tool); let m = manifest(); let r = match tool { "mkarchroot" => mkarchroot(&args), -- cgit v1.2.3