Open maovidal opened 2 years ago
Note a complete answer, but have a look at https://github.com/eclipse/paho.mqtt.python/issues/627, too.
I've wrapped this up into a subclass:
class Client(PahoClient):
def loop_stop(self):
super().loop_stop()
self._reset_sockets(sockpair_only=True)
return self
I'd be curious how this performs in your test.
Thank you!
While I was not able to fully understand the concepts explained in that issue, I just tested the script using the subclass you proposed and no calling __del__()
on my testing script. Unfortunately that leads to no disconnections mosts of the time.
I noticed that the current __del__()
on the original class just calls _reset_sockets(self, sockpair_only=False)
, so the self._sock_close()
is not called, and it seems that one is in charge of actually closing the connection.
@maovidal I have a working code that does not have this issue. The only two changes (not from the doc) that I implemented were
import time
from paho.mqtt.packettypes import PacketTypes
from paho.mqtt.properties import Properties
time.sleep(0.01) # Not sure why, but this is needed otherwise the disconnects do not work as expected
properties = Properties(PacketTypes.DISCONNECT) # Added this to ensure the properties are properly set
client.disconnect(properties=properties)
client.loop_stop()
Thank you @abhi1693
I've noticed that the properties
argument on disconnect
is only used on the MQTTv5
. However I tried what you mentioned and it didn't worked.
Could you please confirm if your working code is running on Linux?
Yes, specifically inside alpine Linux 3.16
Hi all:
Sometimes my MQTT client needs to close its connection to the broker and immediately open a new one with a different client id, will message and subscriptions. That client runs in a
Linux Intel
PC.What I noticed is that the client was not disconnecting from the broker when issuing
disconnect()
, and it kept publishing messages with the same connection. While it is not clear to me why, after reading #170 and #325 I found that__del__()
is needed.I made the script below that performs 100 consecutive connections and disconnections to test the disconnection using
__del__()
. However, myIntel Linux
client is still randomly having its connections not being closed, while my development machine, anIntel macOS
on the same network than the client andmosquitto
broker, does not miss any of the disconnections (But still, requires the__del__()
):In the case of my Linux client, the terminal reported this, with the notable presence of:
OSError: [Errno 9] Bad file descriptor
in some iterations:Is there any way to prevent that error in the Linux machine or any other way to close the connection?
Thank you!