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:
@@ -250,17 +250,17 @@ void Telemetry::flushRuntimeToSD() {
|
|||||||
void Telemetry::logBootEvent(uint32_t bootTimestamp) {
|
void Telemetry::logBootEvent(uint32_t bootTimestamp) {
|
||||||
if (!fileManager) return;
|
if (!fileManager) return;
|
||||||
|
|
||||||
// Read existing boot log (or create fresh)
|
// Heap-allocated — boot log can be large (up to 200 entries).
|
||||||
StaticJsonDocument<8192> doc;
|
// StaticJsonDocument on a task stack would overflow; JsonDocument uses heap.
|
||||||
|
JsonDocument doc;
|
||||||
fileManager->readJsonFile("/telemetry_boot_log.json", doc);
|
fileManager->readJsonFile("/telemetry_boot_log.json", doc);
|
||||||
|
|
||||||
JsonArray entries = doc["boots"].is<JsonArray>()
|
JsonArray entries = doc["boots"].is<JsonArray>()
|
||||||
? doc["boots"].as<JsonArray>()
|
? doc["boots"].as<JsonArray>()
|
||||||
: doc["boots"].to<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) {
|
while (entries.size() >= BOOT_LOG_MAX_ENTRIES) {
|
||||||
// Remove oldest (index 0) by rebuilding — ArduinoJson doesn't support remove(0) on array
|
|
||||||
JsonDocument tmp;
|
JsonDocument tmp;
|
||||||
JsonArray fresh = tmp["boots"].to<JsonArray>();
|
JsonArray fresh = tmp["boots"].to<JsonArray>();
|
||||||
for (size_t i = 1; i < entries.size(); i++) fresh.add(entries[i]);
|
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>();
|
entries = doc["boots"].as<JsonArray>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
uint32_t bootNum = (uint32_t)(doc["total_boots"] | 0) + 1;
|
||||||
JsonObject entry = entries.add<JsonObject>();
|
JsonObject entry = entries.add<JsonObject>();
|
||||||
entry["ts"] = bootTimestamp;
|
entry["ts"] = bootTimestamp;
|
||||||
entry["boot"] = (uint32_t)(doc["total_boots"] | 0) + 1;
|
entry["boot"] = bootNum;
|
||||||
doc["total_boots"] = (uint32_t)(doc["total_boots"] | 0) + 1;
|
doc["total_boots"] = bootNum;
|
||||||
|
|
||||||
if (!fileManager->writeJsonFile("/telemetry_boot_log.json", doc)) {
|
if (!fileManager->writeJsonFile("/telemetry_boot_log.json", doc)) {
|
||||||
LOG_WARNING(TAG, "Failed to write boot log");
|
LOG_WARNING(TAG, "Failed to write boot log");
|
||||||
} else {
|
} 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
@@ -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)
|
// Non-blocking NTP sync timer callback — defers work to a task (Tmr Svc stack is only 2KB)
|
||||||
void ntpSyncTimerCallback(TimerHandle_t xTimer) {
|
void ntpSyncTimerCallback(TimerHandle_t xTimer) {
|
||||||
xTaskCreate(ntpSyncTask, "ntpSync", 3072, NULL, 5, NULL);
|
xTaskCreate(ntpSyncTask, "ntpSync", 5120, NULL, 5, NULL);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user