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
@@ -55,7 +55,7 @@
|
||||
|
||||
use crate::knobs::{FLAG, Knobs};
|
||||
use crate::lease::{Ask, HostMsg, Intent, Kind, Reason};
|
||||
use crate::opener::{HoldError, Lease, Opener};
|
||||
use crate::opener::{HoldError, Holder, Lease, Opener};
|
||||
use ledger_mcp::admin::{self, Request, Response};
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::time::Duration;
|
||||
@@ -116,12 +116,20 @@ fn exit(code: i32) -> Box<dyn std::error::Error> {
|
||||
|
||||
/// Hold the books and wait until they are open (INVARIANT 1).
|
||||
async fn open(opener: &mut Opener) -> Result<Lease, HoldError> {
|
||||
let deadline = tokio::time::Instant::now() + opener.knobs().open_wait;
|
||||
// One bounded wait for the whole command: the deadline a caller set
|
||||
// (`reach`), else `open_wait` from now. The opener's own waits for
|
||||
// another program's server (WB-app-5) end at it with their own words;
|
||||
// the timeout here is a moment later, for what has none.
|
||||
let deadline = opener
|
||||
.deadline()
|
||||
.unwrap_or_else(|| tokio::time::Instant::now() + opener.knobs().open_wait);
|
||||
opener.wait_until(deadline);
|
||||
let grace = Duration::from_millis(500);
|
||||
let too_long = || -> HoldError {
|
||||
"the books didn't open in time (Wanderbooks may still be opening a large company: try again in a moment): run `wanderbooks doctor` to see why".into()
|
||||
};
|
||||
loop {
|
||||
let mut lease = tokio::time::timeout_at(deadline, opener.hold())
|
||||
let mut lease = tokio::time::timeout_at(deadline + grace, opener.hold())
|
||||
.await
|
||||
.map_err(|_| too_long())??;
|
||||
loop {
|
||||
@@ -254,33 +262,78 @@ impl Reached {
|
||||
|
||||
/// Hold the books for a command, or, with nothing running and the work
|
||||
/// one `direct` allows, run a server of its own (WB-app-5).
|
||||
///
|
||||
/// Whatever is found is joined: a host, the app's server, and, once its
|
||||
/// work is done, a command's server or a closing one is waited for. A
|
||||
/// program that takes the books between the look and the command's own
|
||||
/// server taking them (the app starting, `serve`, another command) is
|
||||
/// joined too: the command's server has not got them, it lets go of its
|
||||
/// start and looks again. One wait covers all of it, `open_wait`, said in
|
||||
/// words (`Opener::hold` says what it waits for).
|
||||
async fn reach(opener: &mut Opener, common: &Common, direct: &Direct) -> Result<Reached, String> {
|
||||
let run_dir = opener.run_dir().to_path_buf();
|
||||
match open(opener).await {
|
||||
Ok(lease) => Ok(Reached {
|
||||
run_dir,
|
||||
lease: Some(lease),
|
||||
own: None,
|
||||
}),
|
||||
Err(HoldError::NotRunning) => {
|
||||
let Direct::With(books) = direct else {
|
||||
return Err(crate::opener::START_WANDERBOOKS.to_string());
|
||||
let deadline = tokio::time::Instant::now() + opener.knobs().open_wait;
|
||||
opener.wait_until(deadline);
|
||||
let mut said = false;
|
||||
loop {
|
||||
match open(opener).await {
|
||||
Ok(lease) => {
|
||||
return Ok(Reached {
|
||||
run_dir,
|
||||
lease: Some(lease),
|
||||
own: None,
|
||||
});
|
||||
}
|
||||
Err(HoldError::NotRunning) => {}
|
||||
Err(e) => return Err(hold_words(e)),
|
||||
}
|
||||
let Direct::With(books) = direct else {
|
||||
return Err(crate::opener::START_WANDERBOOKS.to_string());
|
||||
};
|
||||
let (root, books, knobs) = (common.root.clone(), books.clone(), common.knobs);
|
||||
let started = tokio::task::spawn_blocking(move || {
|
||||
let server = match crate::app_server::AppServer::start_for_command(
|
||||
root.as_deref(),
|
||||
&books,
|
||||
&knobs,
|
||||
) {
|
||||
Ok(server) => server,
|
||||
// Another program took the books since the look.
|
||||
Err(why) if why == crate::app_server::ANOTHER_HAS_THEM => {
|
||||
return Ok(None);
|
||||
}
|
||||
Err(why) => return Err(why),
|
||||
};
|
||||
let (root, books) = (common.root.clone(), books.clone());
|
||||
let own = tokio::task::spawn_blocking(move || {
|
||||
let server = crate::app_server::AppServer::start(root.as_deref(), &books)?;
|
||||
server.wait_ready(crate::app_server::READY_WAIT)?;
|
||||
Ok::<_, String>(server)
|
||||
})
|
||||
.await
|
||||
.map_err(|e| e.to_string())??;
|
||||
Ok(Reached {
|
||||
match server.wait_ready_unless_beaten(crate::app_server::READY_WAIT)? {
|
||||
crate::app_server::Waited::Ready => Ok(Some(server)),
|
||||
// Another program took them first: this one's start is let
|
||||
// go of (it has not opened anything), and what runs is
|
||||
// looked at again.
|
||||
crate::app_server::Waited::Beaten => {
|
||||
server.stop(Duration::from_secs(10));
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
})
|
||||
.await
|
||||
.map_err(|e| e.to_string())??;
|
||||
if let Some(own) = started {
|
||||
return Ok(Reached {
|
||||
run_dir,
|
||||
lease: None,
|
||||
own: Some(std::sync::Arc::new(own)),
|
||||
})
|
||||
});
|
||||
}
|
||||
Err(e) => Err(hold_words(e)),
|
||||
if tokio::time::Instant::now() >= deadline {
|
||||
return Err(format!(
|
||||
"Other programs kept the books for {} s while this command waited: try again in a moment (`wanderbooks doctor` says what has them).",
|
||||
opener.knobs().open_wait.as_secs()
|
||||
));
|
||||
}
|
||||
if !std::mem::replace(&mut said, true) {
|
||||
eprintln!("Another program started the books just now: waiting for it…");
|
||||
}
|
||||
tokio::time::sleep(Duration::from_millis(50)).await;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -359,6 +412,23 @@ fn past(deadline: std::time::Instant, now: std::time::Instant) -> bool {
|
||||
now > deadline
|
||||
}
|
||||
|
||||
/// What is said of a server that is not the app's, in the words of its
|
||||
/// own state (`close`, `doctor`): never "the app" for what is not.
|
||||
fn held_words(holder: Holder) -> &'static str {
|
||||
match holder {
|
||||
Holder::Command => {
|
||||
"A wanderbooks command has the books open for a moment: they close when its work is done."
|
||||
}
|
||||
Holder::Closing => "The books are closing: a books server is finishing its last backups.",
|
||||
Holder::Opening => {
|
||||
"Wanderbooks is opening the books (or finishing the last backup of the last session): its server isn't answering yet."
|
||||
}
|
||||
Holder::Host | Holder::App | Holder::Nothing => {
|
||||
"The books are held: `wanderbooks doctor` says by what."
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// What `doctor` says of books no host holds.
|
||||
fn no_host(server_lock_held: bool) -> &'static str {
|
||||
if server_lock_held {
|
||||
@@ -569,16 +639,18 @@ pub fn close(
|
||||
exit(1)
|
||||
})?;
|
||||
match asked {
|
||||
None if crate::app_server::the_apps_server_runs(&run) => {
|
||||
println!("{APP_HAS_THEM}.");
|
||||
Ok(())
|
||||
}
|
||||
None if crate::opener::server_lock_held(&run) => {
|
||||
println!("The books are closing: a books server is finishing its last call.");
|
||||
Ok(())
|
||||
}
|
||||
// No host: what holds the books is read (WB-app-5), so what is
|
||||
// said of it is true.
|
||||
None => {
|
||||
println!("The books are already closed.");
|
||||
match crate::opener::what_holds(&run) {
|
||||
Ok(Holder::App) => println!("{APP_HAS_THEM}."),
|
||||
Ok(Holder::Nothing) => println!("The books are already closed."),
|
||||
Ok(other) => println!("{}", held_words(other)),
|
||||
Err(why) => {
|
||||
eprintln!("error: {why}");
|
||||
return Err(exit(1));
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
Some(HostMsg::Done { .. }) => {
|
||||
@@ -2995,14 +3067,15 @@ pub fn doctor(
|
||||
match crate::opener::ask_once_within(&run, Ask::Leases, DOCTOR_WAIT) {
|
||||
// No host: the app's server (or another that runs without one),
|
||||
// one still opening or finishing, or nothing (WB-app-3).
|
||||
Ok(None) if crate::app_server::the_apps_server_runs(&run) => {
|
||||
println!("The books are open: Wanderbooks (the app) has them.");
|
||||
admin_report(&run);
|
||||
}
|
||||
Ok(None) if crate::opener::server_lock_held(&run) => println!(
|
||||
"Wanderbooks is opening the books (or finishing the last backup of the last session): its server isn't answering yet."
|
||||
),
|
||||
Ok(None) => println!("{}", no_host(false)),
|
||||
Ok(None) => match crate::opener::what_holds(&run) {
|
||||
Ok(Holder::App) => {
|
||||
println!("The books are open: Wanderbooks (the app) has them.");
|
||||
admin_report(&run);
|
||||
}
|
||||
Ok(Holder::Nothing) => println!("{}", no_host(false)),
|
||||
Ok(other) => println!("{}", held_words(other)),
|
||||
Err(why) => println!("The books server can't be asked: {why}"),
|
||||
},
|
||||
Ok(Some(HostMsg::Leases { ready, leases })) => {
|
||||
if ready {
|
||||
println!("The books are open.");
|
||||
|
||||
Reference in new issue
Block a user