summaryrefslogtreecommitdiffstats
path: root/src/pipeline/publish.rs
diff options
context:
space:
mode:
authoraur-build <aur-build@localhost>2026-11-18 15:59:10 +0000
committeraur-build <aur-build@localhost>2026-11-18 15:59:10 +0000
commite04152da00f97c5ecc9a3f6dc5217d61274c7907 (patch)
tree7d70011f688b2dddaa98804575fc885f61876cf6 /src/pipeline/publish.rs
parent062efa6583b7fea292a4f485659661d266461611 (diff)
downloadaur-build-e04152da00f97c5ecc9a3f6dc5217d61274c7907.tar.gz
aur-build-e04152da00f97c5ecc9a3f6dc5217d61274c7907.zip
pipeline: one lock guards the served index, and it says so once
`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.
Diffstat (limited to 'src/pipeline/publish.rs')
-rw-r--r--src/pipeline/publish.rs54
1 files changed, 33 insertions, 21 deletions
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<Vec<WorldMember>> {
let rels = db::all_published_releases(db)?;
@@ -51,20 +76,11 @@ fn index_artifacts(db: &Db, extra: &[PathBuf]) -> Result<Vec<PathBuf>> {
}
/// 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<i64> {
- 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<i64> {
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<usize> {
/// 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))?;