Files
project-vesper/src/Communication/Transport
bonaminandClaude Sonnet 5 9cd6b142ef 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>
2026-07-16 18:47:39 +03:00
..
2026-03-14 22:16:03 +02:00

Transport Layer

Summary

The Transport layer adapts five communication protocols — MQTT, WebSocket, HTTP, UART, and the settings web server — to a common interface consumed by the CommandBus. Each transport is responsible for receiving raw input from its protocol, parsing it into a CommandEnvelope, injecting a reply lambda that routes the response back over the same protocol, and calling CommandBus::dispatch(). For unsolicited events, the bus calls each transport's broadcast() method. Transports have no knowledge of what handlers do; handlers have no knowledge of which transport a command arrived on. The transport identity is recorded in CommandEnvelope::transport for the rare cases where a handler needs it (e.g. identify is WebSocket-only).


ITransport Interface

All transports implement ITransport:

Method Description
virtual void begin() = 0 Initialise the transport. Called once from CommunicationRouter::begin().
virtual void loop() = 0 Service the event loop. No-op for event-driven transports; required for UART polling.
virtual bool isHealthy() const = 0 Health check used by HealthMonitor / CommunicationRouter::isHealthy().
virtual String name() const = 0 Human-readable name for logging (e.g. "MQTT", "WebSocket").
virtual void broadcast(const String& msg) = 0 Push a pre-serialised JSON string to all clients. No-op for stateless transports (HTTP, WebServerTransport).

Transports at a Glance

Transport Protocol Endpoint / Topic Persistent connection Broadcast
MQTTTransport MQTT (AsyncMqttClient) vesper/{uid}/control (RX), vesper/{uid}/data (TX) Yes (broker) Yes — publishes to data topic
WebSocketTransport WebSocket /ws Yes (per client) Yes — all or targeted clients
HTTPTransport HTTP REST POST /api/command, GET /api/ping, GET /api/status No No-op
UARTTransport UART (Serial2) GPIO 12 TX / 13 RX @ 115200 Yes (slave device) Yes — sends to UART slave
WebServerTransport HTTP (settings UI) GET /settings, POST /api/set-mode, POST /api/reboot No No-op

See the subdirectory README for each transport for details.