fix(FileManager): SD log pruning caused stack-canary panic on MQTT connect task

Confirmed on hardware: enabling SD logging at INFO level caused a boot loop
within seconds — "Guru Meditation Error... Stack canary watchpoint triggered
(mqttConnect)". appendLogLine() was running pruneLogDirectory() (a full
SD.open() directory scan + std::vector<pair<String,size_t>> build) on every
single log write, and the first write happened to land on MQTTDriver's
3KB-stack connect task.

Fixes:
- Prune check now throttled to once per 50 writes instead of every write —
  removes the expensive directory scan from the hot logging path entirely.
- Bumped mqttConnect/mqttRecon/mqttHB task stacks from 3072 to 4096 bytes
  as defense-in-depth, since these tasks can still trigger SD log writes.
- Removed a LOG_INFO call inside pruneLogDirectory that could have re-entered
  Logging::writeToSd() from within the SD write path itself.
This commit is contained in:
2026-07-15 00:21:52 +03:00
parent 7675a67c90
commit e737769887
6 changed files with 33 additions and 9 deletions
+4 -2
View File
@@ -622,9 +622,11 @@ Fixes a bug where SD logging was a silent no-op: `FileManager::appendLine` never
New `FileManager::appendLogLine(logDir, line, timestamp, maxTotalBytes)` replaces the direct `appendLine` call as the SD log channel's write path:
- Ensures `logDir` exists before every write.
- Names the file `YYYY-MM-DD.log` from the supplied timestamp (`time(nullptr)`, works with or without an RTC once NTP has synced). Falls back to `unset-date.log` if no wall clock is available yet, rather than dropping early-boot lines.
- Prunes oldest whole files once the directory exceeds a 512KB total budget (`SD_LOG_MAX_TOTAL_BYTES` in `CommunicationRouter.cpp`).
- Every 50 writes (not every write), sums directory size and prunes oldest whole files once over a 512KB total budget (`SD_LOG_MAX_TOTAL_BYTES` in `CommunicationRouter.cpp`).
Subsystems: `src/FileManager/FileManager.cpp`, `src/Communication/CommunicationRouter/CommunicationRouter.cpp`
**Fixed after initial testing:** the first version pruned on every write, which does a full `SD.open()` directory scan + `std::vector` build on the calling task's own stack. The first device test (SD log level set to INFO) hit this from `MQTTDriver`'s 3KB-stack connect task and panicked with a stack canary trigger, causing a reboot loop. Fixed by throttling the prune check to once per 50 writes and bumping the three MQTT driver task stacks (connect/reconnect/heartbeat) from 3072 to 4096 bytes.
Subsystems: `src/FileManager/FileManager.cpp`, `src/Communication/CommunicationRouter/CommunicationRouter.cpp`, `src/Communication/Drivers/MQTTDriver/MQTTDriver.cpp`
---