fix(core): serialize runtime config updates

This commit is contained in:
Tunglies
2026-05-28 17:41:39 +08:00
parent 43843f1eca
commit 08fd2ce68b
3 changed files with 48 additions and 8 deletions
+27 -2
View File
@@ -10,6 +10,7 @@ use crate::{
};
use anyhow::{Result, anyhow};
use clash_verge_logging::{Type, logging};
use scopeguard::defer;
use smartstring::alias::String;
use std::{collections::HashSet, path::PathBuf, time::Instant};
use tauri_plugin_mihomo::Error as MihomoError;
@@ -45,6 +46,14 @@ impl CoreManager {
});
}
if !self.try_start_config_update() {
logging!(info, Type::Core, "Configuration update is already running");
return Ok(ValidationOutcome::Busy);
}
defer! {
self.finish_config_update();
}
if !force && !self.should_update_config() {
logging!(debug, Type::Core, "Skipping config update due to debounce");
return Ok(ValidationOutcome::Skipped {
@@ -89,10 +98,26 @@ impl CoreManager {
return Ok(ValidationOutcome::invalid_from_message(message));
}
self.apply_generate_config().await
self.apply_generate_config_inner().await
}
pub async fn apply_generate_config(&self) -> Result<ValidationOutcome> {
pub(crate) async fn update_runtime_config<F>(&self, f: F) -> Result<ValidationOutcome>
where
F: FnOnce(&mut IRuntime),
{
if !self.try_start_config_update() {
logging!(info, Type::Core, "Configuration update is already running");
return Ok(ValidationOutcome::Busy);
}
defer! {
self.finish_config_update();
}
Config::runtime().await.edit_draft(f);
self.apply_generate_config_inner().await
}
async fn apply_generate_config_inner(&self) -> Result<ValidationOutcome> {
match CoreConfigValidator::global().validate_config_outcome().await {
Ok(outcome) if outcome.is_valid() => {
let run_path = Config::generate_file(ConfigType::Run).await?;