From 68d0741f38abed1971cbd69ad08652104aed3d95 Mon Sep 17 00:00:00 2001 From: zhom <2717306+zhom@users.noreply.github.com> Date: Thu, 11 Dec 2025 22:46:21 +0400 Subject: [PATCH] refactor: properly clear lock --- src-tauri/src/traffic_stats.rs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src-tauri/src/traffic_stats.rs b/src-tauri/src/traffic_stats.rs index 9350246..4ca42d2 100644 --- a/src-tauri/src/traffic_stats.rs +++ b/src-tauri/src/traffic_stats.rs @@ -771,7 +771,16 @@ impl LiveTrafficTracker { // Use file locking to prevent concurrent writes from multiple proxy processes let lock_path = get_traffic_stats_dir().join(format!("{}.lock", storage_key)); - let _lock = acquire_file_lock(&lock_path)?; + let _lock = match acquire_file_lock(&lock_path) { + Ok(lock) => lock, + Err(e) => { + // If lock acquisition fails, reset counters to prevent indefinite accumulation + // The data will be lost, but this prevents memory growth + let _ = self.bytes_sent.swap(0, Ordering::Relaxed); + let _ = self.bytes_received.swap(0, Ordering::Relaxed); + return Err(e); + } + }; // Load or create stats using the storage key let mut stats = load_traffic_stats(&storage_key) @@ -788,7 +797,7 @@ impl LiveTrafficTracker { // Prune old data before adding new data to keep file size manageable stats.prune_old_data(); - // Reset counters after reading + // Reset counters after reading (lock is held, so flush will proceed) let sent = self.bytes_sent.swap(0, Ordering::Relaxed); let received = self.bytes_received.swap(0, Ordering::Relaxed);