CoreMQTT MQTT_ProcessLoop processing time


Does MQTT_ProcessLoop have a fixed processing time? I am trying to run the OTA in the background on my device (ESP32-S3) and timing this function gives me around 1 second for each call but in some cases it can be 4000-5000 microseconds. Can this be lowered or is this inherent with the CoreMQTT library?


Hello Kevin,

The processing time depends on the amount on incoming data. As long as there is data coming in, the process loop function will NOT exit and try and process all the data.

But, if there is no incoming data, then the function should exit quickly. But for that to happen, you MUST have set the socket to be non-blocking. Because if there is no data and the socket is blocking, then the function call will be stuck waiting for the socket to either return with data or timeout. See lwip_fcntl documentation here.

Can you please take a look at your application and verify that the underlying socket was set to be non-blocking?

If the socket is indeed set to non-blocking, then can you write back to us and we can help you find where the bug is.


Hello Aniruddha,

Thank you for your reply. I am using this library for OTA on my device, esp-aws-iot. I basing my OTA off the demo application and I have my listening procedure running in a task in my application which has the call for MQTT_ProcessLoop and I decided to add some logging within the function and noticed this:
Screenshot 2023-06-23 112956

The mt proc printf is time in microseconds. When there is no bytes it calls the keep alive block in the MQTT_ProcessLoop function. I decided to modify the PACKET_TX_TIMEOUT_MS in the handleKeepAlive function to a lower value (e.g. 0 or 1) and am still getting the 1 second processing time for no bytes. handleKeepAlive:

Screenshot 2023-06-23 120839

I have set my keep alive interval to 5 as well and seeing the same processing time.

As for the lwip, in my application I do not have any specific place where I am using lwip and setting sockets configurations. For Espressif’s config it doesn’t look to have any options for to set lwip’s sockets to non blocking.

Hello Kevin,

Reducing the PACKET_TX_TIMEOUT_MS will not have any impact on the blocking time of the MQTT_ProcessLoop function. All it does is send less/more ping depending on the configured value.

The solution is to make the socket non-blocking.

Can you try setting the non_block field in this file here?
That variable is supposed to make the socket non-blocking.
You can do that by adding the following line:

        .use_secure_element = pxNetworkContext->use_secure_element,
        .ds_data = pxNetworkContext->ds_data,
        .clientkey_buf = ( const unsigned char* )( pxNetworkContext->pcClientKey ),
        .clientkey_bytes = pxNetworkContext->pcClientKeySize,
        .timeout_ms = 1000,
+       .non_block = 1;   //This makes the socket non blocking.

See the documentation in the code here: esp-idf/components/esp-tls/esp_tls.h.

Let me know if that works for you.