Skip to content

feat(desktop): attach to or start the local serve, restart it with backoff - #1097

Merged
ajianaz merged 1 commit into
developfrom
feat/desktop-supervisor-lifecycle
Nov 21, 2026
Merged

ajianaz merged 1 commit into
developfrom
feat/desktop-supervisor-lifecycle

Conversation

@ajianaz

@ajianaz ajianaz commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

What

The lifecycle of the local tole serve on top of the building blocks of #1096: Supervisor::launch attaches to a serve that answers on the private socket or starts one, a monitor thread notices a serve that stopped and starts it again with a growing wait, and quit_completely stops it. Nothing in a window calls it yet; the state it publishes is the ServeState of #1095.

Why

Part of #1029 (owner decisions D3, D4 and D8 and the answers of 2026-11-21: closing the window leaves the serve running, the restart numbers, no keychain yet). The desktop is a thin client and has to own the answer to "is there a serve I can talk to, and with what credential".

Changes

  • supervisor/engine.rs: Engine::ensure decides, in order: a serve answers and speaks protocol 1 → attach (using the credential file if the token is accepted); another protocol version, a not-tole listener, a socket directory that is not owner-only → refused with a plain-language ServeFailure; nothing answers → take the launch lock, check again, start one with a fresh per-launch token. A second desktop process attaches and never starts a second serve: the launch lock decides, and the serve's own flock refuses the rest.
  • supervisor/recover.rs: tick looks at the serve (a child through try_wait, an attached one through the socket). A serve that stopped by itself becomes Restarting and is started again once the Backoff has passed; a steady run of 60 s resets the count; after 6 starts that did not hold it is given up on with the last lines of its standard error. A serve that was not started here is not treated as a crash loop.
  • supervisor/stop.rs: stop_serve asks with SIGTERM, waits until the serve's lock is free (the proof that it is gone, however it ended), then SIGKILL, and only then removes the socket file the signal left and the credential file. StopError says why it could not (unknown process, signal refused, still running).
  • supervisor/hub.rs: the shared state, the observer callback and the credential, so a Client can be built from outside only while there is a serve to talk to.
  • supervisor/mod.rs: the public Supervisor (launch, launch_observed, state, client, retry, restart_serve, quit_completely, close_window). close_window honours CloseBehavior: by default it leaves the serve running.
  • supervisor/probe.rs: Refused is split into Insecure (the client refused a socket that is not the owner's alone) and Invalid (a path that cannot be a socket address), so the failure text can tell the user which.
  • supervisor/fake.rs and tests.rs: a fake tole and 13 tests of the engine. process.rs takes the shared spawn guard in its test.
  • CHANGELOG.md.

Known edge, stated rather than hidden: when the serve was not started by this process, stop_serve signals the pid of the socket's owner (SO_PEERCRED / LOCAL_PEERPID), and only while the serve's lock is held. If a different process replaced the socket file while the real serve still holds the lock, the signal would go to that process. The socket lives in the private 0700 directory, so only the same user can do that. A later change can take the pid from the holder of the lock instead; it is not done here.

Callers checked with grep: everything new is inside supervisor/ and has no caller outside it; the one edited existing file is probe.rs (its Refused callers are all in this module and are updated). No pub item of a published crate changes.

Testing

  • cargo test -p tole-desktop: 39 tests pass. The 13 new engine tests cover: attaching to a running serve without starting a second; a second launch attaching to the same serve; another protocol version refused and never talked to; a serve that does not take the credential is not used; a socket directory that is not owner-only refused and left alone; a directory widened while a serve runs stops the desktop from sending anything; no tole binary is a clear state; a start that fails says why and is not retried on launch; a serve that dies restarts with growing waits and is then given up on; a serve that comes back on its own is attached to; closing the window leaves the serve running by default; quit_completely with nothing running cleans up and reports stopped; the desktop never signals itself when the serve is in its own process.
  • Not covered by a test here: the SIGTERM then SIGKILL path against a real serve, and the reset of the restart count after a 60 s steady run (a unit test of Backoff in feat(desktop): add the building blocks of the local serve supervisor #1096 covers the count itself). The end-to-end test against the real tole binary is the next PR of this series and covers the stop.
  • cargo fmt --all --check, cargo clippy --workspace --all-targets -- -D warnings, cargo clippy -p tole-cli --no-default-features --all-targets -- -D warnings, cargo clippy -p tole-desktop --features gpui --all-targets -- -D warnings, bash scripts/desktop-check.sh all: pass.
  • Size: about 1,360 changed lines in 10 files, of which about 560 are the fake and the tests.
  • Not verified: macOS (the peer-pid lookup and flock behaviour were never run there; the Desktop macOS job builds and runs the unit tests only), Windows (not compiled), running as an unprivileged user. scripts/review-local.sh not run (no LLM key); the CI Cora Review is the gate.

Part of #1029.

…ckoff

Supervisor::launch attaches to a tole serve that answers on the private socket
and speaks protocol version 1, or starts one with a per-launch credential on an
inherited pipe. A second launch attaches and never starts a second serve (a
launch lock decides, the serve's flock refuses the rest). A serve with another
protocol version or a socket directory that is not owner-only is refused. A
serve that stops by itself is started again with a growing wait and given up
on after too many stops. Closing the window leaves the serve running;
quit_completely stops it and waits until its lock proves it is gone.

Part of #1029.
@github-actions

Copy link
Copy Markdown

🔍 Cora AI Code Review

✅ No issues found. Code looks good!


Review powered by cora-code · BYOK · MIT

@ajianaz
ajianaz merged commit bfa690f into develop Nov 21, 2026
32 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant