fix(host): what holds the books is known, not guessed; a command joins what starts beside it
Plan P15 unit A5, fix round. A server answering its admin socket with no host over it was taken to be "the app". Three wrong outcomes followed: a `serve` started while a killed host's server was still finishing its last backup was told "the app has these books open" and exited, `close` and `doctor` said "the app" during such a close, and a command attached to another command's short-lived server as if it were the app (refused "not open" for another company, or its server closed under the other). A server now says what it is. `wanderbooks server --started-by app|host|command` writes run/server.info beside its lock (who started it, its pid, whether it has begun to close), moved into place whole as soon as the lock is its, rewritten `closing` the moment it is told to close, removed just before the lock goes. It is read only while the lock is held, which is when it is the lock holder's. A file beside the lock, not a field in the admin hello: it also covers a server that has no admin socket yet (opening) and costs the older admin protocol nothing. A server that says nothing (an earlier build's) is taken for the app's, as before. `opener::what_holds` reads it: only a server the app started, not closing and answering, is the app. A command's server, or one that is closing (the app's, or the orphan of a host that went), is waited for, said in words, bounded by `open_wait` (a bridge waits as long as its session does), and the holder or command goes on once it is gone. `close`, `doctor` and the bridge say the true thing for each. A command racing another program's start no longer fails with "close it" or waits silently 300 s: if its own server loses the lock to a program that started after its look, it lets its start go and joins what runs (`wait_ready_unless_beaten`, `reach`), inside one `open_wait`. A command's own server is started clean, as a host is: the host's environment only, the root as an absolute path, `/` as its folder (Windows: `run/`), and every server closes the descriptors above 2 at its start, so one that outlives its command (still closing after 90 s) holds none of the invoker's pipes. The app's own child keeps its environment: it is the window's child, closes with the window and needs the user's session (ssh agent, git configuration) for backups; it too loses stray descriptors, since the server does that for any starter. Test knobs: `--start-delay` (a server's slow start) and `timer_hold=1` for a command's own server, following `--timer-hold`. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
1 parent
6b95ce59ad
commit
2ff2913274
9 files changed
+598
-80
No files matched your search
@@ -39,6 +39,17 @@
|
||||
//! books. The server's lock held and no admin socket yet is a server
|
||||
//! still opening its companies (or finishing its last backup): the
|
||||
//! holder waits for it, in words, for as long as its own wait lasts.
|
||||
//! 6a. What holds the books is known, not guessed: a server says what
|
||||
//! started it and whether it is closing (`run/server.info`, ledger-mcp
|
||||
//! local.rs INVARIANT 16) and [`what_holds`] reads it. Only a server
|
||||
//! the app started, not closing, is "the app". A server a command
|
||||
//! started, a closing server (the app's, or the orphan of a host
|
||||
//! that was killed), is waited for: the holder says so, in words, and
|
||||
//! goes on once it is gone, for as long as its own wait lasts
|
||||
//! (`open_wait`, 120 s; a bridge waits as long as the session does),
|
||||
//! then says in words what it is still waiting for. A server that
|
||||
//! says nothing (an earlier build's) is taken for the app's, as it
|
||||
//! was before.
|
||||
//! 7. No host, no server: only `wanderbooks serve` starts a host (the
|
||||
//! headless path, plan 3.1). Every other holder is turned away with
|
||||
//! [`HoldError::NotRunning`], and says, in words, to start
|
||||
@@ -62,6 +73,9 @@ const START_WAIT: Duration = Duration::from_secs(45);
|
||||
const LOCK_WAIT: Duration = Duration::from_secs(2);
|
||||
/// How long the host has to answer a hello.
|
||||
const ANSWER_WAIT: Duration = Duration::from_secs(10);
|
||||
/// How long `hold` looks again while it waits for a server that is
|
||||
/// closing or a command's (INVARIANT 6a).
|
||||
const WAIT_LOOK: Duration = Duration::from_millis(100);
|
||||
/// Tries after a failure before giving up (INVARIANT 3).
|
||||
pub const MAX_TRIES: u32 = 5;
|
||||
/// How long the books must stay open for earlier failures to be
|
||||
@@ -128,6 +142,10 @@ pub struct Opener {
|
||||
/// Flags the server this opener starts is started with (P9a):
|
||||
/// `--isolation` and `--http`. Empty for every ordinary holder.
|
||||
server_flags: Vec<String>,
|
||||
/// Until when a wait for another program's server lasts (INVARIANT
|
||||
/// 6a); the caller's own deadline, else `open_wait` from the first
|
||||
/// wait. A bridge has none.
|
||||
deadline: Option<Instant>,
|
||||
}
|
||||
|
||||
/// A held lease: the books stay open while it lives.
|
||||
@@ -193,9 +211,21 @@ impl Opener {
|
||||
opened_at: None,
|
||||
has_held: false,
|
||||
server_flags: Vec::new(),
|
||||
deadline: None,
|
||||
})
|
||||
}
|
||||
|
||||
/// Wait for another program's server (a command's, or one closing) no
|
||||
/// longer than until `deadline` (INVARIANT 6a).
|
||||
pub fn wait_until(&mut self, deadline: Instant) {
|
||||
self.deadline = Some(deadline);
|
||||
}
|
||||
|
||||
/// The deadline set by [`Opener::wait_until`], if any.
|
||||
pub fn deadline(&self) -> Option<Instant> {
|
||||
self.deadline
|
||||
}
|
||||
|
||||
/// Start the server this opener brings up with these flags (P9a:
|
||||
/// `--isolation`, `--http`). Only `serve` uses it: on the owner's own
|
||||
/// machine it is the one holder, so the host it starts is the server's
|
||||
@@ -244,39 +274,54 @@ impl Opener {
|
||||
pub async fn hold(&mut self) -> Result<Lease, HoldError> {
|
||||
let mut started: Option<Instant> = None;
|
||||
let mut said_waiting = false;
|
||||
let mut waited_from: Option<Instant> = None;
|
||||
loop {
|
||||
// The app has the books: its own server serves them, and no
|
||||
// host is started beside it (INVARIANT 6).
|
||||
if !host_lock_held(&self.run) && server_lock_held(&self.run) {
|
||||
match admin_answers(&self.run) {
|
||||
Ok(true) => {
|
||||
if self.kind == Kind::Serve {
|
||||
return Err(HoldError::Failed(
|
||||
"Wanderbooks (the app) has these books open: close it first, or use it"
|
||||
.into(),
|
||||
));
|
||||
}
|
||||
self.has_held = true;
|
||||
return Ok(Lease {
|
||||
id: 0,
|
||||
link: None,
|
||||
told_ready: false,
|
||||
});
|
||||
// What holds the books is read, not guessed (INVARIANT 6a).
|
||||
match what_holds(&self.run)? {
|
||||
// The app has the books: its own server serves them, and
|
||||
// no host is started beside it (INVARIANT 6).
|
||||
Holder::App => {
|
||||
if self.kind == Kind::Serve {
|
||||
return Err(HoldError::Failed(
|
||||
"Wanderbooks (the app) has these books open: close it first, or use it"
|
||||
.into(),
|
||||
));
|
||||
}
|
||||
Ok(false) => {
|
||||
// Opening its companies, or finishing its last
|
||||
// backup: wait for it, and say so once.
|
||||
if !std::mem::replace(&mut said_waiting, true) && self.kind != Kind::Bridge
|
||||
{
|
||||
eprintln!(
|
||||
"Wanderbooks is still opening (or closing) the books: waiting for it…"
|
||||
);
|
||||
}
|
||||
tokio::time::sleep(Duration::from_millis(100)).await;
|
||||
continue;
|
||||
}
|
||||
Err(why) => return Err(HoldError::Failed(why)),
|
||||
self.has_held = true;
|
||||
return Ok(Lease {
|
||||
id: 0,
|
||||
link: None,
|
||||
told_ready: false,
|
||||
});
|
||||
}
|
||||
// Opening its companies, or finishing its last backup:
|
||||
// wait for it, and say so once.
|
||||
Holder::Opening => {
|
||||
if !std::mem::replace(&mut said_waiting, true) && self.kind != Kind::Bridge {
|
||||
eprintln!(
|
||||
"Wanderbooks is still opening (or closing) the books: waiting for it…"
|
||||
);
|
||||
}
|
||||
tokio::time::sleep(WAIT_LOOK).await;
|
||||
continue;
|
||||
}
|
||||
// A command's server, or one closing: it ends soon, and
|
||||
// the books are free; wait for it, bounded (INVARIANT 6a).
|
||||
held @ (Holder::Command | Holder::Closing) => {
|
||||
let from = *waited_from.get_or_insert_with(Instant::now);
|
||||
if self.kind != Kind::Bridge {
|
||||
let until = self.deadline.unwrap_or_else(|| from + self.knobs.open_wait);
|
||||
if Instant::now() >= until {
|
||||
return Err(HoldError::Failed(held.still_waiting_words()));
|
||||
}
|
||||
}
|
||||
if !std::mem::replace(&mut said_waiting, true) && self.kind != Kind::Bridge {
|
||||
eprintln!("{}", held.waiting_words());
|
||||
}
|
||||
tokio::time::sleep(WAIT_LOOK).await;
|
||||
continue;
|
||||
}
|
||||
Holder::Host | Holder::Nothing => {}
|
||||
}
|
||||
wait_for_restart(&self.run).await;
|
||||
let endpoint = ledger_mcp::ipc::Endpoint::new(&self.run, lease::LEASE_SOCKET)
|
||||
@@ -457,6 +502,80 @@ pub fn host_lock_held(run: &Path) -> bool {
|
||||
}
|
||||
}
|
||||
|
||||
/// What holds the books now, read from the locks, the server's own word
|
||||
/// about itself and its admin socket (INVARIANT 6a).
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum Holder {
|
||||
/// Nothing: no host, no server.
|
||||
Nothing,
|
||||
/// A host holds `run/host.lock` (`wanderbooks serve`'s, or an earlier
|
||||
/// build's): leases go to it.
|
||||
Host,
|
||||
/// The app's server, not closing, answering: no host behind it.
|
||||
App,
|
||||
/// A server the app started (or an earlier build's, which says
|
||||
/// nothing of itself) that is not answering yet: opening its
|
||||
/// companies, or finishing its last backup.
|
||||
Opening,
|
||||
/// A server a command started, for the length of its work.
|
||||
Command,
|
||||
/// A server that has begun to close (the app's, or the orphan of a
|
||||
/// host that went): finishing its last backups.
|
||||
Closing,
|
||||
}
|
||||
|
||||
impl Holder {
|
||||
/// What is said while waiting for this to finish (INVARIANT 6a).
|
||||
pub fn waiting_words(self) -> &'static str {
|
||||
match self {
|
||||
Holder::Command => {
|
||||
"Another wanderbooks command has the books open for a moment: waiting for it to finish…"
|
||||
}
|
||||
_ => {
|
||||
"Wanderbooks is closing the books (a last backup is being made): waiting for it to finish…"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// What is said when the wait ran out (INVARIANT 6a).
|
||||
pub fn still_waiting_words(self) -> String {
|
||||
match self {
|
||||
Holder::Command => "Another wanderbooks command still has the books open after waiting for it: try again in a moment (`wanderbooks doctor` says what has them).".into(),
|
||||
_ => "Wanderbooks is still closing the books after waiting for it (a last backup is still being made): try again in a moment.".into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// What holds the books of the root whose `run/` folder this is
|
||||
/// (INVARIANT 6a). `Err` in words when the server's socket is another
|
||||
/// account's.
|
||||
pub fn what_holds(run: &Path) -> Result<Holder, String> {
|
||||
use ledger_mcp::local::StartedBy;
|
||||
if host_lock_held(run) {
|
||||
return Ok(Holder::Host);
|
||||
}
|
||||
if !server_lock_held(run) {
|
||||
return Ok(Holder::Nothing);
|
||||
}
|
||||
let info = ledger_mcp::local::read_server_info(run);
|
||||
Ok(match info {
|
||||
Some(i) if i.closing => Holder::Closing,
|
||||
Some(i) if i.by == StartedBy::Command => Holder::Command,
|
||||
// No host holds the books, so a server a host started is the
|
||||
// orphan of one that went: it is closing, as its input ended.
|
||||
Some(i) if i.by == StartedBy::Host => Holder::Closing,
|
||||
// The app's, or one that says nothing (an earlier build's, or a
|
||||
// newer one's, a moment before it writes its word).
|
||||
Some(_) | None => {
|
||||
if admin_answers(run)? {
|
||||
Holder::App
|
||||
} else {
|
||||
Holder::Opening
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/// Whether the admin socket answers: the books server is up and ready
|
||||
/// (it binds its sockets only once every company has been tried). A
|
||||
/// socket of another account is an error.
|
||||
@@ -569,6 +688,7 @@ mod tests {
|
||||
opened_at: None,
|
||||
has_held: false,
|
||||
server_flags: Vec::new(),
|
||||
deadline: None,
|
||||
};
|
||||
for _ in 0..MAX_TRIES {
|
||||
o.failed().await.unwrap();
|
||||
|
||||
Reference in new issue
Block a user