fix: serialize concurrent configuration updates

This commit is contained in:
wonfen
2026-08-17 19:19:54 +08:00
parent 95ae5e4e71
commit 906ec5e63a
5 changed files with 55 additions and 91 deletions
+16 -22
View File
@@ -8,6 +8,7 @@ use bitflags::bitflags;
use clash_verge_draft::{DraftTransaction, SharedDraft};
use clash_verge_logging::{Type, logging, logging_error};
use serde_yaml_ng::Mapping;
use tokio::sync::MutexGuard;
/// Patch Clash configuration
pub async fn patch_clash(patch: &Mapping) -> Result<()> {
@@ -265,19 +266,9 @@ async fn process_terminated_flags(update_flags: UpdateFlags, patch: &IVerge) ->
Ok(())
}
/// Apply a patch to the app's configuration, then re-check anything it can invalidate.
/// Apply a patch, then reconcile TUN when its setting changes.
///
/// Today that is TUN, which is a question about the *setting* rather than about the Run State:
/// no Run State transition follows a TUN patch, so the reconciliation that reacts to those
/// would never see it. Switching TUN on from the global hotkey is the case that reaches here —
/// unlike the tray item and the settings switch it is not gated on availability, and the flags
/// the patch raises are answered by a Core reload that changes no Run State at all. Left alone
/// the setting stays on and every surface reports TUN as enabled while nothing carries its
/// traffic.
///
/// The reconciliation writes configuration of its own, so it goes through
/// [`apply_verge_patch`] rather than back through here. That makes the absence of a cycle a
/// property of the call graph rather than of a runtime early-return.
/// TUN patches do not always produce a Run State transition, so reconciliation is explicit.
pub async fn patch_verge(patch: &IVerge, not_save_file: bool) -> Result<()> {
apply_verge_patch(patch, not_save_file).await?;
if patch.enable_tun_mode.is_some() {
@@ -286,18 +277,21 @@ pub async fn patch_verge(patch: &IVerge, not_save_file: bool) -> Result<()> {
Ok(())
}
/// Apply a patch and nothing else. For callers that are themselves a reconciliation.
/// Apply a patch without post-update reconciliation.
pub(super) async fn apply_verge_patch(patch: &IVerge, not_save_file: bool) -> Result<()> {
let config_write = Config::lock_config_write().await;
apply_verge_patch_locked(&config_write, patch, not_save_file).await
}
/// Apply a patch with the shared configuration write lock already held.
/// Callers must pass the guard returned by [`Config::lock_config_write`].
pub(super) async fn apply_verge_patch_locked(
_config_write: &MutexGuard<'_, ()>,
patch: &IVerge,
not_save_file: bool,
) -> Result<()> {
let verge = Config::verge().await;
// Applying the flags can fail, and until now that `?` returned straight out of here past
// an `if let Err(..) { discard() }` the compiler could never reach — leaving the failed
// edit sitting in the draft, where every later reader saw a value that was never applied
// and never written to disk.
//
// Claiming the layer is what makes the rollback safe. This function holds its draft across
// `process_terminated_flags`, which restarts the Core and can take seconds; a second patch
// arriving in that window used to share the one draft slot, and whichever of the two failed
// first discarded the other's staged edit too.
// Hold the claim across side effects so concurrent transactions cannot share this draft.
let transaction = DraftTransaction::begin(vec![&verge])?;
verge.edit_draft(|d| d.patch_config(patch));