From fe190081ebe23353f78a8ca0539c9e60fff81fb7 Mon Sep 17 00:00:00 2001 From: Jason Date: Mon, 24 Nov 2025 11:25:41 +0800 Subject: [PATCH] fix: resolve critical bugs in settings and import flow Fixed two critical issues: 1. **Blocking Issue - restart_app return type error** - Fixed compilation error where restart_app() didn't return a value - Used async spawn with 100ms delay to allow response before restart - Prevents "unreachable code" compiler error 2. **High Priority - Import SQL doesn't refresh AppSettings cache** - Added reload_settings() function to refresh in-memory settings cache - Integrated into import flow to ensure imported settings take effect - Prevents imported settings being overwritten by stale memory cache - Affects: language, config directories, auto-launch, custom endpoints Changes: - src/commands/settings.rs: Async delayed restart with proper return value - src/settings.rs: New reload_settings() to sync memory cache from DB - src/commands/import_export.rs: Call reload_settings() after SQL import Verified: cargo clippy --lib and pnpm typecheck both pass --- src-tauri/src/commands/import_export.rs | 5 +++++ src-tauri/src/commands/settings.rs | 7 ++++++- src-tauri/src/settings.rs | 9 +++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src-tauri/src/commands/import_export.rs b/src-tauri/src/commands/import_export.rs index fe2fde142..86ff4298f 100644 --- a/src-tauri/src/commands/import_export.rs +++ b/src-tauri/src/commands/import_export.rs @@ -48,6 +48,11 @@ pub async fn import_config_from_file( log::warn!("导入后同步 live 配置失败: {err}"); } + // 重新加载设置到内存缓存,确保导入的设置生效 + if let Err(err) = crate::settings::reload_settings() { + log::warn!("导入后重载设置失败: {err}"); + } + Ok::<_, AppError>(json!({ "success": true, "message": "SQL imported successfully", diff --git a/src-tauri/src/commands/settings.rs b/src-tauri/src/commands/settings.rs index 63f18e4f3..b10c12339 100644 --- a/src-tauri/src/commands/settings.rs +++ b/src-tauri/src/commands/settings.rs @@ -18,7 +18,12 @@ pub async fn save_settings(settings: crate::settings::AppSettings) -> Result Result { - app.restart(); + // 在后台延迟重启,让函数有时间返回响应 + tauri::async_runtime::spawn(async move { + tokio::time::sleep(tokio::time::Duration::from_millis(100)).await; + app.restart(); + }); + Ok(true) } /// 获取 app_config_dir 覆盖配置 (从 Store) diff --git a/src-tauri/src/settings.rs b/src-tauri/src/settings.rs index d4c8cea77..99d044e46 100644 --- a/src-tauri/src/settings.rs +++ b/src-tauri/src/settings.rs @@ -260,6 +260,15 @@ pub fn update_settings(mut new_settings: AppSettings) -> Result<(), AppError> { Ok(()) } +/// 从数据库重新加载设置到内存缓存 +/// 用于导入配置等场景,确保内存缓存与数据库同步 +pub fn reload_settings() -> Result<(), AppError> { + let fresh_settings = load_initial_settings(); + let mut guard = settings_store().write().expect("写入设置锁失败"); + *guard = fresh_settings; + Ok(()) +} + pub fn ensure_security_auth_selected_type(selected_type: &str) -> Result<(), AppError> { let mut settings = get_settings(); let current = settings