bertmelis / espMqttClient

MQTT 3.1.1 client library for the Espressif devices ESP8266 and ESP32 on the Arduino framework.
https://www.emelis.net/espMqttClient/
MIT License
101 stars 21 forks source link

[Question / Feature Reqest] track free heap to prevent crash #159

Closed lumapu closed 2 weeks ago

lumapu commented 3 months ago

Do not use to discuss topics!

Describe the bug The ESP32 crashes from time to time once Wifi is very poor but data has to be sent continously.

Which platform, esp8266 or esp32?

Bug report (ESP - Coredump, captured from coredump-partition)

Coredump ``` =============================================================== ==================== ESP32 CORE DUMP START ==================== Crashed task handle: 0x3ffbd4d4, name: '', GDB name: 'process 1073468628' ================== CURRENT THREAD REGISTERS =================== exccause 0x1d (StoreProhibitedCause) excvaddr 0x0 epc1 0x40146c11 epc2 0x0 epc3 0x0 epc4 0x0 epc5 0x40082e0e epc6 0x0 eps2 0x0 eps3 0x0 eps4 0x0 eps5 0x60720 eps6 0x0 [New process 1073468628] [New process 1073548364] [New process 1073460608] [New process 1073427564] [New process 1073443660] [New process 1073601224] [New process 1073445360] [New process 1073431632] [New process 1073436104] [New process 1073610016] [New process 1073605768] [New process 1073447304] [New process 1073409856] [New process 1073466120] [Current thread is 1 (process 1073468628)] ==================== CURRENT THREAD STACK ===================== ======================== THREADS INFO ========================= pc 0x40083cb4 0x40083cb4 lbeg 0x4008a29d 1074307741 lend 0x4008a2bf 1074307775 lcount 0x0 0 sar 0x8 8 ps 0x60021 393249 threadptr br scompare1 acclo acchi m0 m1 m2 m3 expstate f64r_lo f64r_hi f64s fcr fsr a0 0x8008c990 -2146907760 a1 0x3ffbec9c 1073474716 a2 0x3ffbed06 1073474822 a3 0x3ffbed33 1073474867 a4 0xa 10 a5 0x2000 8192 a6 0x3ffbec84 1073474692 a7 0x60023 393251 a8 0x0 0 a9 0x1 1 a10 0x3ffbecea 1073474794 a11 0x3ffbecea 1073474794 a12 0xa 10 a13 0x20 32 a14 0x0 0 a15 0xa03fc000 -1606434816 Retrying reading threads information... Retrying reading threads information... TCB NAME PRIO C/B STACK USED/FREE ---------- ---------------- -------- ---------------- 0x3ffbd4d4 Corrupted TCB data 0x3ffd0c4c IDLE1073469616/1073468988 79812/78420 0x3ffbb580 1073459600/1073455472 1073460664/88 0x3ffb346c 1073427056/1073424476 1073427619/84 0x3ffb734c 1073443216/1073442620 1073443698/84 0x3ffddac8 1073600752/1073584824 1073601256/80 0x3ffb79f0 1073444912/1073444320 1073445413/88 0x3ffb4450 1073431104/1073429056 1073431664/88 0x3ffb55c8 1073435648/1073431992 1073436140/80 0x3ffdfd20 1073615920/1073612268 1073610053/6280 0x3ffdec88 1073619984/1073616380 1073605821/14640 0x3ffb8188 1073422224/1073414944 1073447359/24240 0x3ffaef40 1073556368/10735501881073409911/146920 0x3ffbcb08 1073465680/1073461496 1073466153/80 ==================== THREAD 1 (TCB: 0x3ffbd4d4, name: '') ===================== ==================== THREAD 2 (TCB: 0x3ffd0c4c, name: 'IDLE') ===================== #0 0x40083cb4 in panic_abort (details=0x3ffbed06 "abort() was called at PC 0x401174f4 on core 0") at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/esp_system/panic.c:408 #1 0x4008c990 in esp_system_abort (details=0x3ffbed06 "abort() was called at PC 0x401174f4 on core 0") at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/esp_system/esp_system.c:137 #2 0x40092b40 in abort () at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/newlib/abort.c:46 #3 0x401174f7 in task_wdt_isr (arg=) at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/esp_system/task_wdt.c:176 #4 0x40085128 in _xt_lowint1 () at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/freertos/port/xtensa/xtensa_vectors.S:1114 Backtrace stopped: previous frame inner to this frame (corrupt stack?) ==================== THREAD 3 (TCB: 0x3ffbb580, name: '') ===================== #0 0x4019c3f2 in esp_pm_impl_waiti () at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/esp_pm/pm_impl.c:849 #1 0x40117d28 in esp_vApplicationIdleHook () at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/esp_system/freertos_hooks.c:63 #2 0x4008dfef in prvIdleTask (pvParameters=) at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/freertos/tasks.c:3987 ==================== THREAD 4 (TCB: 0x3ffb346c, name: '') ===================== #0 0x40083bf4 in esp_crosscore_int_send_yield (core_id=1) at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/esp_system/crosscore_int.c:145 #1 0x4008da7c in xQueueSemaphoreTake (xQueue=0x3ffd6174, xTicksToWait=) at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/hal/esp32/include/hal/cpu_ll.h:39 #2 0x40133f64 in sys_arch_sem_wait (sem=0x3ffb2758, timeout=1000) at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/lwip/port/esp32/freertos/sys_arch.c:188 #3 0x401246aa in lwip_select (maxfdp1=49, readset=0x0, writeset=0x3ffbb410, exceptset=0x0, timeout=) at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/lwip/lwip/src/api/sockets.c:2153 #4 0x4011a330 in esp_vfs_select (nfds=49, readfds=0x0, writefds=0x3ffbb410, errorfds=0x0, timeout=0x3ffbb418) at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/vfs/vfs.c:1023 #5 0x400f5d01 in WiFiClient::write (this=0x3ffc84f8 , buf=0x3ffdf620 , size=41) at /home/runner/.platformio/packages/framework-arduinoespressif32/libraries/WiFi/src/WiFiClient.cpp:411 #6 0x40101ee4 in espMqttClientInternals::ClientSync::write (this=0x3ffc84f4 , buf=0x3ffdf620 , size=41) at .pio/libdeps/esp32-wroom32-de/espMqttClient/src/Transport/ClientSync.cpp:50 #7 0x401002b3 in MqttClient::_sendPacket (this=0x3ffc7d8c ) at .pio/libdeps/esp32-wroom32-de/espMqttClient/src/MqttClient.cpp:362 #8 0x40100f91 in MqttClient::_checkOutbox (this=0x3ffc7d8c ) at .pio/libdeps/esp32-wroom32-de/espMqttClient/src/MqttClient.cpp:346 #9 0x40101337 in MqttClient::loop (this=0x3ffc7d8c ) at .pio/libdeps/esp32-wroom32-de/espMqttClient/src/MqttClient.cpp:262 #10 0x401013b0 in MqttClient::_loop (c=0x3ffc7d8c ) at .pio/libdeps/esp32-wroom32-de/espMqttClient/src/MqttClient.cpp:326 ==================== THREAD 5 (TCB: 0x3ffb734c, name: '') ===================== #0 0x40083bf4 in esp_crosscore_int_send_yield (core_id=0) at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/esp_system/crosscore_int.c:145 #1 0x4008d916 in xQueueReceive (xQueue=0x3ffb2778, pvBuffer=0x3ffb33cc, xTicksToWait=) at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/hal/esp32/include/hal/cpu_ll.h:39 #2 0x40134062 in sys_arch_mbox_fetch (mbox=, msg=0x3ffb33cc, timeout=250) at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/lwip/port/esp32/freertos/sys_arch.c:330 #3 0x40124a5e in tcpip_timeouts_mbox_fetch (mbox=, msg=) at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/lwip/lwip/src/api/tcpip.c:110 #4 tcpip_thread (arg=) at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/lwip/lwip/src/api/tcpip.c:148 ==================== THREAD 6 (TCB: 0x3ffddac8, name: '') ===================== #0 0x40083bf4 in esp_crosscore_int_send_yield (core_id=0) at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/esp_system/crosscore_int.c:145 #1 0x4008da7c in xQueueSemaphoreTake (xQueue=0x3ffb6ed8, xTicksToWait=) at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/hal/esp32/include/hal/cpu_ll.h:39 #2 0x400857db in ipc_task (arg=) at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/esp_ipc/src/esp_ipc.c:54 ==================== THREAD 7 (TCB: 0x3ffb79f0, name: '') ===================== #0 0x40083bf4 in esp_crosscore_int_send_yield (core_id=1) at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/esp_system/crosscore_int.c:145 #1 0x4008d916 in xQueueReceive (xQueue=0x3ffd99d4, pvBuffer=0x3ffdda24, xTicksToWait=) at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/hal/esp32/include/hal/cpu_ll.h:39 #2 0x400f51a2 in _get_async_event (e=) at .pio/libdeps/esp32-wroom32-de/AsyncTCP-esphome/src/AsyncTCP.cpp:117 #3 _async_service_task (pvParameters=) at .pio/libdeps/esp32-wroom32-de/AsyncTCP-esphome/src/AsyncTCP.cpp:193 ==================== THREAD 8 (TCB: 0x3ffb4450, name: '') ===================== #0 0x40083bf4 in esp_crosscore_int_send_yield (core_id=1) at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/esp_system/crosscore_int.c:145 #1 0x4008da7c in xQueueSemaphoreTake (xQueue=0x3ffb757c, xTicksToWait=) at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/hal/esp32/include/hal/cpu_ll.h:39 #2 0x400857db in ipc_task (arg=) at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/esp_ipc/src/esp_ipc.c:54 ==================== THREAD 9 (TCB: 0x3ffb55c8, name: '') ===================== #0 0x40083bf4 in esp_crosscore_int_send_yield (core_id=0) at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/esp_system/crosscore_int.c:145 #1 0x4008d916 in xQueueReceive (xQueue=0x3ffb3778, pvBuffer=0x3ffb4370, xTicksToWait=) at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/hal/esp32/include/hal/cpu_ll.h:39 #2 0x401a3458 in esp_event_loop_run (event_loop=0x3ffb3750, ticks_to_run=4294967295) at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/esp_event/esp_event.c:566 #3 0x401a35df in esp_event_loop_run_task (args=0x3ffb3750) at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/esp_event/esp_event.c:115 ==================== THREAD 10 (TCB: 0x3ffdfd20, name: '') ===================== #0 0x40083bf4 in esp_crosscore_int_send_yield (core_id=1) at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/esp_system/crosscore_int.c:145 #1 0x4008d916 in xQueueReceive (xQueue=0x3ffb366c, pvBuffer=0x3ffb552c, xTicksToWait=) at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/hal/esp32/include/hal/cpu_ll.h:39 #2 0x400f821c in _arduino_event_task (arg=) at /home/runner/.platformio/packages/framework-arduinoespressif32/libraries/WiFi/src/WiFiGeneric.cpp:304 ==================== THREAD 11 (TCB: 0x3ffdec88, name: '') ===================== #0 0x40083bf4 in esp_crosscore_int_send_yield (core_id=0) at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/esp_system/crosscore_int.c:145 #1 0x4008d916 in xQueueReceive (xQueue=0x3ffd0b50, pvBuffer=0x3ffe155c, xTicksToWait=) at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/hal/esp32/include/hal/cpu_ll.h:39 #2 0x401057a9 in _udp_task (pvParameters=) at /home/runner/.platformio/packages/framework-arduinoespressif32/libraries/AsyncUDP/src/AsyncUDP.cpp:132 ==================== THREAD 12 (TCB: 0x3ffb8188, name: '') ===================== #0 0x40083bf4 in esp_crosscore_int_send_yield (core_id=0) at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/esp_system/crosscore_int.c:145 #1 0x4008d916 in xQueueReceive (xQueue=0x3ffd70e4, pvBuffer=0x3ffe2560, xTicksToWait=) at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/hal/esp32/include/hal/cpu_ll.h:39 #2 0x4013d5c2 in _mdns_service_task (pvParameters=) at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/mdns/mdns.c:4639 ==================== THREAD 13 (TCB: 0x3ffaef40, name: '') ===================== #0 0x40083bf4 in esp_crosscore_int_send_yield (core_id=1) at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/esp_system/crosscore_int.c:145 #1 0x4008da7c in xQueueSemaphoreTake (xQueue=0x3ffba10c, xTicksToWait=) at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/hal/esp32/include/hal/cpu_ll.h:39 #2 0x40100a80 in MqttClient::publish (this=0x3ffc7d8c , topic=0x3ffc881c "", qos=0 '\000', retain=true, payload=0x3ffc85d7 "", length=3) at .pio/libdeps/esp32-wroom32-de/espMqttClient/src/MqttClient.cpp:151 #3 0x40100b7c in MqttClient::publish (this=0x3ffc7d8c , topic=0x3ffc881c "", qos=0 '\000', retain=true, payload=0x3ffc85d7 "") at .pio/libdeps/esp32-wroom32-de/espMqttClient/src/MqttClient.cpp:166 #4 0x400dd363 in PubMqtt > >::publish (this=0x3ffc7d8c , subTopic=, payload=0x3ffc85d7 "", retained=true, addTopic=, qos=0 '\000') at publisher/pubMqtt.h:211 #5 0x400dd4e0 in PubMqtt > >::setup(IApp*, cfgMqtt_t*, char const*, char const*, HmSystem<(unsigned char)16, Inverter >*, unsigned int*, unsigned int*)::{lambda(char const*, char const*, bool, unsigned char)#1}::operator()(char const*, char const*, bool, unsigned char) const (qos=, retained=, payload=, subTopic=, this=) at publisher/pubMqtt.h:66 #6 std::_Function_handler > >::setup(IApp*, cfgMqtt_t*, char const*, char const*, HmSystem<(unsigned char)16, Inverter >*, unsigned int*, unsigned int*)::{lambda(char const*, char const*, bool, unsigned char)#1}>::_M_invoke(std::_Any_data const&, char const*&&, char const*&&, bool&&, unsigned char&&) (__functor=..., __args#0=@0x3ffb217c: 0x3ffc85a6 "", __args#1=@0x3ffb2178: 0x3ffc85d7 "", __args#2=@0x3ffb2174: true, __args#3=@0x3ffb2170: 0 '\000') at /home/runner/.platformio/packages/toolchain-xtensa-esp32/xtensa-esp32-elf/include/c++/8.4.0/bits/std_function.h:297 #7 0x400e0bbe in std::function::operator()(char const*, char const*, bool, unsigned char) const (this=0x3ffc8540 , __args#0=, __args#1=, __args#2=, __args#3=) at /home/runner/.platformio/packages/toolchain-xtensa-esp32/xtensa-esp32-elf/include/c++/8.4.0/bits/std_function.h:687 #8 0x400e777c in PubMqttIvData > >::stateSend (this=0x3ffc8530 ) at /home/runner/.platformio/packages/toolchain-xtensa-esp32/xtensa-esp32-elf/include/c++/8.4.0/array:234 #9 0x400ec07b in PubMqttIvData > >::loop (this=0x3ffc8530 ) at publisher/pubMqttIvData.h:46 #10 PubMqtt > >::loop (this=0x3ffc7d8c ) at publisher/pubMqtt.h:99 #11 app::loop (this=0x3ffc39b8 ) at app.cpp:143 #12 0x400f1cd5 in loop () at main.cpp:42 #13 0x401096c4 in loopTask (pvParameters=) at /home/runner/.platformio/packages/framework-arduinoespressif32/cores/esp32/main.cpp:50 ==================== THREAD 14 (TCB: 0x3ffbcb08, name: '') ===================== #0 0x40083bf4 in esp_crosscore_int_send_yield (core_id=0) at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/esp_system/crosscore_int.c:145 #1 0x4008d916 in xQueueReceive (xQueue=0x3ffae89c, pvBuffer=0x3ffd2cd0, xTicksToWait=) at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/hal/esp32/include/hal/cpu_ll.h:39 #2 0x40122524 in queue_recv_wrapper (queue=0x3ffae89c, item=0x3ffd2cd0, block_time_tick=4294967295) at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/esp_wifi/esp32/esp_adapter.c:441 #3 0x4019f4cc in ppTask () ======================= ALL MEMORY REGIONS ======================== Name Address Size Attrs .rtc.text 0x400c0000 0x0 RW .rtc.dummy 0x3ff80000 0x0 RW .rtc.force_fast 0x3ff80000 0x0 RW .rtc.force_slow 0x50000210 0x0 RW .iram0.vectors 0x40080000 0x403 R XA .iram0.text 0x40080404 0x1419b R XA .dram0.data 0x3ffbdb60 0x5e18 RW A .ext_ram_noinit 0x3f800000 0x0 RW .noinit 0x3ffc3978 0x0 RW .ext_ram.bss 0x3f800000 0x0 RW .flash.appdesc 0x3f400020 0x100 R A .flash.rodata 0x3f400120 0x456c8 RW A .flash.text 0x400d0020 0xd4677 R XA .phyiram.21 0x401a4698 0x61 R XA .phyiram.19 0x401a46fc 0x10e R XA .phyiram.20 0x401a480c 0x9e R XA .phyiram.18 0x401a48ac 0x2d7 R XA .phyiram.0 0x401a4b84 0x2b R XA .phyiram.1 0x401a4bb0 0x7d R XA .phyiram.2 0x401a4c30 0x21e R XA .phyiram.3 0x401a4e50 0x9e R XA .phyiram.4 0x401a4ef0 0x12c R XA .phyiram.6 0x401a501c 0x9a R XA .phyiram.7 0x401a50b8 0x223 R XA .phyiram.8 0x401a52dc 0x183 R XA .phyiram.9 0x401a5460 0x8e R XA .phyiram.12 0x401a54f0 0x188 R XA .phyiram.14 0x401a5678 0xba R XA .phyiram.13 0x401a5734 0xef R XA .phyiram.15 0x401a5824 0x1cb R XA .phyiram.17 0x401a59f0 0x72 R XA .phyiram.11 0x401a5a64 0x78 R XA .phyiram.16 0x401a5adc 0xdc R XA .phyiram.23 0x401a5bb8 0x4a R XA .phyiram.24 0x401a5c04 0x31 R XA .phyiram.25 0x401a5c38 0x103 R XA .phyiram.26 0x401a5d3c 0x87 R XA .iram0.data 0x400945a0 0x0 RW .iram0.bss 0x400945a0 0x0 RW .dram0.heap_start 0x3ffcf6c8 0x0 RW .coredump.tasks.data 0x3ffbd4d4 0x158 RW .coredump.tasks.data 0x3ffbd340 0x180 RW .coredump.tasks.data 0x3ffd0c4c 0x158 RW .coredump.tasks.data 0x3ffbd8b0 0x180 RW .coredump.tasks.data 0x3ffbb580 0x158 RW .coredump.tasks.data 0x3ffbb190 0x3d0 RW .coredump.tasks.data 0x3ffb346c 0x158 RW .coredump.tasks.data 0x3ffb3270 0x1e0 RW .coredump.tasks.data 0x3ffb734c 0x158 RW .coredump.tasks.data 0x3ffb7190 0x1a0 RW .coredump.tasks.data 0x3ffddac8 0x158 RW .coredump.tasks.data 0x3ffdd8f0 0x1c0 RW .coredump.tasks.data 0x3ffb79f0 0x158 RW .coredump.tasks.data 0x3ffb7830 0x1a0 RW .coredump.tasks.data 0x3ffb4450 0x158 RW .coredump.tasks.data 0x3ffb4240 0x1f0 RW .coredump.tasks.data 0x3ffb55c8 0x158 RW .coredump.tasks.data 0x3ffb5400 0x1b0 RW .coredump.tasks.data 0x3ffdfd20 0x158 RW .coredump.tasks.data 0x3ffe1430 0x1b0 RW .coredump.tasks.data 0x3ffdec88 0x158 RW .coredump.tasks.data 0x3ffe2410 0x1e0 RW .coredump.tasks.data 0x3ffb8188 0x158 RW .coredump.tasks.data 0x3ffb1f90 0x380 RW .coredump.tasks.data 0x3ffaef40 0x158 RW .coredump.tasks.data 0x3ffd2b90 0x1d0 RW .coredump.tasks.data 0x3ffbcb08 0x158 RW .coredump.tasks.data 0x3ffbc950 0x1a0 RW ===================== ESP32 CORE DUMP END ===================== =============================================================== ```

Expected behaviour ESP has an endless uptime

To Reproduce Not that easy. Keep the ESP as far as possible from you WiFi an send continuously data from it to a broker. Depending on the environment the quality changes and on some reason the heap runs out of memory.

Example code https://github.com/lumapu/ahoy/tree/development03

Additional context My idea is to have like a monitor running which checks whether it is possilbe to accept messages to be transfered depending on free heap. It would be nice to have some kind of threshold which can be configured (maybe 0 as default, then the current behaviour won't change). I can try to implement this q'n'd in my surronding publish method and test it. I have one ESP which shows this issue almost every day.

lumapu commented 3 months ago

it is really similar to this issue:

Version 1.7.0 of this library is uesd

bertmelis commented 3 months ago

Do you want to test before publishing packets or test before receiving packets?

Before placing new packets on the heap, a check is done with a configurable free heap value: https://github.com/bertmelis/espMqttClient/blob/main/src/Packets/Packet.cpp#L315

You could also the the new memory pool feature which automatically limits the amount of memory that is used by this lib. This is experimental however.

The numbers in your dump also don't seem to be correct. Default stack size for the mqtt task is only 5120.

bertmelis commented 3 months ago

Now that I'm thinking about it. I can check for free heap before writing out data. I suppose the underlying libraries (WiFiClient etc) also use heap memory to allocate the outgoing data buffers. I could add a check before trying to write like this:

// MqttClient.cpp line 353
int MqttClient::_sendPacket() {
  // check before trying to send
  if (EMC_GET_FREE_MEMORY() < EMC_MIN_FREE_MEMORY) {
    return 0;
  }

  OutgoingPacket* packet = _outbox.getCurrent();

  size_t written = 0;
  if (packet) {
    size_t wantToWrite = packet->packet.available(_bytesSent);
    if (wantToWrite == 0) {
      return 0;
    }
    written = _transport->write(packet->packet.data(_bytesSent), wantToWrite);
    packet->timeSent = millis();
    _lastClientActivity = millis();
    _bytesSent += written;
    emc_log_i("tx %zu/%zu (%02x)", _bytesSent, packet->packet.size(), packet->packet.packetType());
  }
  return written;
}

Ideally, the underlying libraries don't crash on low memory but accept the situation and return some sensible value.

lumapu commented 3 months ago

Thank you for your answers. What happens if I set EMC_USE_MEMPOOL 1, the pool reaches its full status and I try to send out packets? Am I right that I will get 0 as return value of publish(...)?

bertmelis commented 3 months ago

Yes, the same thing will happen like when you are running out of regular heap: publish will return 0.

The pool only tries to prevent heap fragmentation and puts a stricter limit on memory usage. Mind that the pool itself still has fragmentation (although it will be less). And only limited real world testing has been done so beware...

ajen2420 commented 2 weeks ago

I solve this problem by setting a condition that if there is connection with mqtt server then it will publish a message. Simple I tested it with esp32 uptime 3days.. Can be more obviously.