为什么在我尝试发布 MQtt 消息时出现 iot_thread 堆栈溢出?

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

我正在做一个 ESP32 项目,我正在从一个保存在 spiffs 中的文件中获取一些数组值。 在使用 IRsend 函数发送 IR 的原始值时获取值,然后向 aws 发送 MQTT 确认。但是在发布时我收到错误堆栈溢出错误。 发送确认

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

发布功能

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;
}

在 Payload 参数中,我发送了字符格式的确认

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

日志:

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: vApplicationWhosebugHook 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: vApplicationWhosebugHook 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

我不明白为什么会这样?

在创建任务 iot_thread 时(大概是通过调用 xTaskCreate() or xTaskCreatePinnedToCore()),您分配的堆栈 space 不足。由于您尚未在创建代码的位置发布代码,因此我不能更具体。首先将当前堆栈增加 2 倍,看看它是否仍然崩溃。如果这没有帮助,请重复。

确定任务堆栈大小的常用方法是将堆栈设置得非常大(可能为 8-16 KiB)并让任务 运行 暂时使用堆栈密集型内容。然后使用 vTaskGetInfo() 中的任务监控功能来查看它有多少未使用的堆栈。最后,您可以将堆栈大小减少到实际使用的数量加上足够的储备。我倾向于保留 1-2 KiB 的堆栈。