fix(app): arm the LLM cancel when a start is accepted; confirm stops by refusal

A stop right after a background start set the LLM's cancel flag, but the
start task then cleared it in Llm::start and went on to download and load
the model. The cancel is now armed in the controller's begin_start, under
its lock (Host::prepare_start -> Llm::arm), and Llm::start never clears
it; a start that finds it set returns before running anything.

wait_gone counted a timeout, a 503 (a server still loading) or an
unreadable answer as stopped, so a failed `launch --stop` could still mark
a live engine off. Only a refused connection confirms the stop now; the
rest keeps checking until the deadline and then reports failed.

Tests: a stop right after a background start runs no launch (and the next
start is not cancelled); a failed stop with a silent or 503 server leaves
the LLM and the services failed through the controller. Fixed-port tests
use an unused port.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
javis-bot
2026-11-12 17:55:40 +09:00
parent 41026a1eff
commit 81ba5e3521
3 changed files with 155 additions and 26 deletions

View File

@@ -38,6 +38,9 @@ pub trait Host: Send + Sync + 'static {
async fn start(&self) -> Result<(Arc<dyn Running>, mpsc::UnboundedReceiver<CoreEvent>), String>;
/// Stored secrets, masked in error messages.
fn secrets(&self) -> Vec<String>;
/// Called when a start is accepted (state `starting`, under the controller's lock): a fresh
/// cancel, so a stop from now on cancels this start and an earlier stop does not.
fn prepare_start(&self) {}
/// Stop a start in progress (the LLM's first start downloads the model); `start` then returns
/// `Err("cancelled")` if it had not finished.
fn cancel_start(&self) {}
@@ -100,6 +103,7 @@ impl Controller {
i.state = State::Starting;
i.error = None;
i.stop_asked = false;
self.host.prepare_start();
Ok(())
}
@@ -282,6 +286,10 @@ impl Host for ServicesHost {
crate::secrets::Secrets::new(&self.data_dir.join("config").join("secrets.dat")).values()
}
fn prepare_start(&self) {
self.llm.arm();
}
fn cancel_start(&self) {
self.llm.cancel();
}
@@ -384,7 +392,6 @@ mod tests {
impl Host for SlowHost {
async fn start(&self) -> Result<(Arc<dyn Running>, mpsc::UnboundedReceiver<CoreEvent>), String> {
self.counts.starts.fetch_add(1, Ordering::SeqCst);
self.cancelled.store(false, Ordering::SeqCst); // as the LLM does: a cancel ends one start
for _ in 0..50 {
if self.honours_cancel && self.cancelled.load(Ordering::SeqCst) {
return Err("cancelled".into());
@@ -396,6 +403,9 @@ mod tests {
fn secrets(&self) -> Vec<String> {
vec![]
}
fn prepare_start(&self) {
self.cancelled.store(false, Ordering::SeqCst); // as the LLM does: armed when a start is accepted
}
fn cancel_start(&self) {
self.cancelled.store(true, Ordering::SeqCst);
}