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:
swaitsandClaude Fable 5.2 committed 2026-11-21 12:12:28 -07:00
1 parent 6b95ce59ad
commit 2ff2913274
9 files changed
+598 -80

No files matched your search

+10 -3
View File
@@ -94,11 +94,13 @@ pub(crate) fn private_options() -> std::fs::OpenOptions {
/// Windows passes no descriptor to a child unless asked to, so there is
/// nothing to close (INVARIANT 2).
#[cfg(not(unix))]
fn close_stray_descriptors() {}
pub fn close_stray_descriptors() {}
/// Close every descriptor above 2 (INVARIANT 2).
/// Close every descriptor above 2 (INVARIANT 2). The host does it at its
/// start, and so does every books server (`wanderbooks server`), whoever
/// started it: it holds none of its starter's pipes or sockets.
#[cfg(unix)]
fn close_stray_descriptors() {
pub fn close_stray_descriptors() {
#[cfg(target_os = "linux")]
{
// SAFETY: close_range only closes descriptors; nothing is open
@@ -765,6 +767,11 @@ impl Host {
#[cfg(unix)]
command.arg0("wanderbooks");
command.arg("server");
// Said in its `run/server.info`: this host's, so that one found
// with no host is known to be an orphan finishing (opener.rs).
command
.arg("--started-by")
.arg(ledger_mcp::local::StartedBy::Host.flag());
if self.explicit_root {
command.arg("--root").arg(self.root.path());
}