fix(MQTTDriver,MQTTTransport): free-heap guard prevents heap-exhaustion abort() under command bursts
Decoded a new crash via addr2line against the exact flashed ELF: console reported faulting task "async_tcp", cause 29, vaddr 0. Full backtrace traced through AsyncTCP's _async_service_task -> AsyncMqttClient::_onData -> MQTTTransport::onMessage -> CommandBus::dispatch -> the reply path -> MQTTDriver::publish -> AsyncMqttClient::publish -> PublishOutPacket's vector::reserve -> operator new failing -> unhandled C++ exception -> abort(). Root cause is heap fragmentation, not a single bad allocation: rapid relay.test_bell commands with Verbose/Debug logging meant every command triggered ~4-5 JsonDocument/String allocations (RX log, dispatch log, handler log, MQTT log-channel copies, reply publish), all on AsyncTCP's own task, faster than the allocator could coalesce freed blocks between cycles. Two "JSON parse error: NoMemory" warnings preceded the abort by seconds - an early symptom of the same fragmentation. Checked why this can't be a catchable exception: this SDK build has CONFIG_COMPILER_CXX_EXCEPTIONS_EMG_POOL_SIZE=0, so when the heap is too fragmented to even allocate the object needed to throw std::bad_alloc, it aborts instead of throwing. That's baked into the precompiled Arduino-ESP32 core; fixing it means rebuilding the whole ESP-IDF core from source, judged out of scope. Chose prevention over a catchable-exception fix. Adds MQTTDriver::hasHeapForPublish() (20KB threshold, chosen with margin above the ~62KB-free-but-already-fragmented state seen at crash time) and applies it before every allocation-heavy publish path (publish, publishDirect, publishHeartbeat) and before MQTTTransport:: onMessage() does its own incoming-command deserialization. Skip warnings use Serial.printf directly, not LOG_*, to avoid recursing into the same low-heap condition via MQTT-channel logging. Also corrects how coredump crash detail should be read for abort()- reached crashes (vs. true hardware exceptions like the F-051 stack canary trip): pc/exc_cause/exc_vaddr describe abort() internals in this case, not the original fault site - only the task name is reliably the "real" crash location. Documented for console/dashboard consumers in feature-catalog.md F-055. A full fix (queueing incoming commands off AsyncTCP's task, mirroring the F-051 logging-queue redesign) is scoped out as a documented future improvement, not rushed into this change. Verified: pio run on vesper-std-v1 and agnus-v1. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -45,6 +45,13 @@ void MQTTTransport::onNetworkDisconnected() {
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
void MQTTTransport::onMessage(const String& topic, const String& payload) {
|
||||
// Skip entirely under critical heap pressure — deserializing, dispatching, and
|
||||
// replying all allocate, and this runs inline on AsyncTCP's own task. Letting a
|
||||
// burst of incoming commands compound an already-low-heap condition is what led
|
||||
// to an unrecoverable abort() in production (this SDK build can't even throw
|
||||
// std::bad_alloc when allocation fails — see MQTTDriver::hasHeapForPublish()).
|
||||
if (!_mqtt.hasHeapForPublish()) return;
|
||||
|
||||
LOG_DEBUG(TAG, "RX [%s]: %s", topic.c_str(), payload.c_str());
|
||||
|
||||
JsonDocument doc;
|
||||
|
||||
@@ -59,3 +59,4 @@ Responses are published to the data topic:
|
||||
- **MQTT must be enabled in config.** `MQTTDriver::connect()` is a no-op if `mqttConfig.enabled` is false. Toggle with `mqtt.enable` / `mqtt.disable` commands.
|
||||
- **No per-message QoS.** Command replies and broadcasts are published at QoS 0. Heartbeats use QoS 1 with `retain = true`.
|
||||
- **JSON parse errors drop the message silently** (with a `LOG_WARNING`). The client receives no error response.
|
||||
- **Incoming commands are dropped under critical heap pressure.** `onMessage()` checks `MQTTDriver::hasHeapForPublish()` (free heap ≥ 20KB) before even constructing the `JsonDocument` used to deserialize the incoming payload. Below that threshold, the message is dropped with no reply and no error response — indistinguishable from a lost network message to the caller. This exists because a rapid burst of commands (each involving deserialize + dispatch + reply, all allocation-heavy, all running inline on AsyncTCP's own task) previously fragmented the heap enough to crash the device with an unrecoverable `abort()` — see `docs/features/feature-catalog.md` F-055. A full fix (queueing incoming commands to a dedicated task, so this work never runs on AsyncTCP's task at all) is a scoped future improvement, not yet implemented.
|
||||
|
||||
Reference in New Issue
Block a user