From 246c9c77cd7ceea4e836a35b207db9e35547ed74 Mon Sep 17 00:00:00 2001 From: Derrors <33656531+Derrors@users.noreply.github.com> Date: Wed, 25 Nov 2026 15:57:11 +0800 Subject: [PATCH 1/2] test(maintenance): wait for the lock in the pending-journal test pending_journal_blocks_other_installs_after_lock_owner_dies took the shared lease right after dropping the exclusive one. Another test that spawns a process meanwhile holds a copy of the dropped descriptor in the child until it execs, and the lock with it, so the last shared_in failed now and then (CI on #188). With a thread spawning `true` in a loop it failed 37 times in 300. The test now retries while the lock is held (as once_free does for the other test in the file), and checks that the first refusal is for the pending journal, not for the lock: before, a held lock passed that assertion too. 0 failures in 300 under the same load. Co-authored-by: Claude Opus 5.5 --- crates/ictl-daemon/src/maintenance.rs | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/crates/ictl-daemon/src/maintenance.rs b/crates/ictl-daemon/src/maintenance.rs index 473e14d3..bde41270 100644 --- a/crates/ictl-daemon/src/maintenance.rs +++ b/crates/ictl-daemon/src/maintenance.rs @@ -141,12 +141,32 @@ mod tests { std::fs::create_dir_all(install.parent().unwrap()).unwrap(); std::fs::write(&install, "prepared").unwrap(); drop(lease); - assert_eq!(shared_in(&dir).unwrap_err().kind(), io::ErrorKind::WouldBlock); + // refused for the journal left behind, not because the lock is still held + assert!(is_pending(&shared_once_free(&dir).unwrap_err())); assert_eq!(remember(&dir, &other).unwrap_err().kind(), io::ErrorKind::WouldBlock); std::fs::remove_file(install).unwrap(); - shared_in(&dir).unwrap(); + shared_once_free(&dir).unwrap(); std::fs::remove_dir_all(dir).unwrap(); } + + /// The refusal for a pending transaction ([`shared_in`]), not for a lock held by someone + fn is_pending(e: &io::Error) -> bool { + e.kind() == io::ErrorKind::WouldBlock && e.to_string().contains("recovery pending") + } + + /// [`shared_in`] once the lock is free again (see [`once_free`]): a refusal for a pending transaction is returned + /// at once + fn shared_once_free(dir: &Path) -> io::Result { + for _ in 0..100 { + match shared_in(dir) { + Err(e) if e.kind() == io::ErrorKind::WouldBlock && !is_pending(&e) => { + std::thread::sleep(std::time::Duration::from_millis(20)) + } + r => return r, + } + } + shared_in(dir) + } /// A lock that is free again: another test spawning a process meanwhile (procs.rs) briefly holds a copy of the /// dropped descriptor in the child until it execs, and the lock with it fn once_free(dir: &Path, operation: libc::c_int) -> File { From d0dd22690bb0f5a19ff4655a3b64ebb5ce5a5c7e Mon Sep 17 00:00:00 2001 From: Derrors <33656531+Derrors@users.noreply.github.com> Date: Wed, 25 Nov 2026 22:01:11 +0800 Subject: [PATCH 2/2] test(update): make the interrupted-worker test robust and say why it failed recovers_after_process_death_using_only_durable_records failed now and then (macOS CI on #198, Linux locally on 2026-11-24): the worker it starts ended without being killed, and its output went to /dev/null, so why was lost. - the worker's output goes to a file in the fixture directory and is shown when it isn't killed (a file, not a pipe: the fixture daemon it starts keeps the descriptor, and reading a pipe to its end would wait for that daemon) - Store::open is retried while the updater lock is held: another test that spawns a process meanwhile keeps a copy of a dropped lock's descriptor in the child until it execs, the race fixed for maintenance in the previous commit - the fixture daemon's spawn is retried on ETXTBSY (text file busy), the same race for the script just written, as archive.rs does for the real candidate Neither race reproduced on Linux (60 runs under a process-spawning thread, 40 full test binaries at 32 threads); the output will tell if it is another cause. Co-Authored-By: Claude Opus 5.5 --- .../src/update/transaction/tests.rs | 61 +++++++++++++++---- 1 file changed, 50 insertions(+), 11 deletions(-) diff --git a/apps/iphone-control/src/update/transaction/tests.rs b/apps/iphone-control/src/update/transaction/tests.rs index b70efc90..05d6f5ae 100644 --- a/apps/iphone-control/src/update/transaction/tests.rs +++ b/apps/iphone-control/src/update/transaction/tests.rs @@ -106,12 +106,14 @@ impl Services for TestService { Ok(()) } async fn start_healthy(&self, version: &str) -> Result<()> { - let child = Command::new(&self.binary) - .arg("daemon") - .env("ICTL_TEST_TX_HARNESS", std::env::current_exe()?) - .env("ICTL_TEST_TX_SOCKET", &self.socket) - .stdout(Stdio::null()) - .spawn()?; + let child = spawn_fixture(|| { + Command::new(&self.binary) + .arg("daemon") + .env("ICTL_TEST_TX_HARNESS", std::env::current_exe()?) + .env("ICTL_TEST_TX_SOCKET", &self.socket) + .stdout(Stdio::null()) + .spawn() + })?; let pid = child.id(); self.child.replace(Some(child)); for _ in 0..2 { @@ -130,6 +132,33 @@ impl Services for TestService { } } +/// Starts a fixture binary that was just written. Another test that spawns a process meanwhile can leave a copy of the +/// writing descriptor in its child until that execs, and executing the file then fails with "text file busy" +/// (ETXTBSY): tried again shortly, as `archive.rs` does for the real candidate +fn spawn_fixture(spawn: impl Fn() -> io::Result) -> io::Result { + for _ in 0..50 { + match spawn() { + Err(e) if e.raw_os_error() == Some(libc::ETXTBSY) => std::thread::sleep(Duration::from_millis(20)), + r => return r, + } + } + spawn() +} + +/// [`Store::open`] once the updater lock is free: a test that spawns a process meanwhile briefly holds a copy of a +/// dropped lock's descriptor in the child until it execs, and the lock with it (as in `maintenance::tests`) +fn open_once_free(binary: &Path, receipt: PathBuf) -> Store { + for _ in 0..100 { + match Store::open(binary, receipt.clone()) { + Err(e) if e.downcast_ref::().is_some_and(|e| e.kind() == io::ErrorKind::WouldBlock) => { + std::thread::sleep(Duration::from_millis(20)) + } + r => return r.unwrap(), + } + } + Store::open(binary, receipt).unwrap() +} + impl Fixture { fn new(reported: &str) -> Self { let dir = tempfile::tempdir_in("/tmp").unwrap(); @@ -249,7 +278,7 @@ async fn interrupted_worker() { let dir = PathBuf::from(dir); let b = dir.join("iphone-control"); let c = dir.join("patch"); - let store = Store::open(&b, dir.join("receipt.json")).unwrap(); + let store = open_once_free(&b, dir.join("receipt.json")); let old = receipt(&b, "0.2.0", digest(&b).unwrap()); let new = receipt(&b, "0.2.1", digest(&c).unwrap()); let host = Interrupted { @@ -275,15 +304,25 @@ async fn recovers_after_process_death_using_only_durable_records() { let binary = f.store.binary.clone(); let receipt_file = f.store.receipt.clone(); drop(f.store); + // what the worker printed, shown if it wasn't killed. A file, not a pipe: the fixture daemon it may have + // started holds the descriptor on, and reading a pipe to its end would wait for that daemon + let log = binary.parent().unwrap().join("worker.log"); + let out = File::create(&log).unwrap(); let status = Command::new(std::env::current_exe().unwrap()) - .args(["--exact", "update::transaction::tests::interrupted_worker"]) + .args(["--exact", "update::transaction::tests::interrupted_worker", "--nocapture"]) .env("ICTL_TEST_INTERRUPTED", binary.parent().unwrap()) .env("ICTL_TEST_PHASE", phase) - .stdout(Stdio::null()) + .stdout(out.try_clone().unwrap()) + .stderr(out) .status() .unwrap(); - assert_eq!(status.signal(), Some(libc::SIGKILL)); - let store = Store::open(&binary, receipt_file).unwrap(); + assert_eq!( + status.signal(), + Some(libc::SIGKILL), + "{phase}: the worker ended otherwise ({status}):\n{}", + fs::read_to_string(&log).unwrap_or_default() + ); + let store = open_once_free(&binary, receipt_file); let mut tx: Transaction = read_json(&store.journal()).unwrap().unwrap(); assert_eq!( digest(&store.binary).unwrap(),