Handle transient observer lock contention and renew diagnostic sudo

This commit is contained in:
Pavel Isaev 2026-11-08 23:25:04 +03:00
parent db1c54871c
commit 6b85be9c99
2 changed files with 57 additions and 1 deletions

View file

@ -124,6 +124,15 @@ Required before claiming hardware readiness:
## Execution order after this audit
Run `20261108T173013Z` passed catalog signature checks, extension enable and nano
resolution. Stage then failed acquiring the compose lock. The read-only observer
also owns that lock for up to 10s plus 2s termination; mutation previously tried
only once. Mutation lock acquisition now waits at most 15s on contention; other
lock errors fail immediately, and external writers still exclude mutation.
Added contention/release and bounded-failure tests to the normal Rust test stage.
Runtime confirmation remains pending. Core failure diagnostics also renew sudo
credentials before fetching logs, since a long operation can expire the timestamp.
Retry `20261108T050444Z` reused the completed images in the normal pipeline and
captured authorized transaction logs. Catalog failure is in systemd-nspawn
(exit 1), but the worker retained only stderr; container console output on stdout

View file

@ -48,16 +48,63 @@ fn repo(args: &[&str]) -> Result<String, String> {
let r = format!("--repo={STATE}/repo");
output("ostree", &[&[r.as_str()], args].concat())
}
fn wait_for_lock(file: &File, timeout: std::time::Duration) -> Result<(), String> {
let started = std::time::Instant::now();
loop {
match file.try_lock() {
Ok(()) => return Ok(()),
Err(std::fs::TryLockError::WouldBlock) if started.elapsed() < timeout => {
std::thread::sleep(std::time::Duration::from_millis(20));
}
Err(error) => return Err(format!("another host operation is running or lock unavailable: {error}")),
}
}
}
pub(crate) fn lock() -> Result<File, String> {
if output("id", &["-u"])?.trim() != "0" { return Err("Run as root".into()); }
if !Path::new("/run/ostree-booted").exists() || !Path::new("/etc/archipelago/provisioned").exists() {
return Err("complete firstboot in an installed deployment first".into());
}
let f = File::options().create(true).truncate(false).write(true).open("/run/archipelago-compose.lock").map_err(|e| e.to_string())?;
f.try_lock().map_err(|e| format!("another host operation is running: {e}"))?;
// The local observer briefly holds this same lock (bounded to 10s + 2s
// termination). Do not fail an admitted mutation on that transient race.
wait_for_lock(&f, std::time::Duration::from_secs(15))?;
fs::DirBuilder::new().recursive(true).mode(0o700).create(STATE).map_err(|e| e.to_string())?;
Ok(f)
}
#[cfg(test)]
mod lock_tests {
use super::*;
fn files() -> (File, File) {
let path = std::env::temp_dir().join(format!("archipelago-lock-{}-{}", std::process::id(),
SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_nanos()));
let first = File::options().create_new(true).write(true).open(&path).unwrap();
let second = File::options().write(true).open(&path).unwrap();
fs::remove_file(path).unwrap();
(first, second)
}
#[test]
fn mutation_waits_for_short_observation() {
let (observer, mutation) = files();
observer.try_lock().unwrap();
let release = std::thread::spawn(move || {
std::thread::sleep(std::time::Duration::from_millis(80));
drop(observer);
});
assert!(wait_for_lock(&mutation, std::time::Duration::from_secs(2)).is_ok());
release.join().unwrap();
}
#[test]
fn competing_writer_still_blocks_mutation() {
let (writer, mutation) = files();
writer.try_lock().unwrap();
assert!(wait_for_lock(&mutation, std::time::Duration::from_millis(40)).is_err());
drop(writer);
assert!(wait_for_lock(&mutation, std::time::Duration::ZERO).is_ok());
}
}
pub(crate) fn binding() -> Result<Value, String> {
let local = Path::new("/etc/archipelago/update.json");
let path = if local.exists() { local } else { Path::new("/usr/lib/archipelago/update.json") };