You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Bug] Boot Failure by memory exhaustion on ESP32-S3 based devices #506
Investigated serial logs and decoded ELF backtraces from a populated T-Deck Plus (t-deck-tft, ESP32-S3, 8 MB PSRAM) running with Wi-Fi enabled, a microSD card inserted, ~138–139 active nodes in NodeDB, and ~1,084 saved messages.
Root cause: too large max_packet_queue_size in PacketServer
This will consumes ~242KB of memory when the queue is full like my case observed on t-deck-tft(ESP32-S3)
And it is hit-or-miss to fit in boot sequence, and becomes impossible when Wi-Fi is enabled and/or SD card is being utilized.
(This also explains why clean bench flashes rarely hit the issue: a fresh NodeDB with few nodes and little/no message history queues only a few KB during both windows.)
I hereby withdraw previous analysis, huge thanks @mverch67 to point me into a right direction 🙇
Primary cause — DataPacket payloads stay in internal DRAM instead of PSRAM (include/util/Packet.h)
On ESP32-S3, ESP-IDF's default CONFIG_SPIRAM_MALLOC_ALWAYSINTERNAL (16384 bytes) routes standard new/malloc allocations <= 16 KB into internal DRAM, even when megabytes of PSRAM are free.
DataPacket<PacketType> allocates new PacketType(...) (~768 B for meshtastic_FromRadio).
With Wi-Fi enabled, free internal DRAM after Wi-Fi starts is ~88 KB. During boot, Core 1 (PacketAPI) pushes two large packet bursts into SharedQueue::serverQueue while Core 0 (DeviceUI) is busy and not calling receive():
Wave 2 (restoreTextMessages()): Restoring 1,084 messages blocks ViewController::receive() for ~8.6 s while PacketAPI queues ~167 satellite-DB replay packets (positions/telemetry/status ≈ 134 KB), exhausting internal DRAM a second time.
Crash mechanism (source/storage/FatFsStorage.cpp)
When microSD is inserted at boot, early SD setup leaves ~392 B less internal DRAM than no-SD. Once Wave 1 empties internal DRAM, FatFsStorage::mount() → FatFsStorage::release() attempts to lock std::mutex ioLock.
Under ESP-IDF, std::mutex lazily allocates its underlying FreeRTOS semaphore via pthread_mutex_lock() on first lock. With zero internal DRAM left, allocation fails and std::mutex::lock() calls std::__throw_system_error(); libgcc's unwinder (_Unwind_Find_FDE → pthread_mutex_unlock) then dereferences an uninitialized mutex handle (LoadProhibited, EXCVADDR: 0x00000003), causing a deterministic boot-loop.
Even with no microSD inserted, internal DRAM was still critically starved: on a "successful" boot, createStorage() barely survived by ~392 B, only for the very next mutex allocation inside FatFs f_mount() to fail with FR_INT_ERR (f_mount failed: (2) Assertion failed), and later Wave 2 starved AsyncMessageLog::start() (xTaskCreate(..., 4096) failed with Cannot start message log worker).
Sanitized Example Logs (T-Deck Plus, ~139 nodes, 1,084 saved messages)
Before fix — microSD inserted + Wi-Fi enabled (deterministic boot-loop)
If 30(~25KB) is still too big and crashes on my test, next value to try will be this(estimated ~21KB)
below was my previous idea, which i hereby withdrawn as it does not solve the root cause:
PSRAM payload allocation for DataPacket<PacketType> (include/util/Packet.h): On ARDUINO_ARCH_ESP32 builds with PSRAM (BOARD_HAS_PSRAM / CONFIG_SPIRAM), allocate packet payloads via heap_caps_aligned_alloc(..., MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT) with a MALLOC_CAP_DEFAULT fallback and placement-new, paired with a stateless PayloadDeleter calling ~PacketType() + heap_caps_free(). This eliminates both Wave 1 and Wave 2 internal-DRAM drains.
Static internal-SRAM FreeRTOS mutex in FatFsStorage (include/storage/FatFsStorage.h, source/storage/FatFsStorage.cpp): Defense-in-depth replacement of std::mutex ioLock with a DRAM_ATTR StaticSemaphore_t initialized once via xSemaphoreCreateMutexStatic, avoiding dynamic internal-heap allocation on first lock while keeping the FreeRTOS spinlock strictly in internal SRAM (never PSRAM).
Initial smoke-testing on T-Deck Plus (microSD + Wi-Fi enabled across multiple manual reboots and shutdown) passed without issues. Soak testing is now ongoing; a PR will follow soon.
It's not a good idea to move the temporary packet data into PSRAM memory. This only addresses symptoms rather than fixing a root cause and may not work on devices with little PSRAM capacity.
Instead I would lower the packet queue size which is filled up too fast by the firmware process which consumes all the temporary memory:
return isConnected && server->available(); should be simply return isConnected,
as current code will mistakenly drops API connection when the queue(which is now smaller than before) is full, causing stale updates.
Copilot (tbh i rarely use this to verify something) agrees with this, and I'm now building a firmware with that change too.
checkIsConnected() is used by APIServerPort to decide whether to discard the current API instance.
So if the queue is temporarily full, this returns false even though the client may still be connected;
the caller may then reset the instance and drop its state. Queue capacity and connection status are better checked separately.
if it's true and does fix all issues in here, firmware-side PR needs to be land first, and device-ui's lowering queue followed by that.
Activity
github-actions commentedon Nov 27, 2026
Congratulations for your first issue
t-miura commentedon Nov 27, 2026
Root Cause Analysis
Investigated serial logs and decoded ELF backtraces from a populated T-Deck Plus (
t-deck-tft, ESP32-S3, 8 MB PSRAM) running with Wi-Fi enabled, a microSD card inserted, ~138–139 active nodes in NodeDB, and ~1,084 saved messages.max_packet_queue_sizeinPacketServerdevice-ui/source/comms/packet/PacketServer.cpp
Line 5 in fb3c069
t-deck-tft(ESP32-S3)(This also explains why clean bench flashes rarely hit the issue: a fresh NodeDB with few nodes and little/no message history queues only a few KB during both windows.)
I hereby withdraw previous analysis, huge thanks @mverch67 to point me into a right direction 🙇
Primary cause —
DataPacketpayloads stay in internal DRAM instead of PSRAM (include/util/Packet.h)CONFIG_SPIRAM_MALLOC_ALWAYSINTERNAL(16384bytes) routes standardnew/mallocallocations<= 16 KBinto internal DRAM, even when megabytes of PSRAM are free.DataPacket<PacketType>allocatesnew PacketType(...)(~768 B formeshtastic_FromRadio).PacketAPI) pushes two large packet bursts intoSharedQueue::serverQueuewhile Core 0 (DeviceUI) is busy and not callingreceive():setupUIConfig()/updateSDCard()): While Core 0 creates screens and initializes SD storage,PacketAPIqueues ~175 packets (138–139 NodeInfos + config + file manifest ≈ 135–140 KB), completely exhausting internal DRAM.restoreTextMessages()): Restoring 1,084 messages blocksViewController::receive()for ~8.6 s whilePacketAPIqueues ~167 satellite-DB replay packets (positions/telemetry/status ≈ 134 KB), exhausting internal DRAM a second time.Crash mechanism (
source/storage/FatFsStorage.cpp)FatFsStorage::mount()→FatFsStorage::release()attempts to lockstd::mutex ioLock.std::mutexlazily allocates its underlying FreeRTOS semaphore viapthread_mutex_lock()on first lock. With zero internal DRAM left, allocation fails andstd::mutex::lock()callsstd::__throw_system_error(); libgcc's unwinder (_Unwind_Find_FDE→pthread_mutex_unlock) then dereferences an uninitialized mutex handle (LoadProhibited,EXCVADDR: 0x00000003), causing a deterministic boot-loop.createStorage()barely survived by ~392 B, only for the very next mutex allocation inside FatFsf_mount()to fail withFR_INT_ERR(f_mount failed: (2) Assertion failed), and later Wave 2 starvedAsyncMessageLog::start()(xTaskCreate(..., 4096)failed withCannot start message log worker).Sanitized Example Logs (T-Deck Plus, ~139 nodes, 1,084 saved messages)
Before fix — microSD inserted + Wi-Fi enabled (deterministic boot-loop)
Decoded call chain:
Before fix — microSD removed + Wi-Fi enabled (latent DRAM OOM in Wave 1 & Wave 2)
After fix — microSD inserted + Wi-Fi enabled (consecutive manual reboots + clean shutdown)
TBD with proper fix(lower the
max_packet_queue_size)Proposed Fix Summary
max_packet_queue_sizesuggested by @mverch6730from current300, which may be a sweet spot.25, which used inPacketClient: the opposite side of thisdevice-ui/source/comms/packet/PacketClient.cpp
Line 7 in fb3c069
below was my previous idea, which i hereby withdrawn as it does not solve the root cause:
DataPacket<PacketType>(include/util/Packet.h): OnARDUINO_ARCH_ESP32builds with PSRAM (BOARD_HAS_PSRAM/CONFIG_SPIRAM), allocate packet payloads viaheap_caps_aligned_alloc(..., MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT)with aMALLOC_CAP_DEFAULTfallback and placement-new, paired with a statelessPayloadDeletercalling~PacketType()+heap_caps_free(). This eliminates both Wave 1 and Wave 2 internal-DRAM drains.FatFsStorage(include/storage/FatFsStorage.h,source/storage/FatFsStorage.cpp): Defense-in-depth replacement ofstd::mutex ioLockwith aDRAM_ATTR StaticSemaphore_tinitialized once viaxSemaphoreCreateMutexStatic, avoiding dynamic internal-heap allocation on first lock while keeping the FreeRTOS spinlock strictly in internal SRAM (never PSRAM).Initial smoke-testing on T-Deck Plus (microSD + Wi-Fi enabled across multiple manual reboots and shutdown) passed without issues. Soak testing is now ongoing; a PR will follow soon.
mverch67 commentedon Nov 27, 2026
It's not a good idea to move the temporary packet data into PSRAM memory. This only addresses symptoms rather than fixing a root cause and may not work on devices with little PSRAM capacity.
Instead I would lower the packet queue size which is filled up too fast by the firmware process which consumes all the temporary memory:
device-ui/source/comms/packet/PacketServer.cpp
Line 5 in fb3c069
Try lowering to 30 and test again.
t-miura commentedon Nov 27, 2026
@mverch67
thanks for the pointing the right path! 🙇
will try that instead of PSRAM patch anytime soon, and report back in here.
t-miura commentedon Nov 27, 2026
lowering that to
30seems to stopped boot-looping, while also stopped updating some of status screen, hmmmmm....gemini found one possible bug in firmware side,
PacketAPI::checkIsConnected()insrc/mesh/api/PacketAPI.cpp:https://github.com/meshtastic/firmware/blob/eb99f95814128956ec6e708ecde9bf564fd93efb/src/mesh/api/PacketAPI.cpp#L139
return isConnected && server->available();should be simplyreturn isConnected,as current code will mistakenly drops API connection when the queue(which is now smaller than before) is full, causing stale updates.
Copilot (tbh i rarely use this to verify something) agrees with this, and I'm now building a firmware with that change too.
if it's true and does fix all issues in here, firmware-side PR needs to be land first, and device-ui's lowering queue followed by that.
t-miura commentedon Nov 27, 2026
read the firmware side's code a bit, i think it's safe to apply that changes as:
server->available()exists inPacketAPI::sendPacket, so "backpressure" when queue is full is handled on there.and, happy to report that now all issue in here seems to be solved by these two small changes, will continue to soak some more.