LiamBindle / MQTT-C

A portable MQTT C client for embedded systems and PCs alike.
https://liambindle.ca/MQTT-C
MIT License
766 stars 269 forks source link

MQTT-C endless loop if sendbuffer is too large #190

Open Bauteiltoeter opened 4 months ago

Bauteiltoeter commented 4 months ago

After enlarging the receive / sendBuffer for MQTT-C to 16MB my application started to go into endless loops at 100% CPU time after some time.

My application is sending and receiveing QoS0-Messages only.

Investigating this issue showed that the do-while-loop inside of __mqtt_next_pid was spinning endlessly because it couldn't find any unused pids. Analysing client->mq showed that there where 65535 packages in the queue.

I tried to find the mechanism where this queue is cleared. The only way that I could find to clear the queue is the callstack mqtt_publish() -> MQTT_CLIENT_TRY_PACK() -> mqtt_pack_publish_request() -> mqtt_pack_fixed_header() returning 0 because there is not enough space in the transmit buffer.

This means there is no "normal" way this queue is cleared, it's only cleared if it's full. If more than 65535 packages fit into the queue it will fail.

My application has a lot of small messages mixed with very big messages, this will make the appearance of the bug much more likely.

Steps to reproduce: