diff --git a/src/helpers/bridges/MQTTBridge.cpp b/src/helpers/bridges/MQTTBridge.cpp index 5d42f00f7..ebb63887a 100644 --- a/src/helpers/bridges/MQTTBridge.cpp +++ b/src/helpers/bridges/MQTTBridge.cpp @@ -1672,12 +1672,18 @@ bool MQTTBridge::ensureSlotClient(int index) { // 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. +// +// PSRAM where the board has it (psram_malloc falls back to internal DRAM otherwise), +// which is what moves the token off internal heap for slots that DO use JWT. Safe +// because the only readers are CPU copies on the bridge task: JWTHelper memcpy's the +// token in here, and esp-mqtt copies it out of _mqtt_cfg into its own internal-DRAM +// storage when connect() applies the config. No DMA, no ISR, and no cache-disabled +// window -- unlike the PSRAM task stack that reset Heltec V4 boards. 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)); + slot.auth_token, AUTH_TOKEN_SIZE, psram_malloc)); if (slot.auth_token == nullptr) { MQTT_DEBUG_PRINTLN("MQTT%d: out of memory allocating auth token", index + 1); return false; @@ -1693,7 +1699,7 @@ 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)); + MQTTRuntimeBufferLifecycle::release(slot.auth_token, psram_free)); slot.token_expires_at = 0; slot.last_token_renewal = 0; } diff --git a/src/helpers/bridges/MQTTBridge.h b/src/helpers/bridges/MQTTBridge.h index d1c72e3f6..4c7436c2f 100644 --- a/src/helpers/bridges/MQTTBridge.h +++ b/src/helpers/bridges/MQTTBridge.h @@ -88,7 +88,8 @@ private: // 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 + // capped off, or on a non-JWT preset never pays for AUTH_TOKEN_SIZE; slots that do + // use JWT keep their buffer in PSRAM where the board has it. 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