fix(mcp,store): a close stops every call and job that can run long, and says so; an unrecorded refusal never ran
A close waited, unbounded, for a running books.back_up_now (git only Backups::shut could cancel), for a restore, create or adopt (they hold the one lifecycle lock, and at the server's close admin_jobs.wait() waited for them before anything was released), and, through the store lock, for a verification or a package reading every stored file. The bound's T_calls term was not finite. Now a close calls hurry at its start, and the stoppable work looks at it: books.back_up_now and timer-asked backups (git stopped, a stopped run records 'stopped' and no result of its own), books.verify (per journal line and file; a heal of a drifted database is begun only if no stop came before it: stopped half way it would leave the live ledger part replayed), books.export_cpa_package (per stored file). Such a call answers that it was stopped because the books are closing, and its access record says refused: stopped_by_close. On the admin socket restore, adopt, create and open take a JobStop: asked at each phase and file copied, they remove what they wrote and leave the root byte for byte as it was; the renames that put the books in place are the point of no return, after which they finish (two renames and two syncs). A close of any book, and the server's own, raises the flag before it waits for the lifecycle lock. A close whose last backup had to be killed in the owner's folder now says which file was left, and the target's backup is marked failed by shut when the run's future was dropped at its bound. INVARIANT 10 is rewritten so each term of the bound is enforced by code, and lists the calls that can run long (and the one known exception, books.import_bank_csv); INVARIANTS 8 and 9 say exactly which refusals are recorded and why an unrecorded one never ran; the format upgrade's wording says a stop is honoured up to its final look. Tests: the close is held to the bound while a backup call, a verification, a package and each admin job is running and would never end; the stopped job leaves the root byte for byte as it was; a lock left in a backup folder is reported and never removed. Sleeps that stood for 'the other side got there' now wait on the condition (Expect: 100-continue for a half-sent body, a writable named pipe for a fetch that is reading). Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
6f174f3f87
commit
aa64df9a25
18 files changed
+1889
-248
No files matched your search
@@ -1734,3 +1734,145 @@ fn a_registry_rewritten_or_a_vault_given_unchecked_is_refused() {
|
||||
}
|
||||
assert!(p.vault.is_dir() && p.key.is_file());
|
||||
}
|
||||
|
||||
/// A stop that is asked at `phase` (and the phases a job has asked, in
|
||||
/// order): the job's own look at its phases, raised from the watcher.
|
||||
fn stopping_at(
|
||||
phase: &'static str,
|
||||
) -> (
|
||||
ledger_store::root::JobStop,
|
||||
std::sync::Arc<std::sync::Mutex<Vec<String>>>,
|
||||
) {
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::sync::{Arc, Mutex};
|
||||
let flag = Arc::new(AtomicBool::new(false));
|
||||
let seen = Arc::new(Mutex::new(Vec::<String>::new()));
|
||||
let stop = ledger_store::root::JobStop::new(flag.clone()).with_phase_watch({
|
||||
let (flag, seen) = (flag.clone(), seen.clone());
|
||||
Arc::new(move |p: &str| {
|
||||
seen.lock().unwrap().push(p.to_string());
|
||||
if p == phase {
|
||||
flag.store(true, Ordering::SeqCst);
|
||||
}
|
||||
})
|
||||
});
|
||||
(stop, seen)
|
||||
}
|
||||
|
||||
/// What a job leaves of the root, byte for byte.
|
||||
fn root_files(root: &Root) -> Vec<(PathBuf, Vec<u8>)> {
|
||||
let mut out = snapshot_files(&root.path().join("books"));
|
||||
if root.path().join("legacy").exists() {
|
||||
out.extend(snapshot_files(&root.path().join("legacy")));
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// A job that changes which books the root holds (create, adopt, restore) is
|
||||
/// asked at each of its phases whether it must stop (the books server's
|
||||
/// close), and stopped at any of them leaves the root byte for byte as it
|
||||
/// was: the staging and scratch folders are gone, books it would have
|
||||
/// replaced untouched, `legacy/` as it was. The last phase it asks is the
|
||||
/// look before its point of no return (the renames that put the books in
|
||||
/// place): nothing is asked after, so a stop that comes then is not heeded
|
||||
/// and the job finishes.
|
||||
#[test]
|
||||
fn a_job_stopped_at_any_phase_before_its_point_of_no_return_leaves_the_root_as_it_was() {
|
||||
use ledger_store::root::JobStop;
|
||||
let home = tmp("job-stop");
|
||||
let (root, _) = Root::setup(home.path(), None, None).unwrap();
|
||||
// create
|
||||
let before = root_files(&root);
|
||||
let (stop, seen) = stopping_at("start");
|
||||
let e = refused(
|
||||
root.create_book_stoppable(&id("new"), Currency::USD, &stop)
|
||||
.map(|_| ()),
|
||||
);
|
||||
assert!(matches!(e, RootError::Stopped(_)), "{e:?}");
|
||||
assert!(e.to_string().contains("nothing was created"), "{e}");
|
||||
assert_eq!(root_files(&root), before);
|
||||
assert!(root.list_books().unwrap().is_empty());
|
||||
assert!(!root.book_dir(&id("new")).exists(), "its folder was made");
|
||||
assert_eq!(*seen.lock().unwrap(), ["start"]);
|
||||
// adopt
|
||||
let old = tmp("job-stop-old");
|
||||
drop(source_books(old.path(), true));
|
||||
let options = AdoptOptions::default();
|
||||
let phases = [
|
||||
"start", "journal", "copy", "file", "open", "verify", "place",
|
||||
];
|
||||
for phase in phases {
|
||||
let before = root_files(&root);
|
||||
let (stop, seen) = stopping_at(phase);
|
||||
let e = refused(root.adopt_stoppable(old.path(), &id("adopted"), &options, &stop));
|
||||
assert!(matches!(e, RootError::Stopped(_)), "{phase}: {e:?}");
|
||||
assert!(
|
||||
e.to_string().contains("nothing was adopted"),
|
||||
"{phase}: {e}"
|
||||
);
|
||||
assert_eq!(root_files(&root), before, "adopt stopped at {phase}");
|
||||
assert!(root.list_books().unwrap().is_empty(), "{phase}");
|
||||
assert_eq!(seen.lock().unwrap().last().map(String::as_str), Some(phase));
|
||||
}
|
||||
// Never stopped: every phase is asked, in this order, `place` the last.
|
||||
let seen = std::sync::Arc::new(std::sync::Mutex::new(Vec::<String>::new()));
|
||||
let flag = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false));
|
||||
let stop = JobStop::new(flag).with_phase_watch({
|
||||
let seen = seen.clone();
|
||||
std::sync::Arc::new(move |p: &str| seen.lock().unwrap().push(p.to_string()))
|
||||
});
|
||||
root.adopt_stoppable(old.path(), &id("adopted"), &options, &stop)
|
||||
.unwrap();
|
||||
let asked = seen.lock().unwrap().clone();
|
||||
let mut distinct = asked.clone();
|
||||
distinct.dedup();
|
||||
assert_eq!(distinct, phases, "{asked:?}");
|
||||
assert_eq!(asked.last().map(String::as_str), Some("place"), "{asked:?}");
|
||||
// restore, over books that exist: they stay as they are.
|
||||
let dir = root.book_dir(&id("adopted"));
|
||||
let store = Store::open(&dir).unwrap();
|
||||
let source = backed_up(home.path(), &dir, &store).expect("git is installed");
|
||||
drop(store);
|
||||
let git = ledger_store::backup::Git::find().unwrap();
|
||||
let preview = root.restore_preview(&git, &source, &id("adopted")).unwrap();
|
||||
let phases = ["start", "fetch", "copy", "file", "open", "verify", "place"];
|
||||
for phase in phases {
|
||||
let before = root_files(&root);
|
||||
let (stop, seen) = stopping_at(phase);
|
||||
let e = refused(root.restore_stoppable(
|
||||
&git,
|
||||
&source,
|
||||
&id("adopted"),
|
||||
&preview.head,
|
||||
true,
|
||||
&stop,
|
||||
));
|
||||
assert!(matches!(e, RootError::Stopped(_)), "{phase}: {e:?}");
|
||||
assert!(
|
||||
e.to_string().contains("nothing was restored"),
|
||||
"{phase}: {e}"
|
||||
);
|
||||
assert_eq!(root_files(&root), before, "restore stopped at {phase}");
|
||||
assert_eq!(seen.lock().unwrap().last().map(String::as_str), Some(phase));
|
||||
}
|
||||
// The preview too: stopped, nothing left of its clone.
|
||||
let before = root_files(&root);
|
||||
let (stop, _) = stopping_at("fetch");
|
||||
let e = refused(root.restore_preview_stoppable(&git, &source, &id("adopted"), &stop));
|
||||
assert!(matches!(e, RootError::Stopped(_)), "{e:?}");
|
||||
assert_eq!(root_files(&root), before);
|
||||
// Never stopped, a restore asks `place` last and goes through.
|
||||
let seen = std::sync::Arc::new(std::sync::Mutex::new(Vec::<String>::new()));
|
||||
let stop = JobStop::new(std::sync::Arc::default()).with_phase_watch({
|
||||
let seen = seen.clone();
|
||||
std::sync::Arc::new(move |p: &str| seen.lock().unwrap().push(p.to_string()))
|
||||
});
|
||||
let r = root
|
||||
.restore_stoppable(&git, &source, &id("adopted"), &preview.head, true, &stop)
|
||||
.unwrap();
|
||||
assert!(r.replaced.is_some());
|
||||
assert_eq!(
|
||||
seen.lock().unwrap().last().map(String::as_str),
|
||||
Some("place")
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user