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:
Tunglies
2026-11-29 00:42:52 +00:00
committed by GitHub
parent 9724aa6d4d
commit 929bb6a0a1
37 changed files with 1671 additions and 1291 deletions
+26 -12
View File
@@ -8,7 +8,7 @@ use std::{
process::{Command, Stdio},
time::{Duration, Instant},
};
use tokio::sync::Mutex;
use tokio::sync::{Mutex, MutexGuard};
// Any address routed into TUN works, since dns-hijack answers it; the value only matters if left behind.
const PLACEHOLDER_DNS: &str = "114.114.114.114\n223.5.5.5\n1.1.1.1";
@@ -42,13 +42,17 @@ fn needs_public_dns(config: &Mapping) -> bool {
}
/// Sets the placeholder DNS while the running config needs it, and restores the system DNS otherwise.
#[allow(
clippy::significant_drop_tightening,
reason = "the DNS lock covers the state read and is transferred to the script worker"
)]
pub async fn sync_public_dns() {
use crate::{
config::Config,
core::{CoreManager, handle::Handle, manager::RunningMode},
};
let _guard = DNS_LOCK.lock().await;
let guard = DNS_LOCK.lock().await;
// Exit cleanup restores DNS on its own budget; a sync queued behind it must not undo that.
if Handle::global().is_exiting() {
return;
@@ -56,20 +60,19 @@ pub async fn sync_public_dns() {
let core_running = !matches!(*CoreManager::global().get_running_mode(), RunningMode::NotRunning);
let runtime = Config::runtime().await.latest_arc();
if core_running && runtime.config.as_ref().is_some_and(needs_public_dns) {
set_public_dns().await;
set_public_dns(guard).await;
} else {
let _ = unset_public_dns().await;
let _ = unset_public_dns(guard).await;
}
}
pub async fn restore_public_dns() -> anyhow::Result<()> {
let _guard = DNS_LOCK.lock().await;
unset_public_dns().await
unset_public_dns(DNS_LOCK.lock().await).await
}
async fn set_public_dns() {
async fn set_public_dns(guard: MutexGuard<'static, ()>) {
logging!(debug, Type::Config, "try to set system dns");
let result = run_dns_script("set_dns.sh", true).await;
let result = run_dns_script(DnsScript::Set, guard).await;
match result {
Ok(DnsScriptOutcome::Complete) => logging!(info, Type::Config, "set system dns successfully"),
Ok(DnsScriptOutcome::Deferred) => {
@@ -79,9 +82,9 @@ async fn set_public_dns() {
}
}
async fn unset_public_dns() -> anyhow::Result<()> {
async fn unset_public_dns(guard: MutexGuard<'static, ()>) -> anyhow::Result<()> {
logging!(debug, Type::Config, "try to unset system dns");
let result = run_dns_script("unset_dns.sh", false).await.map(|_| ());
let result = run_dns_script(DnsScript::Restore, guard).await.map(|_| ());
match &result {
Ok(()) => logging!(info, Type::Config, "unset system dns successfully"),
Err(err) => logging!(error, Type::Config, "unset system dns failed: {err:#}"),
@@ -89,13 +92,22 @@ async fn unset_public_dns() -> anyhow::Result<()> {
result
}
enum DnsScript {
Set,
Restore,
}
enum DnsScriptOutcome {
Complete,
Deferred,
}
/// On timeout the script's whole process group is killed, so no networksetup it started writes late.
async fn run_dns_script(script_name: &str, allow_deferred: bool) -> anyhow::Result<DnsScriptOutcome> {
async fn run_dns_script(action: DnsScript, guard: MutexGuard<'static, ()>) -> anyhow::Result<DnsScriptOutcome> {
let script_name = match action {
DnsScript::Set => "set_dns.sh",
DnsScript::Restore => "unset_dns.sh",
};
let resource_dir = crate::utils::dirs::app_resources_dir()?;
let script = resource_dir.join(script_name);
let dir = dns_state_dir()?;
@@ -111,6 +123,8 @@ async fn run_dns_script(script_name: &str, allow_deferred: bool) -> anyhow::Resu
.spawn()
.with_context(|| format!("failed to run {}", script.display()))?;
crate::process::AsyncHandler::spawn_blocking(move || {
// The caller can time out while the child is still running.
let _guard = guard;
let deadline = Instant::now() + DNS_SCRIPT_TIMEOUT;
let status = loop {
if let Some(status) = child.try_wait()? {
@@ -128,7 +142,7 @@ async fn run_dns_script(script_name: &str, allow_deferred: bool) -> anyhow::Resu
pipe.read_to_string(&mut stderr)?;
}
// EX_TEMPFAIL is reserved by set_dns.sh for a network service that is not ready.
if allow_deferred && status.code() == Some(75) {
if matches!(action, DnsScript::Set) && status.code() == Some(75) {
return Ok(DnsScriptOutcome::Deferred);
}
ensure!(
+2 -2
View File
@@ -12,7 +12,7 @@ use crate::{
service::{SERVICE_MANAGER, ServiceManager},
tray::Tray,
},
core::{auto_backup::AutoBackupManager, lightweight::auto_lightweight_boot},
core::{auto_backup, lightweight::auto_lightweight_boot},
feat,
process::AsyncHandler,
utils::{init, server, window_manager::WindowManager},
@@ -150,7 +150,7 @@ async fn init_auto_lightweight_boot() {
}
async fn init_auto_backup() {
logging_error!(Type::Setup, AutoBackupManager::global().init().await);
auto_backup::init().await;
}
fn init_silent_updater() {