Search code examples
amazon-web-servicesesp32freertos

Why iot_thread stack overflow is coming when I m trying to Publisht the MQtt Message?


I m working on an ESP32 project, where I m fetching some array value from a file that is saved in spiffs. After fetching the value in sending the raw value of IR using IRsend function and after that sending MQTT acknowledgement to aws. But at publishing time I m getting the error stack overflow error. Sending the acknowledgement

{ 
    "Device_Type": "IR", 
    "Sub_Device_Type": "AC", 
    "Message_Type":"user", 
    "DeviceName":"Whirlpool", 
    "Mode":"cool_mode", 
    "Key":"NULL",
    "Status": "File fetch"
}

Publish function

void prvmqttPublishToTopic(const char *pucTopic, char *pcPayload, int size)
{
    uint32_t ulNotificationValue;
    MQTTAgentPublishParams_t xPublishParameters;
    MQTTAgentReturnCode_t xReturn;
    IotMqttPublishInfo_t publishInfo = IOT_MQTT_PUBLISH_INFO_INITIALIZER;
    IotMqttCallbackInfo_t publishComplete = IOT_MQTT_CALLBACK_INFO_INITIALIZER;

    /* Check this function is not being called before the MQTT client object has
    * been created. */
    int status = 0;
    configASSERT(xMQTTHandle != NULL);
    char pPublishPayload[PUBLISH_PAYLOAD_BUFFER_LENGTH] = {0};
    /*PUBLISH_PAYLOAD_BUFFER_LENGTH is 1000 */
    
    publishInfo.pTopicName = MQTT_TEMP_TOPIC;
    publishComplete.function = _publishOperationCompleteCallback;
    publishInfo.qos = IOT_MQTT_QOS_1;
    publishInfo.topicNameLength = sizeof(MQTT_TEMP_TOPIC); //(size_t)pcPayload;
    publishInfo.pPayload = pPublishPayload;
    publishInfo.retryMs = MQTT_TIMEOUT_MS;
    publishInfo.retryLimit = 2;
    printf("Publishing to Topic: %s \n", MQTT_TEMP_TOPIC);
    status = snprintf(pPublishPayload,
                      PUBLISH_PAYLOAD_BUFFER_LENGTH,
                      PUBLISH_PAYLOAD_FORMAT,
                      (char *)pcPayload);
    /* Check for errors from snprintf. */
    if (status < 0)
    {
        IotLogError("Failed to generate MQTT PUBLISH payload for PUBLISH %s.",
                    (char *)pPublishPayload);
    }
    else
    {
        printf("Publishing Payload 1 : %s \n", pPublishPayload);
        publishInfo.payloadLength = sizeof(pPublishPayload); //(size_t) status;
    }
    while (true)
    {

        const unsigned long inTempDelay = 0.5 * 60 * 1000UL; //3 min delay
        static unsigned long lastReadTime = 0;
        if ((getTimeInMillis() - lastReadTime) >= inTempDelay)
        {

            xReturn = IotMqtt_Publish(mqttConnection,
                                      &publishInfo, // publish structure
                                      0,
                                      &publishComplete, //call back
                                      NULL);
            break;
        }
        /* Remove compiler warnings in case configPRINTF() is not defined. */
    }
    return;
}

At the Payload parameter, I m sending the Acknowledgement which is in char format

char *cSendMsg = cJSON_Print(pJsonTree);
prvmqttPublishToTopic((const char*) topic, cSendMsg, msglen);

Log:

COMMAND SEND
9100    4600    650 1650    600 1650    650 500 650 500 650 500 650 500 600 600 650 1650    600 500 600 1700    650 1650    650 500 650 500 650 500 600 600 650 500 650 500 600 500 650 500 600 600 600 550 650 500 650 500 650 500 650 500 600 1700    650 500 650 500 650 500 600 550 650 550 600 1650    650 500 650 500 600 550 600 550 600 550 650 500 650 550 600 550 600 550 600 550 600 550 600 550 600 550 600 550 600 550 650 500 600 . 
Raw Length::[ 99 ]
Sending Raw Data 
send-> 18 
***ERROR*** A stack overflow in task iot_thread has been detected.
abort() was called at PC 0x4008cf00 on core 0
0x4008cf00: vApplicationStackOverflowHook at /home/horsemann/Desktop/WorkSpace/TestingRepo/vendors/espressif/esp-idf/components/esp32/panic.c:122


ELF file SHA256: 1d7a8ac90b11abe3a72412eb4c5ebf88adfa17c45d1ddbf29fe991a39c87b645

Backtrace: 0x4008cd18:0x3ffd6500 0x4008cee9:0x3ffd6520 0x4008cf00:0x3ffd6540 0x40093da1:0x3ffd6560 0x400952b0:0x3ffd6580 0x40095266:0x0000000b
0x4008cd18: invoke_abort at /home/horsemann/Desktop/WorkSpace/TestingRepo/vendors/espressif/esp-idf/components/esp32/panic.c:136

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

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

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

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

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

I did not understand why its happening?


Solution

  • While creating the task iot_thread (presumably by calling xTaskCreate() or xTaskCreatePinnedToCore()) you've allocated insufficient stack space. Since you haven't posted the code where you create it, I can't be more specific. Start by increasing your current stack 2 times, see if it still crashes. If that doesn't help, repeat.

    The usual approach for determining a task's stack size is to set the stack very large (perhaps 8-16 KiB) and let the task run its more stack-intensive stuff for a while. Then use the task monitoring functionality in vTaskGetInfo() to see how much unused stack it has. Finally you can reduce the stack size to the actually used amount plus a sufficient reserve. I tend to keep 1-2 KiB of stack in reserve.