Closed nandlab closed 4 months ago
Actually what you're asking for is to not send the DISCONNECT. The session still needs to be closed (SOCKET closed, state cleaned up). And in MQTT V5 the DISCONNECT can be sent and request the LWT to be sent, so that could be used here.
As the C++ library is attempting resource management via RAII, the client object does need to destruct the underlying C struct to avoid memory and resource leaks.
As to whether the C lib sends the DISCONNECT packet when the client struct it is being destroyed, that is not something that can be controlled externally, and should be more properly raised as an issue in the Paho C repository.
I suppose that, since the sending of a DISCONNECT packet is optional by the specification, then sending it could be an option in the C library. Perhaps that could be added as a boolean like int sendDisconnect
in the MQTTAsync_disconnectOptions
struct.
I'll open this issue in the C lib.
Currently, the destructor of the
mqtt::async_client
class callsMQTTAsync_destroy
, which in turn sends a disconnect request to close the mqtt session, then closes the TCP socket.In
paho.mqtt.cpp/src/async_client.cpp
:In
paho.mqtt.c/src/MQTTAsync.c
:This is not expected and can lead to problems. Instead, it should just disable the callbacks, close the TCP socket and deallocate all acquired resources. The destructor should not send any MQTT packets, because it is the responsibility of the programmer. If the programmer wants to close the session, he should call
disconnect()
explicitly.My Use Case I want to track the status of a mqtt client. Therefore it has a
status
topic, where it publishes retained messages about its status. When connecting it sets will to publishFAILED
on thestatus
topic.ONLINE
directlyOFFLINE
and then callsdisconnect()
FAILED
status for the client automatically (LWAT)Example scenario: The client connected and set the status
ONLINE
. Now we imagine it runs out of memory. In the program, a fatal exceptionstd::bad_alloc
is thrown at some point, which causes the program to crash. While the program stack is unwinded, themqtt::async_client
destructor is called and the program exits abnormally. I would expect the status to becomeFAILED
. Problem: Because themqtt::async_client
destructor closes the session, neitherOFFLINE
norFAILED
are published and the topic status stays atONLINE
, although the clients program crashed.Can you please not close the session in the
mqtt::async_client
destructor?