Skip to content

S06-11 - Reconnect, Loss and Recovery

Purpose: What happens when the host-guest link drops, when a session is lost, and how a lost session’s work is recovered or discarded.

When the host-guest link drops:

wb-hostd waits 0.1 s before the first try, and doubles the wait after each failed one, up to 5 s. It opens at most 20 connections to one VM in a minute (SEC13-bounded-resourcesSEC13-bounded-resources: Bounded resourcesCPU, memory and disk of each VM are capped.).

A guest that answers the hello and then stops answering also counts as a drop. wb-hostd drops the link with guest-time-unanswered when a time call (S06-06-devices-and-warm-startS06-06-devices-and-warm-start: Devices, Warm Start and TimeA VM's devices and resources, saving and restoring its state, and how the guest clock follows the host.) is still unanswered at the link’s call deadline, or fails because the connection failed. The call’s status code is guest input: the host can’t tell a failed transport from an Unavailable that the guest sent, and both lead to the same reconnect, which the waits and the cap above bound. So the link is never shown as up for more than a minute plus that deadline after the guest’s last answer.

After it connects, wb-guestd lists what wb-hostd hasn’t acknowledged:

The list is guest input (SEC11-root-gains-nothingSEC11-root-gains-nothing: Guest root is not a privilegeFull control of the guest grants no control over enforcement, secrets, or policy.). wb-hostd refuses the whole list when:

  • it has more than 64 entries, held sessions and reports counted together;
  • it has more cleanup records than project users in the VM;
  • it names one ID twice, as a held session and as a report, or twice as either;
  • it has two records with one kill ID.

It logs the refusal and closes the connection, which counts as a failed try.

wb-hostd runs at most 64 sessions in one VM.

For each entry, wb-hostd first checks the session ID format of S05-04-namingS05-04-naming: NamingHow wb identifies a project, a session and its branch, and how projects are registered, checked, moved and removed.. Then it looks for a session of this VM and this VM generation only, and answers:

A lost session of this VM gets “finish” from recovery (“Recovery” below).

On “finish”, wb-guestd:

  1. hangs up as in S06-09-session-lifecycleS06-09-session-lifecycle: Session LifecycleA session's states from wb claude to its summary, how it starts and ends, its deadlines, and when its VM may be saved., “Closing the terminal ends the session”, if claude still runs;
  2. kills every process left in the session, a stopped WIP commit or push included;
  3. removes the stale index.lock that a killed commit left (S06-09-session-lifecycleS06-09-session-lifecycle: Session LifecycleA session's states from wb claude to its summary, how it starts and ends, its deadlines, and when its VM may be saved., “As the project user”);
  4. commits and pushes WIP again.

“end” goes to these entries:

  • a starting session, whose start has failed;
  • a session of another VM;
  • a running, paused or ending session of another VM generation;
  • an ended session;
  • a malformed ID;
  • an ID that wb-hostd has no record of.

On “end”, wb-guestd kills every process left in that session, a stopped WIP commit or push included. It doesn’t commit or push. It deletes the entry’s report and recorded POSIX session ID. For a session whose start failed, or that was discarded, it also removes the worktree and its history link. wb-hostd logs each such entry with the rule.

A cleanup record gets “acknowledged” when it passes the checks of S06-10-leftover-processesS06-10-leftover-processes: Leftover ProcessesThe results of the kill of a project user, how wb-hostd accepts them, and what it does when processes are left., or was already acknowledged. Otherwise it gets “end”, which deletes it.

Every entry gets one of these answers, so every report and record is cleared.

wb-guestd resumes a session only on “resume”.

A session of this VM that wb-hostd has as running, paused or ending is lost when the list doesn’t hold it, as a held session or as a report. A starting session isn’t in that rule, because a link drop already failed its start.

A false list can only stop, keep, or end sessions in that guest.

wb doesn’t print anything while a session is paused.

A session that is paused for 30 seconds of host time awake is lost. Time the host spends asleep doesn’t count.

A restore reconnects in about 0.2 s (X18-vsock-handoffX18-vsock-handoff: Host-guest socket and descriptor hand-offCan wb-guestd, written in Go, open AFVSOCK sockets in a macOS guest?). A wb-guestd restart loses the session anyway (“Lost” below). So the limit only has to outlast a slow reconnect.

A session is lost:

  • when wb-vmd reports its VM stopped;
  • when its wb-guestd exits. The reconnected wb-guestd doesn’t list it, and a new wb-guestd process never takes over an earlier one’s sessions;
  • when wb-hostd exits, which stops every VM;
  • at the pause limit.

A wb-guestd exit closes the PTYs it holds. So claude gets a hangup from the guest kernel.

wb prints a wraith box: line that names the session, and says its WIP returns at the next connection to the VM. Then it exits with 255 (S05-cliS05-cli: Command Line InterfaceSpecify wb, the single user-facing command.).

When wb-hostd starts, it marks as lost every session it has as starting, running, paused or ending.

A lost session counts as ended for these:

A lost session still counts as a session for the lock and the kill of a project’s last session (S13-01-process-hardeningS13-01-process-hardening: Layer 1: Process Hardening (v1)How wb-guestd hardens every process it starts for a project user, and how it stops and kills a project user's processes.). So the project user is locked only after its lost sessions have ended. They end by recovery, wb discard, wb project rm or the deadline.

A lost session’s worktree stays on the data disk.

At the next connection to a wb-guestd in that VM, wb-hostd sends “finish” for each lost session of the VM that it has a record of, whatever else the list holds. That connection is after a restart, or at the next VM start. Recovery skips a lost session whose report the list holds. “acknowledged” ends that one instead (“Reconnect”).

On “finish”, wb-guestd kills what is left of the session’s processes. Then it commits and pushes WIP, as at a normal end.

During recovery, wb-hostd:

The session is then ended, with “lost” as its exit status in wb sessions. When this was the project’s last session in the VM, the project user is then locked and its processes killed (S13-01-process-hardeningS13-01-process-hardening: Layer 1: Process Hardening (v1)How wb-guestd hardens every process it starts for a project user, and how it stops and kills a project user's processes.).

A lost session stays live for export.git until it ends (S08-02-export-repositoryS08-02-export-repository: Export RepositoryWhich refs and objects the guest can fetch from export.git, and how wb, its one writer, keeps it safe to run git in.). So its WIP push still has its base.

wb discard on a lost session ends it with no recovery push. So does wb project rm, for each lost session of the project. wb-hostd:

  1. takes the project’s landing lock (S08-05-push-limitsS08-05-push-limits: Push LimitsCleanup after a push that failed, the size cap of landing.git, and the time and memory bounds of the git gateway., “Landing lock”);
  2. records the session as ended and discarded;
  3. only then deletes its refs.

A recovery push that already holds the lock finishes first. Its refs are deleted after it. A later one is refused, because the session is no longer live.

At the next connection to the VM, wb-guestd gets “end” for the session. It removes the worktree and its history link.

Status: Draft