fix(app): settle the start state before answering and report failed stops
service_start now moves the controller to starting before it returns (Controller::start_in_background), so a stop right after it cancels the start and a second start is refused instead of the start running late. service_restart stops, then starts the same way. Stopping Strata now confirms the server is gone: `launch --stop` is ended after 2 minutes with everything it started, then the port must stop listing a model. A server still answering makes the LLM failed with the reason, and Services::stop / Running::stop return it, so the controller shows failed instead of stopped (also for a cancelled start that could not clear the server). A stop command that fails while nothing answers counts as stopped. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -195,17 +195,14 @@ impl AdminApi {
|
||||
}
|
||||
// Starting can take long (the LLM's first start downloads the model), so it runs in the
|
||||
// background and the dashboard shows how it goes; stop also cancels a start.
|
||||
"service_start" | "service_restart" => {
|
||||
"service_start" => self.controller()?.start_in_background().map(|_| json!({"ok": true})),
|
||||
"service_restart" => {
|
||||
let c = self.controller()?;
|
||||
let restart = command == "service_restart";
|
||||
let ok = if restart { c.state() == State::Running } else { matches!(c.state(), State::Stopped | State::Failed) };
|
||||
if !ok {
|
||||
return Err(format!("cannot {} while {:?}", if restart { "restart" } else { "start" }, c.state()).to_lowercase());
|
||||
if c.state() != State::Running {
|
||||
return Err(format!("cannot restart while {:?}", c.state()).to_lowercase());
|
||||
}
|
||||
tokio::spawn(async move {
|
||||
let _ = if restart { c.restart().await } else { c.start().await };
|
||||
});
|
||||
Ok(json!({"ok": true}))
|
||||
c.stop().await?;
|
||||
c.start_in_background().map(|_| json!({"ok": true}))
|
||||
}
|
||||
"service_stop" => self.controller()?.stop().await.map(|_| json!({"ok": true})),
|
||||
"dashboard" => Ok(self.controller()?.snapshot().await),
|
||||
@@ -308,4 +305,20 @@ mod tests {
|
||||
assert_eq!(d["state"], json!("stopped"));
|
||||
assert!(a.call("service_stop", json!({})).await.is_err());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_start_is_starting_when_it_answers_so_a_stop_right_after_it_is_not_refused() {
|
||||
let (a, base) = api("api4");
|
||||
a.call("folder_choose", json!({"path": base.join("d").to_string_lossy()})).await.unwrap();
|
||||
a.call("service_start", json!({})).await.unwrap();
|
||||
assert!(a.call("service_start", json!({})).await.unwrap_err().contains("starting"));
|
||||
a.call("service_stop", json!({})).await.unwrap();
|
||||
let mut state = Value::Null;
|
||||
for _ in 0..200 {
|
||||
state = a.call("dashboard", json!({})).await.unwrap()["state"].clone();
|
||||
if state == json!("stopped") { break; }
|
||||
tokio::time::sleep(std::time::Duration::from_millis(10)).await;
|
||||
}
|
||||
assert_eq!(state, json!("stopped"), "the start was cancelled, not started late");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user