From 13c4fb3f7df230f49a05480d3574da2954444e11 Mon Sep 17 00:00:00 2001 From: 21pages Date: Thu, 12 Feb 2026 11:46:09 +0800 Subject: [PATCH] fix: invalidate key_pair cache instead of setting directly Signed-off-by: 21pages --- src/config.rs | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/src/config.rs b/src/config.rs index 20230110e..cfecf6f33 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1356,14 +1356,31 @@ impl Config { } *lock = cfg; lock.store(); - // Currently only tested on macOS, so this change is limited to macOS for safety. + // Drop CONFIG lock before acquiring KEY_PAIR lock to avoid potential deadlock. #[cfg(target_os = "macos")] - { - *KEY_PAIR.lock().unwrap() = Some(lock.key_pair.clone()); - } + let new_key_pair = lock.key_pair.clone(); + drop(lock); + #[cfg(target_os = "macos")] + Self::invalidate_key_pair_cache_if_changed(&new_key_pair); true } + /// Invalidate KEY_PAIR cache if it differs from the new key_pair. + /// Use None to invalidate the cache instead of Some(key_pair). + /// If we use Some with an empty key_pair, get_key_pair() would always return + /// the empty key_pair from cache without regenerating. + /// By clearing the cache, get_key_pair() will reload and regenerate if needed. + #[cfg(target_os = "macos")] + fn invalidate_key_pair_cache_if_changed(new_key_pair: &KeyPair) { + let mut key_pair_cache = KEY_PAIR.lock().unwrap(); + if let Some(cached) = key_pair_cache.as_ref() { + if cached != new_key_pair { + *key_pair_cache = None; + log::info!("key pair cache invalidated"); + } + } + } + fn with_extension(path: PathBuf) -> PathBuf { let ext = path.extension(); if let Some(ext) = ext {