fix(app): the virtual monitor's driver never changes under running services and its failures can be retried

- installing or removing the driver is refused unless the services are stopped (or failed), and
  a service start or restart (and a second driver task) is refused while one runs, so the
  monitor never vanishes under the bot browser's window
- a failed or declined install undoes what that attempt made (device, and the registry value and
  settings folder when the driver was not ours before); a declined install's
  0xE0000243 reads as cancelled; installed means ours with its monitor there
- devices are found among all root devices whatever their class: a device whose driver was
  declined drops out of the display class and was left behind
- uninstall keeps the registry value (what makes it ours) until the device and the driver
  package are gone (a package not in the store, 1168, counts as gone), so a failed removal can
  be retried
- checked on the win-ci VM: a declined install leaves nothing, the next install gives one working
  device, the uninstall leaves nothing

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
javis-bot
2026-11-14 02:55:01 +09:00
parent 1bd7defbb4
commit cfab18a47e
4 changed files with 201 additions and 52 deletions

View File

@@ -23,6 +23,8 @@ pub struct AdminApi {
installer: Mutex<Option<Arc<crate::install::Installer>>>,
syncer: Mutex<Option<Arc<crate::channel_sync::Syncer>>>,
hub: Arc<LogHub>,
/// The virtual monitor's driver is being installed or removed: no service start meanwhile.
driver_busy: Mutex<bool>,
}
fn arg<'a>(args: &'a Value, key: &str) -> Result<&'a Value, String> {
@@ -51,7 +53,7 @@ impl AdminApi {
hub.set_secrets(Secrets::new(&d.secrets_file()).values());
}
Self { location_file: location_file.to_path_buf(), dir: Mutex::new(dir), controller: Mutex::new(None), installer: Mutex::new(None),
syncer: Mutex::new(None), hub }
syncer: Mutex::new(None), hub, driver_busy: Mutex::new(false) }
}
fn dir(&self) -> Result<DataDir, String> {
@@ -100,6 +102,33 @@ impl AdminApi {
Arc::new(crate::channel_sync::DiscordRest::new(dir.secrets_file())), dir.settings_file())).clone())
}
/// Start the services in the background, unless the virtual monitor's driver is being installed
/// or removed (its monitor would vanish under the bot browser, or appear half made).
fn start_services(&self, c: &Arc<Controller>) -> Result<Value, String> {
let busy = self.driver_busy.lock().unwrap();
if *busy {
return Err("가상 모니터 드라이버 작업이 끝난 뒤에 시작해 주세요".into());
}
c.start_in_background().map(|_| json!({"ok": true}))
}
/// Mark a driver install or removal as running; only while the services are stopped (a failed
/// run has stopped the bot browser already) and no other driver task runs.
fn begin_driver_task(&self) -> Result<(), String> {
let mut busy = self.driver_busy.lock().unwrap();
if *busy {
return Err("가상 모니터 드라이버 작업이 이미 진행 중입니다".into());
}
let controller = self.controller.lock().unwrap().clone();
if let Some(state) = controller.map(|c| c.state()) {
if !matches!(state, State::Stopped | State::Failed) {
return Err("서비스를 정지한 뒤에 가상 모니터 드라이버를 설치하거나 제거해 주세요".into());
}
}
*busy = true;
Ok(())
}
fn controller(&self) -> Result<Arc<Controller>, String> {
let dir = self.dir()?;
let mut c = self.controller.lock().unwrap();
@@ -286,7 +315,10 @@ impl AdminApi {
"display_driver_status" => Ok(json!(crate::vdisplay::status())),
"display_driver_install" | "display_driver_uninstall" => {
let action = if command == "display_driver_install" { "install" } else { "uninstall" };
if let Err(e) = crate::vdisplay::run_elevated(action).await {
self.begin_driver_task()?;
let done = crate::vdisplay::run_elevated(action).await;
*self.driver_busy.lock().unwrap() = false;
if let Err(e) = done {
crate::warn_log!("admin", "Warning: display driver {action} failed: {e}");
return Err(e);
}
@@ -308,14 +340,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" => self.controller()?.start_in_background().map(|_| json!({"ok": true})),
"service_start" => self.start_services(&self.controller()?),
"service_restart" => {
let c = self.controller()?;
if c.state() != State::Running {
return Err(format!("cannot restart while {:?}", c.state()).to_lowercase());
}
c.stop().await?;
c.start_in_background().map(|_| json!({"ok": true}))
self.start_services(&c)
}
"service_stop" => self.controller()?.stop().await.map(|_| json!({"ok": true})),
"dashboard" => Ok(self.controller()?.snapshot().await),
@@ -355,7 +387,7 @@ mod tests {
let base = temp_dir(tag);
let loc = base.join("cfg").join("location.json");
(AdminApi { location_file: loc, dir: Mutex::new(None), controller: Mutex::new(None), installer: Mutex::new(None), syncer: Mutex::new(None),
hub: LogHub::new(None) },
hub: LogHub::new(None), driver_busy: Mutex::new(false) },
base)
}
@@ -572,6 +604,33 @@ mod tests {
assert_eq!(std::fs::read_to_string(&file).unwrap(), before);
}
#[tokio::test]
async fn the_display_driver_and_the_services_never_change_at_the_same_time() {
let (a, base) = api("api5");
a.call("folder_choose", json!({"path": base.join("d").to_string_lossy()})).await.unwrap();
// While the services start (or run), the driver is neither installed nor removed.
a.call("service_start", json!({})).await.unwrap();
for cmd in ["display_driver_install", "display_driver_uninstall"] {
assert!(a.call(cmd, json!({})).await.unwrap_err().contains("정지"), "{cmd}");
}
a.call("service_stop", json!({})).await.unwrap();
for _ in 0..200 {
if a.call("dashboard", json!({})).await.unwrap()["state"] == json!("stopped") { break; }
tokio::time::sleep(std::time::Duration::from_millis(10)).await;
}
// While a driver task runs, the services do not start and no second task begins.
a.begin_driver_task().unwrap();
assert!(a.call("service_start", json!({})).await.unwrap_err().contains("드라이버"));
assert!(a.begin_driver_task().is_err());
assert_eq!(a.call("dashboard", json!({})).await.unwrap()["state"], json!("stopped"));
// A finished task (here off Windows it fails at once) frees both again.
*a.driver_busy.lock().unwrap() = false;
assert!(a.call("display_driver_install", json!({})).await.is_err());
assert!(!*a.driver_busy.lock().unwrap(), "a failed task does not stay running");
a.call("service_start", json!({})).await.unwrap();
a.call("service_stop", json!({})).await.unwrap();
}
#[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");