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
+13 -2
View File
@@ -355,8 +355,15 @@ bool FileManager::appendLogLine(const String& logDir, const String& line, time_t
return false;
}
// Pruning does a full directory scan — too expensive (stack and I/O) to
// run on every write. Check periodically instead; a day-rotated file
// growing past budget between checks is fine, the next check catches it.
if (maxTotalBytes > 0) {
pruneLogDirectory(logDir, maxTotalBytes);
logWriteCountSinceProbe++;
if (logWriteCountSinceProbe >= LOG_PRUNE_CHECK_INTERVAL) {
logWriteCountSinceProbe = 0;
pruneLogDirectory(logDir, maxTotalBytes);
}
}
return true;
@@ -413,7 +420,11 @@ void FileManager::pruneLogDirectory(const String& logDir, size_t maxTotalBytes)
String fullPath = dirPath + name;
if (SD.remove(fullPath.c_str())) {
LOG_INFO(TAG, "Log rotation: removed oldest log file %s (%u bytes)", fullPath.c_str(), (unsigned)entry.second);
// Deliberately not logged: this runs from inside the SD log write
// path itself (appendLogLine -> pruneLogDirectory), and LOG_* here
// would re-enter Logging::writeToSd() if FileManager's SD level is
// enabled. Serial-only would be safe but adds complexity for a
// rotation event that isn't actionable at runtime.
totalBytes -= entry.second;
}
}