From 9141ad854fce703627eea781ab09e3e154dbd4ad Mon Sep 17 00:00:00 2001 From: agessaman Date: Tue, 4 Aug 2026 09:45:30 -0700 Subject: [PATCH] perf(mqtt): allocate slot JWT token buffers on demand MQTTSlot carried an inline char auth_token[768] -- 768 of its 1192 bytes -- for every runtime slot, whether the slot was JWT, username/password, disabled, or above the active-slot cap. Now a char* allocated by ensureSlotAuthToken() from createSlotAuthToken(), the sole writer, which runs only once a slot is confirmed to have a JWT audience. Because _slots[] is a fixed member array, this shrinks the bridge object unconditionally at construction and re-adds only what is used: MQTTSlot 1192 -> 428 B (-764) MQTTBridge non-PSRAM 12032 -> 9740 B (-2292) PSRAM 10080 -> 5496 B (-4584) The object is allocated as one contiguous block at boot, so a smaller boot-time chunk is easier to satisfy and leaves a larger contiguous remainder; JWT slots then take 768 B each as separate blocks. Net steady-state saving is 768 B per slot that never creates a token -- 768 B on a fully configured board, up to 3840 B on a PSRAM board with one JWT slot. 22 of the 29 presets are MQTT_AUTH_JWT, so this is mostly reclaiming unconfigured slots rather than non-JWT ones. Lifetime rules, which are the whole risk here: - setCredentials() stores this pointer in _mqtt_cfg rather than copying, and esp-mqtt re-reads it whenever a later connect() re-applies a dirtied config. So the buffer is freed only alongside the client, in destroySlotClients(), after the delete. MQTTSlot::broker_uri already carries an "avoids dangling pointer" comment from this same hazard with setServer(). - teardownSlot() still clears the token to an empty string but keeps the buffer: the client survives teardown and does not clear cfg->password (only setupSlot()'s reconfigure branch does). Freeing there would dangle. - Never freed per reconnect, preserving the churn-avoidance the inline buffer was there for. Allocation goes through MQTTRuntimeBufferLifecycle (already host-tested) using plain malloc, so the buffer stays in internal DRAM exactly where it was when inline. Failure propagates through createSlotAuthToken()'s existing bool. The two call sites that create a token and then test it (preset JWT in setupSlot, and the custom-slot audience path) now null-check first; the five other readers are all inside if (createSlotAuthToken(...)) success branches and need no change. destroySlotClients() releases the token unconditionally rather than after its client null-check, so a token can never outlive its slot. flash non-PSRAM 1592697 -> 1592801 B (+104) PSRAM 1555109 -> 1555213 B (+104) 267/267 native tests pass; both observer envs build clean. --- src/helpers/bridges/MQTTBridge.cpp | 71 +++++++++++++++++++++++------- src/helpers/bridges/MQTTBridge.h | 14 ++++-- 2 files changed, 65 insertions(+), 20 deletions(-) diff --git a/src/helpers/bridges/MQTTBridge.cpp b/src/helpers/bridges/MQTTBridge.cpp index 556936a11..5d42f00f7 100644 --- a/src/helpers/bridges/MQTTBridge.cpp +++ b/src/helpers/bridges/MQTTBridge.cpp @@ -692,7 +692,7 @@ MQTTBridge::MQTTBridge(NodePrefs *prefs, MQTTPrefs *obs, mesh::PacketManager *mg _slots[i].enabled = false; _slots[i].client = nullptr; _slots[i].preset = nullptr; - // auth_token[0] == '\0' after memset above — no valid token + // auth_token == nullptr after memset above — allocated on first token creation _slots[i].connected = false; _slots[i].initial_connect_done = false; _slots[i].token_expires_at = 0; @@ -1670,21 +1670,52 @@ bool MQTTBridge::ensureSlotClient(int index) { return true; } +// Allocate this slot's JWT token buffer. Called only from createSlotAuthToken(), the +// sole writer, so a slot on a non-JWT preset (or no preset at all) never allocates. +// Internal DRAM, matching where the buffer lived when it was inline in MQTTSlot. +bool MQTTBridge::ensureSlotAuthToken(int index) { + if (index < 0 || index >= RUNTIME_MQTT_SLOTS) return false; + MQTTSlot& slot = _slots[index]; + slot.auth_token = static_cast(MQTTRuntimeBufferLifecycle::allocateIfMissing( + slot.auth_token, AUTH_TOKEN_SIZE, malloc)); + if (slot.auth_token == nullptr) { + MQTT_DEBUG_PRINTLN("MQTT%d: out of memory allocating auth token", index + 1); + return false; + } + slot.auth_token[0] = '\0'; + return true; +} + +// Safe only once this slot's client is gone: setCredentials() gave the client this +// pointer, and esp-mqtt re-reads it from _mqtt_cfg on any later connect() that +// re-applies a dirtied config. See the MQTTSlot::auth_token comment. +void MQTTBridge::releaseSlotAuthToken(int index) { + if (index < 0 || index >= RUNTIME_MQTT_SLOTS) return; + MQTTSlot& slot = _slots[index]; + slot.auth_token = static_cast( + MQTTRuntimeBufferLifecycle::release(slot.auth_token, free)); + slot.token_expires_at = 0; + slot.last_token_renewal = 0; +} + void MQTTBridge::destroySlotClients() { for (int i = 0; i < RUNTIME_MQTT_SLOTS; i++) { MQTTSlot& slot = _slots[i]; - if (slot.client == nullptr) continue; - - if (slot.client->connected()) { - slot.client->disconnect(); + if (slot.client != nullptr) { + if (slot.client->connected()) { + slot.client->disconnect(); + } + #ifdef ESP_PLATFORM + vTaskDelay(pdMS_TO_TICKS(50)); + #else + delay(50); + #endif + delete slot.client; + slot.client = nullptr; } - #ifdef ESP_PLATFORM - vTaskDelay(pdMS_TO_TICKS(50)); - #else - delay(50); - #endif - delete slot.client; - slot.client = nullptr; + // Unconditional: only now is the token unreachable from the client's stored + // config, and a token without a client would otherwise leak. + releaseSlotAuthToken(i); } } @@ -1727,7 +1758,7 @@ void MQTTBridge::setupSlot(int index) { cfg->username = nullptr; cfg->password = nullptr; #endif - slot.auth_token[0] = '\0'; + if (slot.auth_token) slot.auth_token[0] = '\0'; slot.connected = false; slot.token_expires_at = 0; slot.last_token_renewal = 0; @@ -1760,7 +1791,7 @@ void MQTTBridge::setupSlot(int index) { // Try to create token and connect (will succeed only if NTP synced) if (slot.preset->auth_type == MQTT_AUTH_JWT) { createSlotAuthToken(index); - if (slot.auth_token[0] != '\0') { + if (slot.auth_token && slot.auth_token[0] != '\0') { slot.client->setCredentials(_jwt_username, slot.auth_token); } } else if (slot.preset->auth_type == MQTT_AUTH_USERPASS) { @@ -1871,9 +1902,9 @@ void MQTTBridge::setupSlot(int index) { // Custom slot authentication: JWT if audience is set, else username/password if (slot.audience[0] != '\0') { - // JWT auth for custom slot — create initial token (buffer is always inline) + // JWT auth for custom slot — create initial token (allocates the buffer) createSlotAuthToken(index); - if (slot.auth_token[0] != '\0') { + if (slot.auth_token && slot.auth_token[0] != '\0') { slot.client->setCredentials(_jwt_username, slot.auth_token); } MQTT_DEBUG_PRINTLN("MQTT%d custom broker using JWT auth (audience: %s)", index + 1, slot.audience); @@ -1903,7 +1934,9 @@ void MQTTBridge::teardownSlot(int index) { #endif } - slot.auth_token[0] = '\0'; + // Invalidate the token but keep the buffer: the client survives teardown and still + // holds this pointer in its config (see MQTTSlot::auth_token). + if (slot.auth_token) slot.auth_token[0] = '\0'; slot.connected = false; slot.initial_connect_done = false; slot.broker_uri[0] = '\0'; @@ -2172,6 +2205,10 @@ bool MQTTBridge::createSlotAuthToken(int index) { } if (!audience || audience[0] == '\0') return false; + // This slot is confirmed JWT, so it needs the token buffer. Allocated on first use + // and kept thereafter; every caller already treats false as "no usable token". + if (!ensureSlotAuthToken(index)) return false; + // Ensure JWT username is set if (_jwt_username[0] == '\0') { char public_key_hex[65]; diff --git a/src/helpers/bridges/MQTTBridge.h b/src/helpers/bridges/MQTTBridge.h index 850794e8e..d1c72e3f6 100644 --- a/src/helpers/bridges/MQTTBridge.h +++ b/src/helpers/bridges/MQTTBridge.h @@ -86,9 +86,15 @@ private: bool connected; // Updated in callbacks bool initial_connect_done; // True after first connect() call - // JWT auth state (used by preset JWT slots and custom slots with audience set) - // Inline buffer avoids per-reconnect heap alloc/free churn (fragmentation source). - char auth_token[AUTH_TOKEN_SIZE]; // empty string = no valid token + // JWT auth state (used by preset JWT slots and custom slots with audience set). + // nullptr until this slot first creates a token, so a slot that is unconfigured, + // capped off, or on a non-JWT preset never pays for AUTH_TOKEN_SIZE. Allocated by + // ensureSlotAuthToken() and then held for the client's lifetime -- never freed per + // reconnect (alloc/free churn is a fragmentation source) and never freed on + // teardown, because setCredentials() hands this exact pointer to the client and + // esp-mqtt re-reads it whenever a later connect() re-applies a dirtied config. + // Freed only alongside the client in destroySlotClients(). + char* auth_token; // nullptr or empty string = no valid token unsigned long token_expires_at; unsigned long last_token_renewal; @@ -409,6 +415,8 @@ private: // This avoids delete/new cycles that shed ~40 KB of mbedTLS buffers per // reconfigure and fragment the internal heap on non-PSRAM boards. bool ensureSlotClient(int index); // Allocate this slot's persistent client + callbacks on first use + bool ensureSlotAuthToken(int index); // Allocate this slot's JWT token buffer on first token creation + void releaseSlotAuthToken(int index);// Free the token buffer (only with the client — see MQTTSlot) void destroySlotClients(); // Delete all persistent clients (shutdown only) void setupSlot(int index); // Configure and connect the slot's existing client void teardownSlot(int index); // Disconnect the slot's client (keeps the object alive)