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
@@ -213,6 +213,11 @@ pub struct SharedStore {
|
||||
stopped: std::sync::atomic::AtomicBool,
|
||||
/// Closing: no new request runs (INVARIANT 3).
|
||||
closing: std::sync::atomic::AtomicBool,
|
||||
/// A close of these books has begun (`hurry`): a call that can run
|
||||
/// long (a verification, a backup asked for, a package of every stored
|
||||
/// file) stops at its next look, answering that it was stopped. Shared
|
||||
/// with the books' backups (backup.rs), whose git it stops.
|
||||
hurry: Arc<std::sync::atomic::AtomicBool>,
|
||||
/// Requests running (from before their blocking task to its end).
|
||||
running: std::sync::atomic::AtomicUsize,
|
||||
/// Woken whenever one ends.
|
||||
@@ -310,6 +315,7 @@ impl SharedStore {
|
||||
store: tokio::sync::Mutex::new(Some(store)),
|
||||
stopped: std::sync::atomic::AtomicBool::new(false),
|
||||
closing: std::sync::atomic::AtomicBool::new(false),
|
||||
hurry: Arc::default(),
|
||||
running: std::sync::atomic::AtomicUsize::new(0),
|
||||
ended: tokio::sync::Notify::new(),
|
||||
})
|
||||
@@ -334,6 +340,23 @@ impl SharedStore {
|
||||
self.closing.load(std::sync::atomic::Ordering::SeqCst)
|
||||
}
|
||||
|
||||
/// A close of these books begins: every call and job that can run long
|
||||
/// stops at its next look (local.rs INVARIANT 10).
|
||||
pub fn hurry(&self) {
|
||||
self.hurry.store(true, std::sync::atomic::Ordering::SeqCst);
|
||||
}
|
||||
|
||||
/// Whether a close of these books has begun.
|
||||
pub fn hurried(&self) -> bool {
|
||||
self.hurry.load(std::sync::atomic::Ordering::SeqCst)
|
||||
}
|
||||
|
||||
/// The stop flag itself, for what looks at it from another thread or
|
||||
/// hands it to git (ledger_store `cancel`, `Git::with_cancel`).
|
||||
pub fn hurry_flag(&self) -> Arc<std::sync::atomic::AtomicBool> {
|
||||
self.hurry.clone()
|
||||
}
|
||||
|
||||
fn stopped(&self) -> bool {
|
||||
self.stopped.load(std::sync::atomic::Ordering::SeqCst)
|
||||
}
|
||||
@@ -357,6 +380,18 @@ impl SharedStore {
|
||||
|
||||
/// What every request gets after an internal error (SharedStore
|
||||
/// INVARIANT 2).
|
||||
/// What a call that a close stopped is told (local.rs INVARIANT 10): it
|
||||
/// was stopped, because the books are closing, and nothing it was doing
|
||||
/// is recorded as done.
|
||||
pub(crate) const STOPPED_BY_CLOSE_WORDS: &str = "this call was stopped because the books are closing: nothing it was doing is recorded as done. Open the books again, then ask again";
|
||||
|
||||
/// The words of a call a close stopped, and the mark that its access record
|
||||
/// says so (`refused: stopped_by_close`): returned as the call's error.
|
||||
pub(crate) fn stopped_by_close() -> String {
|
||||
STOPPED_BY_CLOSE.with(|s| s.set(true));
|
||||
STOPPED_BY_CLOSE_WORDS.to_string()
|
||||
}
|
||||
|
||||
pub(crate) const STOPPED: &str = "the books server stopped taking requests after an internal error, to protect the books: ask the human to restart the books server (the books are checked again when it starts)";
|
||||
|
||||
/// The store, locked for one stretch of synchronous work. Not `Send`, so
|
||||
@@ -1385,6 +1420,9 @@ thread_local! {
|
||||
/// The journal's head after the last write made by the request
|
||||
/// running on this thread (StoreGuard's drop), for its access record.
|
||||
static WROTE: std::cell::RefCell<Option<(u64, String)>> = const { std::cell::RefCell::new(None) };
|
||||
/// Whether the request running on this thread was stopped by a close
|
||||
/// (`stopped_by_close`), for its access record.
|
||||
static STOPPED_BY_CLOSE: std::cell::Cell<bool> = const { std::cell::Cell::new(false) };
|
||||
/// Who the request running on this thread is, as `books.start` shows
|
||||
/// it: `{principal, kind, scopes, client_id}`. The token's own facts
|
||||
/// and the client's name; set at the start of the request.
|
||||
@@ -1843,6 +1881,7 @@ impl ServerHandler for BooksServer {
|
||||
let result = run_blocking(&self.store, move || {
|
||||
Box::pin(async move {
|
||||
WROTE.with(|w| w.borrow_mut().take());
|
||||
STOPPED_BY_CLOSE.with(|s| s.set(false));
|
||||
ARGS.with(|a| *a.borrow_mut() = request.arguments.clone());
|
||||
let caller = running_call
|
||||
.caller()
|
||||
@@ -1895,7 +1934,14 @@ impl ServerHandler for BooksServer {
|
||||
let mut c = running_call;
|
||||
c.digested(digest);
|
||||
c.with_agent(AGENT_NOTE.with(|a| a.borrow_mut().take()));
|
||||
let recorded = c.record_blocking(outcome_of(&out), wrote);
|
||||
// A call a close stopped is on record as stopped, not as an
|
||||
// answer it gave (local.rs INVARIANT 10).
|
||||
let outcome = if STOPPED_BY_CLOSE.with(|s| s.replace(false)) {
|
||||
Outcome::Refused(access::STOPPED_BY_CLOSE.into())
|
||||
} else {
|
||||
outcome_of(&out)
|
||||
};
|
||||
let recorded = c.record_blocking(outcome, wrote);
|
||||
(out, recorded)
|
||||
})
|
||||
})
|
||||
|
||||
Reference in new issue
Block a user