mlesniew / PicoMQTT

ESP MQTT client and broker library
GNU Lesser General Public License v3.0
229 stars 25 forks source link

inefficient use of RAM. (BearSSL) #43

Closed justbendev closed 1 week ago

justbendev commented 2 weeks ago

Hello :wave:

This is just a quick report, i unfortunately don't have any time yet to investigate the matter.

Using an ESP8266 with PicoMQTT v1.1.1-1-gf49428c.

When you are working in projects that have really small available RAM the MQTT Auto reconnect can crash the microcontroller by allocating 2000 bytes for each Auto reconnect failed attempt. When a successful connection is made the buffers are correctly freed.

In my specific case i only have 6000 Bytes free in normal operating state (Already connected to MQTT Server) so more than 2 auto reconnect in row result in a crash.

Please note that im using SSL (BearSSL::WiFiClientSecure) and the issue could be specific to this use case.

Anyways i will investigate the matter when i have some time free, and will report back my findings here, and will suggest a pull if a bug is found.

Have a great day :+1:

justbendev commented 2 weeks ago

This comment is for logging purposes only

DECODED STACK After Crash (Exception) ``` Exception (29): epc1=0x4000df64 epc2=0x00000000 epc3=0x00000000 excvaddr=0x00000000 depc=0x00000000 StoreProhibited: A store referenced a page mapped with an attribute that does not permit stores 0x4024d4eb in ieee80211_setup_ratetable at ??:? 0x4024d480 in ieee80211_setup_ratetable at ??:? 0x4024d427 in ieee80211_phy_init at ??:? 0x4024c57c in cnx_update_bss_more at ??:? 0x4024c5a5 in cnx_update_bss_more at ??:? 0x4024a004 in scan_parse_beacon at ??:? 0x40100934 in timer1_isr_handler at ??:? 0x4025e4fc in DefFreqCalTimerCB at ??:? 0x4024635d in ieee80211_parse_beacon at ??:? 0x4024acff in sta_input at ??:? 0x4024a76e in sta_input at ??:? 0x4025de8f in pp_tx_idle_timeout at ??:? 0x4025d753 in ppPeocessRxPktHdr at ??:? 0x402161de in loop_task(ETSEventTag*) at core_esp8266_main.cpp:? 0x40105c45 in call_user_start_local at ??:? 0x40105c4b in call_user_start_local at ??:? 0x4010000d in call_user_start at ??:? 0x401000ab in app_entry_redefinable at ??:? 0x40252684 in cont_ret at cont.S.o:? 0x40252635 in cont_continue at cont.S.o:? 0x40100ae4 in digitalWrite at ??:? 0x401003a0 in dscKeybusInterface::dscClockInterrupt() at ??:? 0x40100c4c in interrupt_handler at ??:? 0x40100b80 in interrupt_handler at ??:? 0x40231b61 in br_sha2small_round at /home/earle/src/esp-quick-toolchain/arduino/tools/sdk/ssl/bearssl/src/hash/sha2small.c:88 (discriminator 3) 0x40231b34 in br_sha2small_round at /home/earle/src/esp-quick-toolchain/arduino/tools/sdk/ssl/bearssl/src/hash/sha2small.c:86 0x40100c4c in interrupt_handler at ??:? 0x401008ec in timer1_isr_handler at ??:? 0x40100934 in timer1_isr_handler at ??:? 0x40100b80 in interrupt_handler at ??:? 0x40231c9d in br_sha2small_round at /home/earle/src/esp-quick-toolchain/arduino/tools/sdk/ssl/bearssl/src/hash/sha2small.c:100 (discriminator 3) 0x40231b34 in br_sha2small_round at /home/earle/src/esp-quick-toolchain/arduino/tools/sdk/ssl/bearssl/src/hash/sha2small.c:86 0x401008ec in timer1_isr_handler at ??:? 0x40100934 in timer1_isr_handler at ??:? 0x40231b6a in br_sha2small_round at /home/earle/src/esp-quick-toolchain/arduino/tools/sdk/ssl/bearssl/src/hash/sha2small.c:88 (discriminator 3) 0x40231b34 in br_sha2small_round at /home/earle/src/esp-quick-toolchain/arduino/tools/sdk/ssl/bearssl/src/hash/sha2small.c:86 0x401008ec in timer1_isr_handler at ??:? 0x40100c4c in interrupt_handler at ??:? 0x401008ec in timer1_isr_handler at ??:? 0x40100934 in timer1_isr_handler at ??:? 0x401008ec in timer1_isr_handler at ??:? 0x401007c7 in ets_intr_unlock at ??:? 0x402601bc in esf_buf_alloc at ??:? 0x40100c4c in interrupt_handler at ??:? 0x40100b80 in interrupt_handler at ??:? 0x40246eb7 in ieee80211_output_pbuf at ??:? 0x40224bb7 in new_linkoutput at /local/users/gauchard/arduino/arduino_esp8266/origin/tools/sdk/lwip2/builder/glue-lwip/lwip-git.c:272 0x40246f87 in ieee80211_output_pbuf at ??:? 0x4025e8a7 in pp_attach at ??:? 0x4025e8f6 in pp_attach at ??:? 0x401007e4 in ets_post at ??:? 0x401028ac in pp_post at ??:? 0x4025d9b7 in ppTxPkt at ??:? 0x40105a91 in lmacTxFrame at ??:? 0x401007e4 in ets_post at ??:? 0x40102cdd in rcUpdateTxDone at ??:? 0x401028ac in pp_post at ??:? 0x40105b2b in lmacTxFrame at ??:? 0x40104d06 in lmacRecycleMPDU at ??:? 0x40104fc5 in lmacRecycleMPDU at ??:? 0x401007e4 in ets_post at ??:? 0x40105579 in lmacMSDUAged at ??:? 0x401028ac in pp_post at ??:? 0x40105b43 in lmacRxDone at ??:? 0x401003a0 in dscKeybusInterface::dscClockInterrupt() at ??:? 0x40103437 in rcReachRetryLimit at ??:? 0x40100ae4 in digitalWrite at ??:? 0x40103614 in rcReachRetryLimit at ??:? 0x40103ace in wDev_ProcessFiq at ??:? 0x40100b80 in interrupt_handler at ??:? 0x401037f0 in wDev_ProcessFiq at ??:? 0x4020c1ae in BearSSL::WiFiClientSecureCtx::_run_until(unsigned int, bool) at ??:? 0x4020c1ae in BearSSL::WiFiClientSecureCtx::_run_until(unsigned int, bool) at ??:? 0x40101002 in umm_free_core at umm_malloc.cpp:? 0x40229182 in tcp_write at /local/users/gauchard/arduino/arduino_esp8266/origin/tools/sdk/lwip2/builder/lwip2-src/src/core/tcp_out.c:666 0x402162fa in yield at ??:? 0x4020ae2a in WiFiClient::available() at ??:? 0x4020c262 in BearSSL::WiFiClientSecureCtx::_run_until(unsigned int, bool) at ??:? 0x40233820 in br_ssl_engine_closed at /home/earle/src/esp-quick-toolchain/arduino/tools/sdk/ssl/bearssl/src/inner.h:2211 (inlined by) jump_handshake at /home/earle/src/esp-quick-toolchain/arduino/tools/sdk/ssl/bearssl/src/ssl/ssl_engine.c:1081 0x40234978 in br_ssl_hs_client_run at /home/earle/src/esp-quick-toolchain/arduino/tools/sdk/ssl/bearssl/src/ssl/ssl_hs_client.c:913 0x4020c2db in BearSSL::WiFiClientSecureCtx::_wait_for_handshake() at ??:? 0x4020c4c7 in BearSSL::WiFiClientSecureCtx::_connectSSL(char const*) at ??:? 0x4020b62a in WiFiClient::connect(IPAddress, unsigned short) at ??:? 0x4020c569 in BearSSL::WiFiClientSecureCtx::connect(char const*, unsigned short) at ??:? 0x4021c0bc in DhcpServer::isRunning() const at ??:? 0x4021c0bc in DhcpServer::isRunning() const at ??:? 0x4020de30 in PicoMQTT::BasicClient::connect(char const*, unsigned short, char const*, char const*, char const*, char const*, char const*, unsigned int, unsigned char, bool, bool, PicoMQTT::ConnectReturnCode*) at ??:? 0x401008ec in timer1_isr_handler at ??:? 0x40100934 in timer1_isr_handler at ??:? 0x4020e098 in PicoMQTT::Client::loop() at ??:? 0x4020e098 in PicoMQTT::Client::loop() at ??:? 0x401007d0 in ets_post at ??:? 0x40100b80 in interrupt_handler at ??:? 0x401007e4 in ets_post at ??:? 0x40213f20 in HardwareSerial::write(unsigned char const*, unsigned int) at ??:? 0x4020e148 in PicoMQTT::Client::loop() at ??:? 0x402162fa in yield at ??:? 0x40207fde in loop at ??:? ```
justbendev commented 1 week ago

Bug traced back to how BearSSL work's nothing to do with this library so closing :+1: