fix(Logging): defer MQTT/SD publish off the caller's stack via dedicated task+queue
Logging::log() previously ran AsyncMqttClient::publish() and SD file writes synchronously on whatever task called LOG_*. Both are variable-depth, stack-hungry operations, and this is what let TelemetryTask's 4096B stack silently absorb enough nested cost to overflow its canary (see feature-catalog.md F-051). Serial output stays synchronous (cheap, and must reflect true event order for crash debugging). MQTT/SD-bound messages are now copied into a fixed-size LogQueueItem (256-byte message cap, audited against real LOG_* call sites — worst case ~150-180 chars, comfortable margin) and handed via non-blocking xQueueSend to a lazily-created LoggingTask, which performs the actual publish/write on its own 4096B stack. A full queue drops the line rather than blocking the caller. Queue+task creation is spinlock-guarded against concurrent first-callers. This removes the stack-nesting risk for every current and future task that logs, not just Telemetry/HealthMonitor — closing the broader risk flagged in F-051 for OutputManager, TimeKeeper, MQTTDriver's own tasks, and Networking's netRecon. Verified via pio run: RAM impact negligible (62980 vs 62956 bytes). Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -34,6 +34,8 @@
|
||||
#include <Arduino.h>
|
||||
#include <map>
|
||||
#include <functional>
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/queue.h>
|
||||
|
||||
class Logging {
|
||||
public:
|
||||
@@ -130,6 +132,40 @@ private:
|
||||
// Resolve effective level for a tag on a channel
|
||||
static LogLevel resolveLevel(const char* tag, LogLevel globalLevel, const std::map<String, LogLevel>& overrides);
|
||||
|
||||
// ═══════════════════════════════════════════════════════════════════════════════
|
||||
// DEFERRED OUTPUT (MQTT publish + SD write)
|
||||
// Both channels do variable-depth, stack-hungry I/O (AsyncMqttClient::publish(),
|
||||
// SD file writes). Running them synchronously on whatever task called LOG_*
|
||||
// let a small task's stack silently absorb that cost — this is what overflowed
|
||||
// TelemetryTask's 4096B stack in production (see feature-catalog.md F-051).
|
||||
// Instead, log() copies the formatted message into a fixed-size queue item and
|
||||
// a dedicated LoggingTask (its own stack) performs the actual MQTT/SD I/O.
|
||||
// Serial output is NOT deferred — it's cheap and stays synchronous so what you
|
||||
// see over USB reflects the true, immediate order of events (important when
|
||||
// debugging a crash).
|
||||
// ═══════════════════════════════════════════════════════════════════════════════
|
||||
static constexpr size_t LOG_QUEUE_MESSAGE_MAX = 256; // truncates longer messages; see F-051 audit
|
||||
static constexpr UBaseType_t LOG_QUEUE_DEPTH = 16;
|
||||
|
||||
struct LogQueueItem {
|
||||
LogLevel level;
|
||||
char tag[24];
|
||||
char levelStr[6];
|
||||
char message[LOG_QUEUE_MESSAGE_MAX];
|
||||
bool toMqtt;
|
||||
bool toSd;
|
||||
};
|
||||
|
||||
static QueueHandle_t _logQueue;
|
||||
static TaskHandle_t _loggingTaskHandle;
|
||||
static portMUX_TYPE _logQueueInitSpinlock;
|
||||
|
||||
// Lazily created on first deferred item — MQTT/SD callbacks are registered
|
||||
// well after boot (CommunicationRouter::begin()), so there's nothing to
|
||||
// drain before then.
|
||||
static void ensureLoggingTaskStarted();
|
||||
static void loggingTask(void* parameter);
|
||||
|
||||
// ═══════════════════════════════════════════════════════════════════════════════
|
||||
// MQTT RATE LIMITING
|
||||
// Per-subsystem token bucket. Protects the broker/airtime from a chatty or
|
||||
|
||||
Reference in New Issue
Block a user