heap memory asseertion faailed

accacca
Posts: 33
Joined: Mon Aug 06, 2018 4:59 pm

heap memory asseertion faailed

Postby accacca » Fri Jun 09, 2023 12:47 pm

We work with a esp module ESP32-S3-WROOM-1-N8R8 esp-idf-v5.0 framework aand visualcode IDE
In our project we have 3 tasks running
SNTP task that update clock every 20sec (high read frequency in test mode only)
MQTT client that periodically (!minute) looking for a broker Our esp32 device mqtt configuration is intentionally worng simulating connection error.
TELEGRAM task that checks every minute for new posts in the associated bot

We tested the tasks by trying them individually and then tried to run the system with all the tasks active at the same time
Random, without apparent reason, the sytem stop working with assertion failed.We can't explain the reason for this error and we can't figure out where and what we need to check for that I ask for help.
The informations provided by the log are reproduced below. It seems to crash the moment it starts a GET to check for new telegram messages. Which does not happen with the MQTT task stopped (tried for days)

Code: Select all

I (3322805) METER_MQTT: Other event id:7
E (3322855) esp-tls: [sock=54] delayed connect error: Connection reset by peer
E (3322855) transport_base: Failed to open a new connection: 32772
E (3322855) mqtt_client: Error transport connect
I (3322865) METER_MQTT: post stack 3784
I (3322865) METER_MQTT: post Free heap memory 8301668


I (3322875) METER_MQTT: MQTT_EVENT_ERROR
E (3322875) METER_MQTT: Last error reported from esp-tls: 0x8004
E (3322885) METER_MQTT: Last error captured as transport's socket errno: 0x68
I (3322895) METER_MQTT: Last errno string (Connection reset by peer)
I (3322905) METER_MQTT: post stack 3784
I (3322905) METER_MQTT: post Free heap memory 8301668


I (3322915) METER_MQTT: MQTT_EVENT_DISCONNECTED
W (3326555) METER_TLG: getupdate start
I (3326555) METER_TLG: last update 0
I (3326555) METER_TLG: GET url https://api.telegram.org/bot.../getUpdates?limit=02

assert failed: block_trim_free tlsf.c:496 (block_is_free(block) && "block must be free")


Backtrace: 0x403762be:0x3fceae70 0x40381141:0x3fceae90 0x403888e9:0x3fceaeb0 0x403874d0:0x3fceafd0 0x40386e16:0x3fceaff0 0x403765a1:0x3fceb010 0x403766e7:0x3fceb030 0x42086360:0x3fceb080 0x420863fb:0x3fceb0a0 0x42086466:0x3fceb0c0 0x4208c3c8:0x3fceb0e0 0x4208c9a6:0x3fceb110 0x42097e5a:0x3fceb180 0x42098c78:0x3fceb1a0 0x42084825:0x3fceb1c0 0x420848c8:0x3fceb1e0 0x40384939:0x3fceb210
0x403762be: panic_abort at F:/ub/hw/micro/Espressif/frameworks/esp-idf-v5.0/esp-idf/components/esp_system/panic.c:423

0x40381141: esp_system_abort at F:/ub/hw/micro/Espressif/frameworks/esp-idf-v5.0/esp-idf/components/esp_system/esp_system.c:153

0x403888e9: __assert_func at F:/ub/hw/micro/Espressif/frameworks/esp-idf-v5.0/esp-idf/components/newlib/assert.c:78

0x403874d0: block_trim_free at F:/ub/hw/micro/Espressif/frameworks/esp-idf-v5.0/esp-idf/components/heap/tlsf/tlsf.c:496
 (inlined by) block_prepare_used at F:/ub/hw/micro/Espressif/frameworks/esp-idf-v5.0/esp-idf/components/heap/tlsf/tlsf.c:579
 (inlined by) tlsf_malloc at F:/ub/hw/micro/Espressif/frameworks/esp-idf-v5.0/esp-idf/components/heap/tlsf/tlsf.c:1005

0x40386e16: multi_heap_malloc_impl at F:/ub/hw/micro/Espressif/frameworks/esp-idf-v5.0/esp-idf/components/heap/multi_heap.c:207

0x403765a1: heap_caps_malloc_base at F:/ub/hw/micro/Espressif/frameworks/esp-idf-v5.0/esp-idf/components/heap/heap_caps.c:145

0x403766e7: heap_caps_malloc_prefer at F:/ub/hw/micro/Espressif/frameworks/esp-idf-v5.0/esp-idf/components/heap/heap_caps.c:260

0x42086360: mem_malloc at F:/ub/hw/micro/Espressif/frameworks/esp-idf-v5.0/esp-idf/components/lwip/lwip/src/core/mem.c:209

0x420863fb: do_memp_malloc_pool at F:/ub/hw/micro/Espressif/frameworks/esp-idf-v5.0/esp-idf/components/lwip/lwip/src/core/memp.c:254

0x42086466: memp_malloc at F:/ub/hw/micro/Espressif/frameworks/esp-idf-v5.0/esp-idf/components/lwip/lwip/src/core/memp.c:350 (discriminator 2)

0x4208c3c8: tcp_create_segment at F:/ub/hw/micro/Espressif/frameworks/esp-idf-v5.0/esp-idf/components/lwip/lwip/src/core/tcp_out.c:168 (discriminator 2)

0x4208c9a6: tcp_write at F:/ub/hw/micro/Espressif/frameworks/esp-idf-v5.0/esp-idf/components/lwip/lwip/src/core/tcp_out.c:666

0x42097e5a: lwip_netconn_do_writemore at F:/ub/hw/micro/Espressif/frameworks/esp-idf-v5.0/esp-idf/components/lwip/lwip/src/api/api_msg.c:1774

0x42098c78: lwip_netconn_do_write at F:/ub/hw/micro/Espressif/frameworks/esp-idf-v5.0/esp-idf/components/lwip/lwip/src/api/api_msg.c:1901 (discriminator 2)

0x42084825: tcpip_thread_handle_msg at F:/ub/hw/micro/Espressif/frameworks/esp-idf-v5.0/esp-idf/components/lwip/lwip/src/api/tcpip.c:162

0x420848c8: tcpip_thread at F:/ub/hw/micro/Espressif/frameworks/esp-idf-v5.0/esp-idf/components/lwip/lwip/src/api/tcpip.c:148

0x40384939: vPortTaskWrapper at F:/ub/hw/micro/Espressif/frameworks/esp-idf-v5.0/esp-idf/components/freertos/FreeRTOS-Kernel/portable/xtensa/port.c:154



MicroController
Posts: 1136
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: heap memory asseertion faailed

Postby MicroController » Fri Jun 09, 2023 2:16 pm

assert failed: block_trim_free tlsf.c:496 (block_is_free(block) && "block must be free"
It looks like some heap corruption occurred. This may happen due to a write outside/beyond an allocated block of heap, including write-after-free.
Hard to tell what exactly the problem could be. Might be some invalid concurrent use/release of heap memory by different tasks, or any number of other reasons.

accacca
Posts: 33
Joined: Mon Aug 06, 2018 4:59 pm

Re: heap memory asseertion faailed

Postby accacca » Mon Jun 12, 2023 5:56 pm

Thanks for reply MicroController
I think I solved it...I hope....

I started my code from an example .... getUpdate function partially initializes the client configuration structure other data be random and sometime this can cause a "strange" heap error.

Code: Select all

void app_telegram_api_getUpdate(void) 
{
  esp_http_client_config_t client_config = 
  {
   .url                    = "https://api.telegram.org";
   .transport_type = HTTP_TRANSPORT_OVER_SSL;
   .event_handler  = app_telegram_http_event_handler;
   .cert_pem          = telegram_context->telegramcert_pem;
   .user_data          = telegram_context->msg_buffer;        // Pass address of local buffer to get response
  }
I changed the structure init with

Code: Select all

  esp_http_client_config_t client_config = {} ; 
....and also called memset all zero after variable declaration....

Code: Select all

  memset (&client_config, 0, sizeof(esp_http_client_config_t)) ;
Now it run from 8 hours, normally after three four hours the problem arise.
I cross my fingers.

Who is online

Users browsing this forum: No registered users and 132 guests