summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/pipeline/publish.rs54
-rw-r--r--tests/production_surface.rs25
2 files changed, 58 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))?;
diff --git a/tests/production_surface.rs b/tests/production_surface.rs
index 1fa855f..5ade95b 100644
--- a/tests/production_surface.rs
+++ b/tests/production_surface.rs
@@ -58,6 +58,31 @@ fn no_production_module_references_the_shim_harness() {
}
#[test]
+fn the_served_index_has_exactly_one_serialization_lock() {
+ // §6 M-5: two mutexes in `publish.rs`, each documented as *the* "one publish
+ // per repo" guarantee, with `withdraw_package` taking only one of them, is
+ // worse than either answer — a reader cannot tell whether the guarantee is
+ // redundant or incomplete. There is one lock now, and this keeps it that way.
+ let src = std::fs::read_to_string(Path::new(env!("CARGO_MANIFEST_DIR")).join("src/pipeline/publish.rs"))
+ .expect("read publish.rs");
+ let mutexes = src
+ .lines()
+ .filter(|l| l.contains("static") && l.contains("Mutex<"))
+ .collect::<Vec<_>>();
+ assert_eq!(mutexes.len(), 1, "publish.rs must declare exactly one serialization mutex, found {mutexes:?}");
+ assert!(!src.contains("PUBLISH_LOCK"), "the second lock must stay deleted");
+ // And both writers of the served index take it, not just one of them.
+ let acquisitions = src
+ .lines()
+ .filter(|l| l.contains("lock_repo()") && !l.trim_start().starts_with("fn "))
+ .count();
+ assert!(
+ acquisitions >= 2,
+ "publish and withdraw_package must both take the repo lock; found {acquisitions} acquisitions"
+ );
+}
+
+#[test]
fn the_shim_binaries_are_test_only() {
let manifest = std::fs::read_to_string(Path::new(env!("CARGO_MANIFEST_DIR")).join("Cargo.toml"))
.expect("read Cargo.toml");