Search code examples
stackembeddedesp32freertos

why "A stack overflow in task iot_thread has been detected" is coming continuously?


I'm working on an aws/amazon-freertos project. In there I found some unusual error "A stack overflow in task iot_thread has been detected".

Many time I got this error and somehow I managed to remove it by changing the code.

I just want to know what this error means actually?

As per what I know, it simply means that the iot_thread ask stack size is not sufficient. So it's getting overflow.

Is this the only reason why this error comes or can there be another reason for this?

If yes then where should I increase the stack size of the iot_thread task?

Full Log:

***ERROR*** A stack overflow in task iot_thread has been detected.
abort() was called at PC 0x4008cf94 on core 0
0x4008cf94: vApplicationStackOverflowHook at /home/horsemann/Desktop/WorkSpace/TestingRepo/vendors/espressif/esp-idf/components/esp32/panic.c:122


ELF file SHA256: 5252c38b96a3325472de5cf0f1bbc2cae90ffda1a169e71823c5aebbaa4fce2d

Backtrace: 0x4008cdac:0x3ffd74c0 0x4008cf7d:0x3ffd74e0 0x4008cf94:0x3ffd7500 0x40093e35:0x3ffd7520 0x400953d4:0x3ffd7540 0x4009538a:0x3ffd7560 0x401390e5:0x3ffc81bc
0x4008cdac: invoke_abort at /home/horsemann/Desktop/WorkSpace/TestingRepo/vendors/espressif/esp-idf/components/esp32/panic.c:136

0x4008cf7d: abort at /home/horsemann/Desktop/WorkSpace/TestingRepo/vendors/espressif/esp-idf/components/esp32/panic.c:171

0x4008cf94: vApplicationStackOverflowHook at /home/horsemann/Desktop/WorkSpace/TestingRepo/vendors/espressif/esp-idf/components/esp32/panic.c:122

0x40093e35: vTaskSwitchContext at /home/horsemann/Desktop/WorkSpace/TestingRepo/freertos_kernel/tasks.c:5068

0x400953d4: _frxt_dispatch at /home/horsemann/Desktop/WorkSpace/TestingRepo/freertos_kernel/portable/ThirdParty/GCC/Xtensa_ESP32/portasm.S:406

0x4009538a: _frxt_int_exit at /home/horsemann/Desktop/WorkSpace/TestingRepo/freertos_kernel/portable/ThirdParty/GCC/Xtensa_ESP32/portasm.S:206

Solution

  • It simply means that the iot_thread ask stack size is not sufficient. [...] Is this the only reason why this error comes or can there be another reason for this?

    Either it is insufficient or your stack usage is excessive (due to recursion error or instantiation of instantiation of large objects or arrays. Either way the cause is the same. Whether it is due insufficient stack or excessive stack usage is a matter of design an intent.

    If yes then where should I increase the stack size of the iot_thread task?

    The stack for a thread is assigned in the task creation function. For a dynamically allocated stack that would be the xTaskCreate() call usStackDepth parameter:

     BaseType_t xTaskCreate(    TaskFunction_t pvTaskCode,
                                const char * const pcName,
                                configSTACK_DEPTH_TYPE usStackDepth, // <<<<<<<<<<
                                void *pvParameters,
                                UBaseType_t uxPriority,
                                TaskHandle_t *pxCreatedTask
                              );
    

    and for a statically allocated stack the xTaskCreateStatic() ulStackDepth and puxStackBuffer().

     TaskHandle_t xTaskCreateStatic( TaskFunction_t pxTaskCode,
                                     const char * const pcName,
                                     const uint32_t ulStackDepth,         //<<<<<<
                                     void * const pvParameters,
                                     UBaseType_t uxPriority,
                                     StackType_t * const puxStackBuffer,  //<<<<<<<
                                     StaticTask_t * const pxTaskBuffer );
    

    In some case you might also use xTaskCreateRestrictedStatic() but that is more complicated - read the documentation if that is what you are doing.

    Appropriate allocation of task stacks is fundamental to any RTOS application design. You need to read the documentation and understand the concepts.

    The FreeRTOS API is extensively documented at https://www.freertos.org/a00106.html (with plenty of documentation and tutorial content at https://www.freertos.org/RTOS.html besides. There really is no substitute for reading the documentation.