From a5395b0de7477a0672ffcd8c711dd8f8182e9bff Mon Sep 17 00:00:00 2001 From: bonamin Date: Sat, 11 Jul 2026 11:27:50 +0300 Subject: [PATCH] =?UTF-8?q?fix(Telemetry):=20heap-corrupt=20crash=20in=20l?= =?UTF-8?q?ogBootEvent=20=E2=80=94=20StaticJsonDocument=20on=20small=20tas?= =?UTF-8?q?k=20stack?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- src/Telemetry/Telemetry.cpp | 15 ++++++++------- src/main.cpp | 2 +- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/Telemetry/Telemetry.cpp b/src/Telemetry/Telemetry.cpp index f81a879..0d4906e 100644 --- a/src/Telemetry/Telemetry.cpp +++ b/src/Telemetry/Telemetry.cpp @@ -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() ? doc["boots"].as() : doc["boots"].to(); - // 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(); 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(); } + uint32_t bootNum = (uint32_t)(doc["total_boots"] | 0) + 1; JsonObject entry = entries.add(); 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); } } diff --git a/src/main.cpp b/src/main.cpp index 88067fa..59c4455 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -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