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
+1 -1
View File
@@ -167,7 +167,7 @@ Command example:
**MQTT feedback loop.** Any subsystem whose logs are emitted on the MQTT channel must not call `Logging` from within the MQTT publish path. `MQTTClient` and `CommRouter` are silenced on MQTT by default for this reason. The `isPublishing` guard is a secondary safeguard.
**SD writes are synchronous.** `setSdWriteCallback` delegates to `FileManager::appendLine`, which acquires the SD card mutex. Calling `LOG_*` at high frequency with SD enabled can stall the calling task for the duration of the SD write.
**SD writes are synchronous, on whatever task logged.** `setSdWriteCallback` delegates to `FileManager::appendLogLine`, which runs on the calling task's own stack — there is no dedicated logging task. Calling `LOG_*` at high frequency with SD enabled stalls the calling task for the duration of the write, and every 50th write additionally does a full `/logs` directory scan (see [FileManager's log rotation notes](../FileManager/README.md#log-file-rotation)). Tasks with small stacks that may log at an SD-enabled level need enough headroom for this — the MQTT driver's connect/reconnect/heartbeat tasks were bumped to 4096 bytes after a stack-canary panic surfaced this.
**`isLevelEnabled` checks Serial only.** If you need to guard an expensive log-string build (e.g. formatting a large buffer before calling `LOG_DEBUG`), use `Logging::isLevelEnabled(Logging::DEBUG)`. Be aware this ignores MQTT and SD levels.