fix(Telemetry,HealthMonitor): bump task stacks to fix reboot-every-10min crash

TelemetryTask (4096B) overflowed its stack canary because
flushRuntimeToSD() nests ArduinoJson serialization + SD write + a
LOG_DEBUG call that fans out to Logging::log()'s 512B format buffer
and publishToMqtt()'s String + AsyncMqttClient::publish() — all on
the same small stack. Bumped to 8192B.

HealthMonitor's monitoring task shares the same nested-logging
pattern (11 LOG_* calls per health check pass) and was judged next
most likely to trip, so bumped 4096B -> 6144B pre-emptively.

RAM headroom confirmed fine post-bump (19.2%, 63KB/320KB used).
Root cause, other at-risk tasks, and the planned logging-queue
redesign are documented in feature-catalog.md F-051.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
2026-07-16 09:22:11 +03:00
co-authored by Claude Sonnet 5
parent 326ae2b767
commit 838e206a82
3 changed files with 35 additions and 2 deletions
+27
View File
@@ -60,6 +60,7 @@ Every feature — user-facing or significant internal mechanism — is listed he
| F-048 | SD log retrieval commands (`logs.list` / `logs.download`) | v2.1.2 | Stable |
| F-049 | `firmware.status` rollback safety-net diagnostics (retry/failure counts, backup partition) | v2.1.2 | Stable |
| F-050 | SD log lines prefixed with wall-clock date/time + uptime | v2.1.2 | Stable |
| F-051 | Fix: TelemetryTask/HealthMonitor stack overflow on SD+MQTT-nested logging | v2.0.3 | Stable |
---
@@ -687,4 +688,30 @@ Date/time comes from `time(nullptr)`, set by NTP via `Timekeeper::syncTimeWithNT
Serial and MQTT lines are unchanged (Serial already had the icon; MQTT already carries a `timestamp` field, albeit `millis()`-only).
---
### F-051 — Fix: TelemetryTask/HealthMonitor stack overflow on SD+MQTT-nested logging
Device rebooted every ~10 minutes with `Guru Meditation Error: ... Stack canary watchpoint triggered (TelemetryTask)`.
**Root cause:** `Telemetry::flushRuntimeToSD()` runs on `TelemetryTask` (4096-byte stack) every `RUNTIME_FLUSH_INTERVAL_MS`. Its call chain nests several stack-hungry operations on top of each other: `FileManager::writeJsonFile()` (SD.open + ArduinoJson `serializeJson`) → `LOG_DEBUG` on success → `Logging::log()` (allocates a 512-byte `char buffer[512]` for `vsnprintf`) → `Logging::publishToMqtt()` (builds a `String` reserved at 600 bytes) → `AsyncMqttClient::publish()`. Combined, this exceeded the 4096-byte budget and corrupted the stack canary.
**Fix applied:** Bumped `TelemetryTask` to 8192 bytes ([Telemetry.cpp](../../src/Telemetry/Telemetry.cpp)) and `HealthMonitor`'s monitoring task to 6144 bytes ([HealthMonitor.cpp](../../src/HealthMonitor/HealthMonitor.cpp)) — `HealthMonitor::performFullHealthCheck()` logs once per subsystem (11 `LOG_*` calls per pass) through the same nested nesting pattern and was judged the next most likely to trip. Verified via `pio run` — RAM usage after the bump is 19.2% (63KB/320KB), so the +6KB combined stack cost is not a concern.
**Not yet fixed — same risk remains in:** `OutputManager` (`OutputDuration`, 4096B), `TimeKeeper` (`TimeKeeper`, 4096B), `MQTTDriver`'s `mqttRecon`/`mqttHB`/`mqttConnect` (4096B each — their job *is* the MQTT publish, so no extra JSON/SD nesting, but no extra margin either), and `Networking`'s `netRecon` (3072B, the tightest of all). None have tripped yet, but all share the same structural fragility: any `LOG_*` call adds `Logging::log()`'s 512-byte buffer plus, if MQTT-enabled for that tag, `publishToMqtt()`'s 600-byte `String` and a synchronous `AsyncMqttClient::publish()` call onto whatever stack the calling task happens to have.
**Proper fix (not yet implemented) — move logging output off the caller's stack:**
The root architectural problem is that `Logging::log()` does all formatting *and* all I/O (Serial/MQTT/SD) synchronously, on whatever task called `LOG_*`. A task that already has other stack-heavy work nested (JSON serialization, SD I/O) has no way to know it's borrowing stack for the logger too.
Planned design:
1. Add a small dedicated `LoggingTask` (its own generously-sized stack, e.g. 4096B, since it only formats + ships bytes) fed by a FreeRTOS queue.
2. `Logging::log()` still does `vsnprintf` into its local buffer (cheap, fixed-size, stays on the caller's stack — this part is fine), but instead of calling `publishToMqtt()`/`writeToSd()` directly, it copies the formatted line (tag, level, message, timestamp) into a fixed-size queue item and calls `xQueueSend()` (non-blocking or short-timeout, so a full queue drops rather than blocks the caller).
3. `LoggingTask` drains the queue and performs the actual `AsyncMqttClient::publish()` and SD write — these expensive, variable-depth operations now only ever nest on the logging task's own stack, never on the caller's.
4. Serial output can stay synchronous in `Logging::log()` — `Serial.printf()` is cheap and doesn't need to move.
This fixes the *class* of bug for every current and future task that calls `LOG_*`, not just Telemetry/HealthMonitor. It does **not** fix stack pressure from non-logging causes (e.g. `FileManager::writeJsonFile()`'s own ArduinoJson/SD usage) — those still consume whatever they consume on the calling task's stack; only the logging fan-out moves.
Subsystem: `src/Telemetry/Telemetry.cpp`, `src/HealthMonitor/HealthMonitor.cpp`, `src/Logging/Logging.cpp`, `src/FileManager/FileManager.cpp`
Subsystem: `src/Logging/Logging.cpp`
+4 -1
View File
@@ -43,7 +43,10 @@ bool HealthMonitor::begin() {
xTaskCreatePinnedToCore(
monitoringTask,
"HealthMonitor",
4096,
// 6144: performFullHealthCheck() logs once per subsystem (11 LOG_* calls per
// pass) — same LOG_DEBUG -> publishToMqtt() nesting that overflowed
// TelemetryTask's 4096B stack. See stack-audit notes.
6144,
this,
3, // Medium priority
&_monitoringTaskHandle,
+4 -1
View File
@@ -20,7 +20,10 @@ void Telemetry::begin() {
loadStrikeCounters();
loadMetrics();
xTaskCreatePinnedToCore(telemetryTask, "TelemetryTask", 4096, this, 2, &telemetryTaskHandle, 1);
// 8192: flushRuntimeToSD() nests ArduinoJson serialization + SD write + LOG_DEBUG
// (512B format buffer + MQTT publish String + AsyncMqttClient::publish()) on this
// task's own stack. 4096 overflowed the canary in production — see stack-audit notes.
xTaskCreatePinnedToCore(telemetryTask, "TelemetryTask", 8192, this, 2, &telemetryTaskHandle, 1);
LOG_INFO(TAG, "Telemetry initialized");
}