From 564c57fefc8e1e2c88c85b9a9abcaadd28354355 Mon Sep 17 00:00:00 2001 From: zhom <2717306+zhom@users.noreply.github.com> Date: Thu, 11 Dec 2025 22:57:21 +0400 Subject: [PATCH] refactor: prevent double counting --- src-tauri/src/traffic_stats.rs | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src-tauri/src/traffic_stats.rs b/src-tauri/src/traffic_stats.rs index 4ca42d2..acc6b17 100644 --- a/src-tauri/src/traffic_stats.rs +++ b/src-tauri/src/traffic_stats.rs @@ -797,16 +797,19 @@ impl LiveTrafficTracker { // Prune old data before adding new data to keep file size manageable stats.prune_old_data(); - // 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); - - // Update flush timestamp to track when data was last flushed - // This prevents double-counting session snapshots written before this flush + // Update flush timestamp BEFORE reading/resetting counters + // This prevents double-counting session snapshots written after this timestamp + // If we set it after reading counters, a session snapshot written just before + // the flush completes could have a timestamp newer than last_flush_timestamp, + // causing its data to be added even though it was already included in the flush let now = current_timestamp(); stats.last_flush_timestamp = now; stats.last_update = now; + // 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); + // Update bandwidth history stats.record_bandwidth(sent, received);