mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-12-04 05:00:05 +08:00
refactor(backend): share single-owner drivers for background tasks (#8759)
* docs(backend): define background task ownership * refactor(backend): give background operations single task owners * test(backend): remove actor migration regression cases * docs(changelog): drop backend actor migration entry * refactor(backend): extract workspace task drivers * fix(timer): omit profile identifier from panic log * docs(backend): define workspace crate boundaries * refactor(lightweight): use the task countdown driver * refactor(lightweight): name countdown callbacks * refactor(task): simplify driver startup and coalescing requests * refactor(core): bind service handoff to the sidecar generation * refactor(tun): coalesce background run state reconciliation * refactor(core): own sidecar exit resources in one session * refactor(core): remove automatic Windows service handoff * refactor(task): simplify coalescing job signals * refactor(runstate): keep operation status with service state * refactor(dns): coalesce background override synchronization * refactor(sysproxy): own the local guard under its operation lock * fix(tray): read menu settings at publication * fix(dns): retain script lock across caller cancellation * refactor(profiles): own selected node restoration in one task * refactor(timer): own subscription schedules in the scheduler * test(timer): remove the added application test * fix(service): renew provider sync retry budget on refresh
This commit is contained in:
@@ -93,9 +93,7 @@ impl RunStateEnv for RealEnv {
|
||||
|
||||
fn publish(&self, state: &RunState) {
|
||||
// Reconciliation must run before the app handle exists and must not block this transition.
|
||||
crate::process::AsyncHandler::spawn(|| async {
|
||||
crate::feat::reconcile_tun_availability().await;
|
||||
});
|
||||
crate::feat::request_tun_reconciliation();
|
||||
|
||||
let Some(app_handle) = crate::APP_HANDLE.get() else {
|
||||
return;
|
||||
|
||||
@@ -9,10 +9,7 @@ mod owner;
|
||||
mod probe;
|
||||
|
||||
use std::{
|
||||
sync::{
|
||||
Arc, LazyLock,
|
||||
atomic::{AtomicBool, Ordering},
|
||||
},
|
||||
sync::{Arc, LazyLock},
|
||||
time::Duration,
|
||||
};
|
||||
|
||||
@@ -57,6 +54,7 @@ impl std::fmt::Display for ReadyWaitError {
|
||||
#[derive(Debug, Default)]
|
||||
struct VersionedService {
|
||||
service: StoredService,
|
||||
operation_running: bool,
|
||||
generation: u64,
|
||||
observation: u64,
|
||||
}
|
||||
@@ -79,7 +77,6 @@ pub struct RunStateStore<E: RunStateEnv> {
|
||||
env: E,
|
||||
service: Mutex<VersionedService>,
|
||||
mode: ArcSwap<RunningMode>,
|
||||
operation_running: AtomicBool,
|
||||
operation_done: Notify,
|
||||
}
|
||||
|
||||
@@ -89,7 +86,6 @@ impl<E: RunStateEnv> RunStateStore<E> {
|
||||
env,
|
||||
service: Mutex::new(VersionedService::default()),
|
||||
mode: ArcSwap::new(Arc::new(RunningMode::NotRunning)),
|
||||
operation_running: AtomicBool::new(false),
|
||||
operation_done: Notify::new(),
|
||||
}
|
||||
}
|
||||
@@ -97,8 +93,18 @@ impl<E: RunStateEnv> RunStateStore<E> {
|
||||
/// The last known Run State, without waiting and without probing.
|
||||
/// It reports when a privileged operation may make the snapshot stale.
|
||||
pub fn state(&self) -> RunState {
|
||||
let service = self.service.lock().service.clone();
|
||||
self.snapshot(service)
|
||||
let (service, op_in_flight) = {
|
||||
let state = self.service.lock();
|
||||
(state.service.clone(), state.operation_running)
|
||||
};
|
||||
RunState {
|
||||
health: service.health,
|
||||
pending: service.pending,
|
||||
sidecar_allowed: service.sidecar_allowed,
|
||||
mode: *self.mode.load().as_ref(),
|
||||
is_admin: self.env.is_elevated(),
|
||||
op_in_flight,
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns one snapshot taken after any operation visible in that snapshot has settled.
|
||||
@@ -309,7 +315,7 @@ impl<E: RunStateEnv> RunStateStore<E> {
|
||||
/// Accepts Sidecar only while the Service is unusable and no conflicting operation is active.
|
||||
pub fn allow_sidecar_for_session(&self) -> Result<()> {
|
||||
let mut state = self.service.lock();
|
||||
if self.operation_running.load(Ordering::Acquire) {
|
||||
if state.operation_running {
|
||||
bail!("service operation already running");
|
||||
}
|
||||
let permitted = match (state.service.pending, state.service.sidecar_allowed) {
|
||||
@@ -333,7 +339,7 @@ impl<E: RunStateEnv> RunStateStore<E> {
|
||||
#[cfg(target_os = "windows")]
|
||||
pub fn allow_sidecar_after_service_refusal(&self, reason: String) -> Result<()> {
|
||||
let mut state = self.service.lock();
|
||||
if self.operation_running.load(Ordering::Acquire) || state.service.pending.is_some() {
|
||||
if state.operation_running || state.service.pending.is_some() {
|
||||
bail!("cannot fall back while a service operation is pending");
|
||||
}
|
||||
// Publish the refusal and allowance together so it never asks the UI for a repair.
|
||||
@@ -348,7 +354,7 @@ impl<E: RunStateEnv> RunStateStore<E> {
|
||||
/// Requests installation only for a confirmed absent Service.
|
||||
pub fn require_install_for_session(&self) -> Result<()> {
|
||||
let mut state = self.service.lock();
|
||||
if self.operation_running.load(Ordering::Acquire) {
|
||||
if state.operation_running {
|
||||
bail!("service operation already running");
|
||||
}
|
||||
if state.service.pending.is_none()
|
||||
@@ -427,9 +433,10 @@ impl<E: RunStateEnv> RunStateStore<E> {
|
||||
/// Only one privileged operation may be in flight; [`Self::settled`] waits on this.
|
||||
pub fn begin_operation(&self) -> Result<OperationGuard<'_, E>> {
|
||||
let mut state = self.service.lock();
|
||||
if self.operation_running.swap(true, Ordering::AcqRel) {
|
||||
if state.operation_running {
|
||||
bail!("service operation already running");
|
||||
}
|
||||
state.operation_running = true;
|
||||
state.bump();
|
||||
drop(state);
|
||||
self.announce();
|
||||
@@ -438,7 +445,7 @@ impl<E: RunStateEnv> RunStateStore<E> {
|
||||
|
||||
#[cfg(test)]
|
||||
pub fn operation_in_flight(&self) -> bool {
|
||||
self.operation_running.load(Ordering::Acquire)
|
||||
self.service.lock().operation_running
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -455,17 +462,6 @@ impl<E: RunStateEnv> RunStateStore<E> {
|
||||
fn announce(&self) {
|
||||
self.env.publish(&self.state());
|
||||
}
|
||||
|
||||
fn snapshot(&self, service: StoredService) -> RunState {
|
||||
RunState {
|
||||
health: service.health,
|
||||
pending: service.pending,
|
||||
sidecar_allowed: service.sidecar_allowed,
|
||||
mode: *self.mode.load().as_ref(),
|
||||
is_admin: self.env.is_elevated(),
|
||||
op_in_flight: self.operation_running.load(Ordering::Acquire),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Releases the privileged-operation slot and wakes everything waiting in [`RunStateStore::settled`].
|
||||
@@ -476,7 +472,7 @@ pub struct OperationGuard<'a, E: RunStateEnv> {
|
||||
|
||||
impl<E: RunStateEnv> Drop for OperationGuard<'_, E> {
|
||||
fn drop(&mut self) {
|
||||
self.store.operation_running.store(false, Ordering::Release);
|
||||
self.store.service.lock().operation_running = false;
|
||||
self.store.operation_done.notify_waiters();
|
||||
self.store.announce();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user