MQTTAsync_disconnect could return with success while disconnect command dropped
smallSwed opened this issue · comments
Describe the bug
MQTTAsync_disconnect
can return with success while the disconnect MQTTAsync_queuedCommand
not inserted into the MQTTAsync_commands
via MQTTAsync_addCommand
if there are not enough memory. (like: memory constrained environment)
The caller of MQTTAsync_disconnect
could be blocked indefinitly while waiting for the callbacks (onSuccess or onFailiure).
Source of the problem
After a bit of investigation I found one possible source for the problem inside MQTTAsyncUtils.c:
The MQTTAsync_addCommand
calls the ListAppend
in order to append the new disconnect command but does not check the return value to be not NULL (which is the case when the malloc fails for ListElement
). In this case the content will be leaked and the disconnect never processed i.e. no callback called
The ListInsert
call in MQTTAsync_addCommand
has the same problem.
Expected behavior
A MQTTAsync_disconnect
should return an error when the disconnect command is not queued.
Environment
- OS: Windows 11
- Version: paho.mqtt.c 1.3.12
Additional context
I was investigating a block on shutdown and the reason for the block was a signal not firing. In our execution of shutdown we try to disconnect via the async api:
0. set up option parameter with callbacks (e.g. onSuccess5
)
- calling
MQTTAsync_disconnect
which returns no error - the thread waits for a signal, which should be set/triggered when any of the callbacks
options.onSuccess5
oroptions.onFailure5
are executed - but neither
onSuccess5
oronFailure5
called and the whole execution is blocked indefinitly (or for a very long time, I cannot be sure about this, there was a lot of traffic)