Fix file locking race condition in SMART trend analysis

Check both file existence AND size > 0 before opening in r+ mode.
Previously, an empty file (0 bytes) would be opened in r+ mode, causing
json.load() to fail on the empty content.

Resolves #7

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-02-10 12:56:21 -05:00
parent d79005eb42
commit 0559f2d668

View File

@@ -783,7 +783,10 @@ class SystemHealthMonitor:
# Load historical data with file locking # Load historical data with file locking
history = [] history = []
file_mode = 'r+' if os.path.exists(historical_file) else 'w+' if os.path.exists(historical_file) and os.path.getsize(historical_file) > 0:
file_mode = 'r+'
else:
file_mode = 'w+'
with open(historical_file, file_mode) as f: with open(historical_file, file_mode) as f:
# Acquire exclusive lock # Acquire exclusive lock