refactor: Replace tokio::spawn with AsyncHandler::spawn for better task management

- Replace direct tokio::spawn calls with AsyncHandler::spawn across multiple modules
- Improves task lifecycle management and error handling consistency
- Affected files:
  - src-tauri/src/cmd/network.rs
  - src-tauri/src/core/core.rs
  - src-tauri/src/core/event_driven_proxy.rs
  - src-tauri/src/enhance/tun.rs
  - src-tauri/src/ipc/logs.rs
  - src-tauri/src/ipc/memory.rs
  - src-tauri/src/ipc/monitor.rs
  - src-tauri/src/ipc/traffic.rs
  - src-tauri/src/utils/network.rs
  - src-tauri/src/utils/resolve.rs

This change provides better control over async task spawning and helps prevent
potential issues with unmanaged background tasks.
This commit is contained in:
Tunglies
2025-08-22 03:41:14 +08:00
parent 02f67961a9
commit e4c243de2d
10 changed files with 29 additions and 16 deletions

View File

@@ -10,7 +10,7 @@ use std::{
};
use tokio::runtime::{Builder, Runtime};
use crate::{config::Config, logging, singleton_lazy, utils::logging::Type};
use crate::{config::Config, logging, process::AsyncHandler, singleton_lazy, utils::logging::Type};
// HTTP2 相关
const H2_CONNECTION_WINDOW_SIZE: u32 = 1024 * 1024;
@@ -351,7 +351,7 @@ impl NetworkManager {
let (cancel_tx, cancel_rx) = tokio::sync::oneshot::channel::<()>();
let url_clone = url.to_string();
let watchdog = tokio::spawn(async move {
let watchdog = AsyncHandler::spawn(move || async move {
tokio::time::sleep(Duration::from_secs(timeout_duration)).await;
let _ = cancel_tx.send(());
logging!(warn, Type::Network, true, "请求超时取消: {}", url_clone);