ESP Core MQTT Agent blocks for 3s when sending MQTT messages
Posted: Tue Feb 21, 2023 9:37 pm
Hi,
We're using esp-aws-iot libraries (https://github.com/espressif/esp-aws-iot) to connect our device to AWS. We are using release/202012.04-LTS.
Specifically, I am using the coreMQTT-Agent library to connect to AWS and send device shadow updates and telemetry messages.
I am observing a strange behavior where it takes the MQTT Agent typically 3 seconds to send each MQTT message.
We have an MQTT agent task which connects to the broker with "xTlsConnect", then calls "MQTTAgent_CommandLoop" to start the MQTT Agent.
Note it uses the "xTlsConnect" implementation in esp-aws-iot\libraries\coreMQTT\port\network_transport\network_transport.c, which is what the examples used.
When I send several agent commands (e.g. with MQTTAgent_Publish), the first one will be processed quickly (i.e. data sent to the server) but subsequent commands will be delayed by up to 3 seconds before the agent sends them.
When I traced the code and added log messages, I found this:
* MQTT agent task receives commands from a queue and processes them in processCommand(core_mqtt_agent.c)
* After calling the command function, MQTT_ProcessLoop is called (if commandOutParams.runProcessLoop is TRUE, e.g. for publish commands)
* MQTT_ProcessLoop calls receiveSingleIteration then MQTT_GetIncomingPacketTypeAndLength
* MQTT_GetIncomingPacketTypeAndLength calls the transport interface "readFunc" to read from the TLS connection
* "readFunc" maps to espTlsTransportRecv in "network_transport.c"
* If there is no data waiting, espTlsTransportRecv will BLOCK for 3000 mS before returning.
It looks to me like this call is expected to be a non-blocking read? However, the "non_block" option is NOT specified in the esp_tls_cfg_t passed to esp_tls_conn_new_sync (in xTlsConnect).
I'm confused by this. It looks like it will almost always block for 3 seconds per message when several MQTT messages are sent, and this is what I observe. It became very obvious when I tried to request a firmware update file (in 1 kb chunks) via MQTT file stream, using QOS 0, and each request took 3 seconds, which was spent waiting for the agent to publish the request message.
Am I using this interface incorrectly? Or is it supposed to have "non_block = 1" in the esp_tls_cfg_t?
We're using esp-aws-iot libraries (https://github.com/espressif/esp-aws-iot) to connect our device to AWS. We are using release/202012.04-LTS.
Specifically, I am using the coreMQTT-Agent library to connect to AWS and send device shadow updates and telemetry messages.
I am observing a strange behavior where it takes the MQTT Agent typically 3 seconds to send each MQTT message.
We have an MQTT agent task which connects to the broker with "xTlsConnect", then calls "MQTTAgent_CommandLoop" to start the MQTT Agent.
Note it uses the "xTlsConnect" implementation in esp-aws-iot\libraries\coreMQTT\port\network_transport\network_transport.c, which is what the examples used.
When I send several agent commands (e.g. with MQTTAgent_Publish), the first one will be processed quickly (i.e. data sent to the server) but subsequent commands will be delayed by up to 3 seconds before the agent sends them.
When I traced the code and added log messages, I found this:
* MQTT agent task receives commands from a queue and processes them in processCommand(core_mqtt_agent.c)
* After calling the command function, MQTT_ProcessLoop is called (if commandOutParams.runProcessLoop is TRUE, e.g. for publish commands)
* MQTT_ProcessLoop calls receiveSingleIteration then MQTT_GetIncomingPacketTypeAndLength
* MQTT_GetIncomingPacketTypeAndLength calls the transport interface "readFunc" to read from the TLS connection
* "readFunc" maps to espTlsTransportRecv in "network_transport.c"
* If there is no data waiting, espTlsTransportRecv will BLOCK for 3000 mS before returning.
It looks to me like this call is expected to be a non-blocking read? However, the "non_block" option is NOT specified in the esp_tls_cfg_t passed to esp_tls_conn_new_sync (in xTlsConnect).
I'm confused by this. It looks like it will almost always block for 3 seconds per message when several MQTT messages are sent, and this is what I observe. It became very obvious when I tried to request a firmware update file (in 1 kb chunks) via MQTT file stream, using QOS 0, and each request took 3 seconds, which was spent waiting for the agent to publish the request message.
Am I using this interface incorrectly? Or is it supposed to have "non_block = 1" in the esp_tls_cfg_t?