From e04152da00f97c5ecc9a3f6dc5217d61274c7907 Mon Sep 17 00:00:00 2001 From: aur-build Date: Wed, 18 Nov 2026 15:59:10 +0000 Subject: pipeline: one lock guards the served index, and it says so once MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `publish.rs` had two static mutexes, each documented as *the* "one publish per repo" guarantee — `PUBLISH_LOCK` taken at the top of `publish()`, `REPO_LOCK` taken partway through and by `withdraw_package`. Either the second was redundant or the guarantee was incomplete, and a reader could not tell which, which is worse than either answer: the comment on `REPO_LOCK` claimed a single-writer property that its own position in the function did not provide (artifacts were already staged by then), and the comment on `PUBLISH_LOCK` claimed it for a section that did not include the rename. There is one lock now. `REPO_LOCK` is taken at the top of `publish()` and held for the whole of it — staging, render, DB commit, rename, sign — which is the sequence that must not interleave, and `withdraw_package` takes the same lock for the same reason. The poison-recovery rule that belonged to both is stated once, on the lock. `reconcile` is documented as the one rewriter that does not take it, because it runs at startup before the dispatcher exists. M-5 is a clarity defect rather than a live misbehaviour, so the guard is structural: `tests/production_surface.rs` fails if `publish.rs` declares a second serialization mutex, if `PUBLISH_LOCK` comes back, or if either index writer stops taking the one that remains. A timing-based test would have been the flaky kind and would not have proven anything the I4 and I8 crash tests do not already prove. --- src/pipeline/publish.rs | 54 ++++++++++++++++++++++++++++++------------------- 1 file changed, 33 insertions(+), 21 deletions(-) (limited to 'src/pipeline/publish.rs') diff --git a/src/pipeline/publish.rs b/src/pipeline/publish.rs index 40048c3..877d8e1 100644 --- a/src/pipeline/publish.rs +++ b/src/pipeline/publish.rs @@ -17,11 +17,36 @@ use crate::error::{Error, Result}; use crate::pipeline::{Collect, Ctx}; use crate::time; -/// One publish per repo at a time. In one process a mutex is exactly the -/// single-writer guarantee the design asks for; the publisher thread in P3 keeps -/// the critical section tiny (db file rewrite only), never the build. +/// One writer to the served repo at a time. +/// +/// This is *the* serialization guarantee for the index (§8.2, §9, I4): a publish +/// renders a new pacman database to `.tmp`, commits the DB, then renames over +/// the live one, and a second writer interleaving anywhere in that sequence +/// would produce an index that matches neither DB state. It is held by +/// [`publish`] for the whole of it — staging, render, DB commit, rename, sign — +/// and by [`withdraw_package`] for the same reason. With +/// `max_concurrent_builds > 1` it is what stops two workers racing on one file. +/// +/// It is a process-local mutex, which is exactly the guarantee the design asks +/// for: one `aur-build` process owns one repo directory. Two processes sharing +/// a directory is not a configuration this system supports. +/// +/// [`reconcile`] rewrites the index without it because it runs at startup, +/// before the dispatcher exists, when there is nothing to race with. +/// +/// A panic while holding it is recovered from rather than propagated: atomic +/// publish means a poisoned lock cannot have left a half-written database — the +/// live file is whatever was last renamed into place — so clearing the poison +/// continues instead of wedging the queue forever. static REPO_LOCK: Mutex<()> = Mutex::new(()); +fn lock_repo() -> std::sync::MutexGuard<'static, ()> { + match REPO_LOCK.lock() { + Ok(g) => g, + Err(p) => p.into_inner(), + } +} + /// The world a build is admitted against: every pkgbase's accepted version. pub fn index_world(db: &Db) -> Result> { let rels = db::all_published_releases(db)?; @@ -51,20 +76,11 @@ fn index_artifacts(db: &Db, extra: &[PathBuf]) -> Result> { } /// Publish the collected artifacts of a build. Returns the new release id. -/// One publish at a time per repo (§8.2, §9). With `max_concurrent_builds > 1` -/// two workers could otherwise interleave the index rewrite; the critical -/// section is the db file rewrite only, never a build. -static PUBLISH_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(()); - +/// +/// Serialized against every other writer of the index by [`REPO_LOCK`], which +/// is held for the whole function. pub fn publish(ctx: &Ctx) -> Result { - let _guard = match PUBLISH_LOCK.lock() { - Ok(g) => g, - // A previous publish panicked while holding it. The index on disk is - // still whatever was last renamed into place — atomic publish means a - // poisoned lock cannot have left a half-written db — so we clear the - // poison and continue rather than wedge the queue forever. - Err(p) => p.into_inner(), - }; + let _guard = lock_repo(); let collect = Collect::load(&ctx.collect_path)?; if collect.packages.is_empty() { return Err(Error::invalid("nothing to publish")); @@ -89,10 +105,6 @@ pub fn publish(ctx: &Ctx) -> Result { staged.push((dest, from)); } - let _guard = REPO_LOCK - .lock() - .unwrap_or_else(|e| e.into_inner()); - // 1. render the new database to .tmp and fsync it. The old one is untouched. let db_name = ctx.cfg.repo_db_name(); let final_db = arch_dir.join(format!("{db_name}.tar.gz")); @@ -368,7 +380,7 @@ pub fn reconcile(cfg: &Config, db: &Db) -> Result { /// Remove a package from the served index (§9 withdrawal path). pub fn withdraw_package(cfg: &Config, db: &Db, name: &str) -> Result<()> { - let _guard = REPO_LOCK.lock().unwrap_or_else(|e| e.into_inner()); + let _guard = lock_repo(); let rid = db::repo_id(db)?; let n = name.to_string(); db.writer.tx(move |tx| db::remove_package_by_name_in(tx, rid, &n))?; -- cgit v1.2.3