fix(Telemetry): heap-corrupt crash in logBootEvent — StaticJsonDocument on small task stack

StaticJsonDocument<8192> in logBootEvent() was allocating 8KB on the stack of
ntpSyncTask, which only had 3072 bytes. Stack overflow silently corrupted the
heap, triggering assert in multi_heap_free immediately after the boot log write.

Fix: switch logBootEvent to heap-allocated JsonDocument (ArduinoJson v7 default).
Also bumped ntpSyncTask stack from 3072 → 5120 bytes for NTP + SD write headroom.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-07-11 11:27:50 +03:00
co-authored by Claude Sonnet 4.6
parent 07d5b2fb81
commit a5395b0de7
2 changed files with 9 additions and 8 deletions
+8 -7
View File
@@ -250,17 +250,17 @@ void Telemetry::flushRuntimeToSD() {
void Telemetry::logBootEvent(uint32_t bootTimestamp) {
if (!fileManager) return;
// Read existing boot log (or create fresh)
StaticJsonDocument<8192> doc;
// Heap-allocated — boot log can be large (up to 200 entries).
// StaticJsonDocument on a task stack would overflow; JsonDocument uses heap.
JsonDocument doc;
fileManager->readJsonFile("/telemetry_boot_log.json", doc);
JsonArray entries = doc["boots"].is<JsonArray>()
? doc["boots"].as<JsonArray>()
: doc["boots"].to<JsonArray>();
// Rotate: keep only the last BOOT_LOG_MAX_ENTRIES - 1 entries
// Rotate: keep only the last BOOT_LOG_MAX_ENTRIES - 1 entries before adding
while (entries.size() >= BOOT_LOG_MAX_ENTRIES) {
// Remove oldest (index 0) by rebuilding — ArduinoJson doesn't support remove(0) on array
JsonDocument tmp;
JsonArray fresh = tmp["boots"].to<JsonArray>();
for (size_t i = 1; i < entries.size(); i++) fresh.add(entries[i]);
@@ -268,15 +268,16 @@ void Telemetry::logBootEvent(uint32_t bootTimestamp) {
entries = doc["boots"].as<JsonArray>();
}
uint32_t bootNum = (uint32_t)(doc["total_boots"] | 0) + 1;
JsonObject entry = entries.add<JsonObject>();
entry["ts"] = bootTimestamp;
entry["boot"] = (uint32_t)(doc["total_boots"] | 0) + 1;
doc["total_boots"] = (uint32_t)(doc["total_boots"] | 0) + 1;
entry["boot"] = bootNum;
doc["total_boots"] = bootNum;
if (!fileManager->writeJsonFile("/telemetry_boot_log.json", doc)) {
LOG_WARNING(TAG, "Failed to write boot log");
} else {
LOG_INFO(TAG, "Boot #%u logged at timestamp %u", (uint32_t)doc["total_boots"], bootTimestamp);
LOG_INFO(TAG, "Boot #%u logged at timestamp %u", bootNum, bootTimestamp);
}
}
+1 -1
View File
@@ -459,7 +459,7 @@ static void ntpSyncTask(void* param) {
// Non-blocking NTP sync timer callback — defers work to a task (Tmr Svc stack is only 2KB)
void ntpSyncTimerCallback(TimerHandle_t xTimer) {
xTaskCreate(ntpSyncTask, "ntpSync", 3072, NULL, 5, NULL);
xTaskCreate(ntpSyncTask, "ntpSync", 5120, NULL, 5, NULL);
}
#endif