diff --git a/crates/ledger-cli/tests/book_open_close.rs b/crates/ledger-cli/tests/book_open_close.rs index 9396f53d..a91c8aa5 100644 --- a/crates/ledger-cli/tests/book_open_close.rs +++ b/crates/ledger-cli/tests/book_open_close.rs @@ -487,7 +487,15 @@ fn closing_and_opening_a_book_over_and_over_loses_and_duplicates_nothing() { for cycle in 0..CYCLES { std::thread::sleep(Duration::from_millis(150 + (cycle as u64 * 37) % 100)); let beta_before = done.lock().unwrap().iter().filter(|d| d.book == B).count(); - assert_eq!(close_book(&home, A), CloseOutcome::Closed, "cycle {cycle}"); + let began = std::time::Instant::now(); + let closed = close_book(&home, A); + let took = began.elapsed(); + eprintln!("cycle {cycle}: the close took {took:?}"); + assert_eq!( + closed, + CloseOutcome::Closed, + "cycle {cycle}, after {took:?}" + ); // Answered closed: from here nothing runs on the book until it is // opened, so its files stand still (and, the writer lock being // released, another program could open it). @@ -823,9 +831,7 @@ mod raw { let mut extra = extra.to_vec(); extra.push(("expect", "100-continue")); self.start(method, path, &extra, body, 0); - self.stream - .set_read_timeout(Some(Duration::from_secs(120))) - .unwrap(); + self.bound_reads(Duration::from_secs(120)); let mut seen = Vec::new(); let mut byte = [0u8; 1]; while !seen.ends_with(b"\r\n\r\n") { @@ -871,10 +877,24 @@ mod raw { self.stream.write_all(piece).is_ok() } + /// Bound how long a read of this connection may wait. macOS refuses + /// (EINVAL) to set an option on a socket whose peer has already + /// closed it, which is what the server's cutting a connection is: + /// such a socket can't block a read (it gives what was sent, then + /// the end), so that refusal, and only that, is passed over. + fn bound_reads(&self, within: Duration) { + assert!(!within.is_zero(), "a read bound of no time"); + match self.stream.set_read_timeout(Some(within)) { + Ok(()) => {} + Err(e) if e.kind() == std::io::ErrorKind::InvalidInput => {} + Err(e) => panic!("no bound on a read of the connection: {e}"), + } + } + /// Everything the server sends until it closes the connection, /// which it must do within `within`. pub fn until_closed(&mut self, within: Duration) -> String { - self.stream.set_read_timeout(Some(within)).unwrap(); + self.bound_reads(within); let mut seen = std::mem::take(&mut self.taken); let mut buf = [0u8; 4096]; loop { @@ -884,7 +904,9 @@ mod raw { Err(e) if matches!( e.kind(), - std::io::ErrorKind::ConnectionReset | std::io::ErrorKind::BrokenPipe + std::io::ErrorKind::ConnectionReset + | std::io::ErrorKind::BrokenPipe + | std::io::ErrorKind::NotConnected ) => { break; @@ -934,9 +956,7 @@ mod raw { /// The response's status line and headers (the body is left). pub fn head(&mut self) -> (u16, String) { - self.stream - .set_read_timeout(Some(Duration::from_secs(60))) - .unwrap(); + self.bound_reads(Duration::from_secs(60)); let mut seen = std::mem::take(&mut self.taken); let mut byte = [0u8; 1]; while !seen.ends_with(b"\r\n\r\n") { @@ -2270,11 +2290,16 @@ fn a_late_timer_does_nothing(name: &str, held_timers: &str) { std::fs::create_dir_all(&disk).unwrap(); set_backup_folder(&mut w, &disk); w.end(); - wait_for( - "the timers hold their handles", - Duration::from_secs(60), - all_held, - ); + // (Not `wait_for`: a failure says which of the timers never held.) + let by = std::time::Instant::now() + Duration::from_secs(60); + while !all_held() { + assert!( + std::time::Instant::now() < by, + "never: the timers hold their handles; wanted `{held_timers}`, held so far {:?}", + seen().lines().collect::>() + ); + std::thread::sleep(Duration::from_millis(10)); + } let dir = home.book_dir(A); assert!(matches!(close_book(&home, A), CloseOutcome::Closed)); for folder in ["uploads", "exports"] { @@ -2412,7 +2437,11 @@ fn a_close_stops_a_slow_drill_instead_of_waiting_for_it() { /// where a `reference-transaction` hook waits, while `slow-commit` (for the /// book's archive) or `slow-push` (for any other repository) exists in the /// shim folder, at the point where git holds its ref locks. `hooked` has a -/// line per hook that waited. A real git, killed inside real work. +/// line per hook that waited. A real git, killed inside real work. While +/// `deaf` exists in the shim folder when a git begins, that git (and all it +/// starts) has SIGTERM blocked, a mask every child inherits: the signal stays +/// pending and only the kill ends it (a stopped process is not enough: +/// macOS acts on a SIGTERM sent to one, where Linux holds it). #[cfg(unix)] fn slow_real_git(home: &Home) -> PathBuf { use std::os::unix::fs::PermissionsExt; @@ -2434,8 +2463,9 @@ fn slow_real_git(home: &Home) -> PathBuf { std::fs::write( &exe, format!( - "#!/bin/sh\nn=$#\nwhile [ $n -gt 0 ]; do\n a=\"$1\"; shift\n case \"$a\" in\n *core.hooksPath=/dev/null*) a=$(printf '%s' \"$a\" | sed 's|core.hooksPath=/dev/null|core.hooksPath={}|g') ;;\n esac\n set -- \"$@\" \"$a\"\n n=$((n-1))\ndone\nexec {real} \"$@\"\n", - hooks.display() + "#!/bin/sh\nn=$#\nwhile [ $n -gt 0 ]; do\n a=\"$1\"; shift\n case \"$a\" in\n *core.hooksPath=/dev/null*) a=$(printf '%s' \"$a\" | sed 's|core.hooksPath=/dev/null|core.hooksPath={}|g') ;;\n esac\n set -- \"$@\" \"$a\"\n n=$((n-1))\ndone\nif [ -e '{dir}/deaf' ]; then exec perl -MPOSIX -e 'sigprocmask(SIG_BLOCK, POSIX::SigSet->new(SIGTERM)) or die; exec @ARGV or die' {real} \"$@\"; fi\nexec {real} \"$@\"\n", + hooks.display(), + dir = dir.display(), ), ) .unwrap(); @@ -2641,10 +2671,10 @@ fn call_unanswered(home: &Home, w: &Wire, id: u64, tool: &str, args: Value) -> r raw } -/// Hold every process of the group `pid` leads still (SIGSTOP): a SIGTERM -/// sent to it then stays pending, git can't end on it, and only the kill -/// after the grace ends it (how a git that can't end on SIGTERM, holding -/// its locks, is made on purpose). +/// Hold every process of the group `pid` leads still (SIGSTOP), so that none +/// goes on while the stop is being asked for. That git can't end on SIGTERM, +/// holding its locks, is made on purpose by the shim's `deaf` file, not by +/// this: macOS acts on a SIGTERM sent to a stopped process. #[cfg(unix)] fn freeze_group_of(pid: u32) { let out = std::process::Command::new("ps") @@ -3558,6 +3588,7 @@ fn a_lock_a_kill_leaves_in_the_backup_folder_is_reported_and_never_removed_by_th // named. let another = target.join("refs").join("heads").join("another.lock"); std::fs::write(&another, b"").unwrap(); + std::fs::write(shim.join("deaf"), b"").unwrap(); std::fs::write(shim.join("slow-push"), b"").unwrap(); let closing = std::thread::spawn({ let admin = home.run().join("admin.sock"); diff --git a/crates/ledger-mcp/src/local.rs b/crates/ledger-mcp/src/local.rs index d767ab6f..18dc51df 100644 --- a/crates/ledger-mcp/src/local.rs +++ b/crates/ledger-mcp/src/local.rs @@ -5418,8 +5418,14 @@ mod tests { if path.is_dir() { walk(base, &path, out); } else if let Ok(bytes) = std::fs::read(&path) { + // Keyed with `/` between parts on every system (the + // test names files so), not the system's separator. out.insert( - path.strip_prefix(base).unwrap().display().to_string(), + path.strip_prefix(base) + .unwrap() + .display() + .to_string() + .replace('\\', "/"), bytes, ); } diff --git a/crates/ledger-store/src/backup.rs b/crates/ledger-store/src/backup.rs index 4ea1f6ec..c09765c5 100644 --- a/crates/ledger-store/src/backup.rs +++ b/crates/ledger-store/src/backup.rs @@ -2561,6 +2561,10 @@ mod tests { /// `hooks/slow` exists, at the point where git holds its ref locks (and /// a commit its index lock): how a real git is made slow enough to be /// killed inside the work. `hooks/hooked` has a line per hook that waited. + /// While `hooks/deaf` exists when a git begins, that git (and all it + /// starts) has SIGTERM blocked, as a mask every child inherits: the + /// signal stays pending, git's handler never runs, and only the kill + /// ends it ([`deaf`]). fn slow_git(dir: &Path, real: &Git) -> (Git, PathBuf) { use std::os::unix::fs::PermissionsExt; let hooks = dir.join("slow-hooks"); @@ -2585,8 +2589,10 @@ mod tests { std::fs::write( &wrapper, format!( - "#!/bin/sh\nn=$#\nwhile [ $n -gt 0 ]; do\n a=\"$1\"; shift\n case \"$a\" in\n *core.hooksPath=/dev/null*) a=$(printf '%s' \"$a\" | sed 's|core.hooksPath=/dev/null|core.hooksPath={}|g') ;;\n esac\n set -- \"$@\" \"$a\"\n n=$((n-1))\ndone\nexec '{}' \"$@\"\n", + "#!/bin/sh\nn=$#\nwhile [ $n -gt 0 ]; do\n a=\"$1\"; shift\n case \"$a\" in\n *core.hooksPath=/dev/null*) a=$(printf '%s' \"$a\" | sed 's|core.hooksPath=/dev/null|core.hooksPath={}|g') ;;\n esac\n set -- \"$@\" \"$a\"\n n=$((n-1))\ndone\nif [ -e '{}/deaf' ]; then exec perl -MPOSIX -e 'sigprocmask(SIG_BLOCK, POSIX::SigSet->new(SIGTERM)) or die; exec @ARGV or die' '{}' \"$@\"; fi\nexec '{}' \"$@\"\n", hooks.display(), + hooks.display(), + real.exe.display(), real.exe.display() ), ) @@ -2635,10 +2641,19 @@ mod tests { .collect() } - /// Hold every process of the group `pid` leads still (SIGSTOP): a - /// SIGTERM sent to it then stays pending, git can't run its handler, - /// and only the kill after the grace ends it. This is how a git that - /// can't end on SIGTERM, with the locks it holds, is made on purpose. + /// Make the gits `slow_git` starts from now on unable to end on + /// SIGTERM (a blocked signal stays pending on every system). A stopped + /// process is not enough: macOS acts on a SIGTERM sent to a process + /// that was stopped (the git ended inside its grace, and removed its + /// own locks), where Linux holds the signal until it is continued. + fn deaf(hooks: &Path) { + std::fs::write(hooks.join("deaf"), b"").unwrap(); + } + + /// Hold every process of the group `pid` leads still (SIGSTOP), so that + /// none goes on while the stop is being asked for. A git that can't end + /// on SIGTERM, with the locks it holds, is made on purpose by [`deaf`]; + /// this only keeps it from finishing meanwhile. fn freeze_group_of(pid: u32) { let out = std::process::Command::new("ps") .args(["-o", "pgid=", "-p", &pid.to_string()]) @@ -2738,6 +2753,7 @@ mod tests { s.add_account("6000", "Supplies", AccountType::Expense, None, false) .unwrap(); let archive = b.join(ARCHIVE_DIR); + deaf(&hooks); std::fs::write(hooks.join("slow"), b"").unwrap(); let running = { let (git, b, head) = (cancelled.clone(), b.clone(), s.head()); @@ -2917,6 +2933,7 @@ mod tests { .clone() .with_cancel(flag.clone()) .with_term_grace(grace); + deaf(&hooks); std::fs::write(hooks.join("slow"), b"").unwrap(); let running = { let (git, b, target) = (cancelled.clone(), b.clone(), target.clone()); @@ -3015,6 +3032,9 @@ mod tests { .clone() .with_cancel(flag.clone()) .with_term_grace(Duration::from_millis(if frozen { 400 } else { 120_000 })); + if frozen { + deaf(&hooks); + } std::fs::write(hooks.join("slow-pack"), b"").unwrap(); let running = { let (git, b, target) = (cancelled.clone(), b.clone(), target.clone()); diff --git a/deny.toml b/deny.toml index 96e457c0..8ba8f0fe 100644 --- a/deny.toml +++ b/deny.toml @@ -31,6 +31,12 @@ ignore = [ # Since 2026-11-09. { id = "RUSTSEC-2026-0194", reason = "typst's citationberg only; no outside XML reaches it" }, { id = "RUSTSEC-2026-0195", reason = "typst's citationberg only; no outside XML reaches it" }, + # chrono 0.4.45, RUSTSEC-2026-0543: an "unmaintained" notice, not a + # vulnerability, and the advisory offers no upgrade. chrono is a + # declared dependency of ledger-core, so the "workspace" rule above + # catches it. Replacing chrono with jiff (already a dependency) is a + # follow-up for the owner to decide. Since 2026-11-21. + { id = "RUSTSEC-2026-0543", reason = "unmaintained notice only, not a vulnerability; no upgrade offered; chrono to jiff is the owner's call" }, ] [licenses]