eclipse / paho.mqtt.python

paho.mqtt.python
Other
2.17k stars 723 forks source link

disconnect() and __del__() not always working on a linux machine #673

Open maovidal opened 2 years ago

maovidal commented 2 years ago

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, my Intel Linux client is still randomly having its connections not being closed, while my development machine, an Intel macOS on the same network than the client and mosquitto broker, does not miss any of the disconnections (But still, requires the __del__()):

import time
import paho.mqtt.client as mqtt

# Callbacks to help diagnose
def on_connect(mqttc, obj, flags, rc):
    print("Connected to %s:%s" % (mqttc._host, mqttc._port))

def on_disconnect(mqttc, client, rc):
    print("Disconnected from %s:" % mqttc._host)

def on_log(mqttc, obj, level, string):
    print(string)

# Client preparation
mqttc = mqtt.Client(
    client_id='testing_disconnect'
)
mqttc.on_connect = on_connect
mqttc.on_disconnect = on_disconnect
mqttc.on_log = on_log

mqttc.username_pw_set(
    username=<REDACTED>,
    password=<REDACTED>,
)

# Test
connection_test: int = 1
while connection_test <= 100:

    # 1. Client prepares a will message whose content is the test number
    #    performed (that should be received by other clients on every
    #    disconnection)
    mqttc.will_set(
        topic="Disconnection test",
        payload=str(connection_test),
        qos=0,
        retain=False,
    )

    # 2. Client connects
    mqttc.connect_async(
    # mqttc.connect(  # Also works
        host="192.168.2.1",
        port=1883,
        keepalive=60,
    )
    mqttc.loop_start()
    print(connection_test)

    # 3. Client waits a bit, in case someonelse needs a bit of time to perform
    #    something else before receiving the disconnection.
    time.sleep(0.5)

    # 4. Client disconnects (The will message should be dispatched by the
    #    the broker)
    mqttc.disconnect()
    mqttc.__del__()
    mqttc.loop_stop()

    # 5. Preparation for the next test
    connection_test = connection_test + 1

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:

# python demo_paho_client.py 
1
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
Received CONNACK (0, 0)
Connected to 192.168.2.1:1883
Sending DISCONNECT
2
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
Received CONNACK (0, 0)
Connected to 192.168.2.1:1883
Sending DISCONNECT
3
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
Received CONNACK (0, 0)
Connected to 192.168.2.1:1883
Sending DISCONNECT
Exception in thread Thread-3 (_thread_main):
Traceback (most recent call last):
  File "/usr/lib/python3.10/threading.py", line 1009, in _bootstrap_inner
  File "/usr/lib/python3.10/threading.py", line 946, in run
  File "/usr/lib/python3.10/site-packages/paho/mqtt/client.py", line 3591, in _thread_main
    self.loop_forever(retry_first_connection=True)
  File "/usr/lib/python3.10/site-packages/paho/mqtt/client.py", line 1756, in loop_forever
    rc = self._loop(timeout)
  File "/usr/lib/python3.10/site-packages/paho/mqtt/client.py", line 1176, in _loop
    self._sockpairR.recv(10000)
OSError: [Errno 9] Bad file descriptor
4
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
Sending DISCONNECT
5
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
6
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
Received CONNACK (0, 0)
Connected to 192.168.2.1:1883
Sending DISCONNECT
7
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
Sending DISCONNECT
8
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
9
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
10
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
Sending DISCONNECT
11
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
Received CONNACK (0, 0)
Connected to 192.168.2.1:1883
Sending DISCONNECT
12
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
13
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
14
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
Received CONNACK (0, 0)
Connected to 192.168.2.1:1883
Sending DISCONNECT
Disconnected from 192.168.2.1:
15
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
16
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
17
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
Received CONNACK (0, 0)
Connected to 192.168.2.1:1883
Sending DISCONNECT
18
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
Received CONNACK (0, 0)
Connected to 192.168.2.1:1883
Sending DISCONNECT
19
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
Received CONNACK (0, 0)
Connected to 192.168.2.1:1883
Sending DISCONNECT
20
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
Received CONNACK (0, 0)
Connected to 192.168.2.1:1883
Sending DISCONNECT
21
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
Received CONNACK (0, 0)
Connected to 192.168.2.1:1883
Sending DISCONNECT
22
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
Received CONNACK (0, 0)
Connected to 192.168.2.1:1883
Sending DISCONNECT
23
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
Received CONNACK (0, 0)
Connected to 192.168.2.1:1883
Sending DISCONNECT
Disconnected from 192.168.2.1:
24
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
25
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
Received CONNACK (0, 0)
Connected to 192.168.2.1:1883
Sending DISCONNECT
Disconnected from 192.168.2.1:
26
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
Received CONNACK (0, 0)
Connected to 192.168.2.1:1883
Sending DISCONNECT
27
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
Received CONNACK (0, 0)
Connected to 192.168.2.1:1883
Sending DISCONNECT
Disconnected from 192.168.2.1:
28
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
Received CONNACK (0, 0)
Connected to 192.168.2.1:1883
Sending DISCONNECT
Disconnected from 192.168.2.1:
29
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
Received CONNACK (0, 0)
Connected to 192.168.2.1:1883
Sending DISCONNECT
30
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
Received CONNACK (0, 0)
Connected to 192.168.2.1:1883
Sending DISCONNECT
Disconnected from 192.168.2.1:
31
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
Received CONNACK (0, 0)
Connected to 192.168.2.1:1883
Sending DISCONNECT
32
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
Received CONNACK (0, 0)
Connected to 192.168.2.1:1883
Sending DISCONNECT
33
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
Received CONNACK (0, 0)
Connected to 192.168.2.1:1883
Sending DISCONNECT
34
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
Received CONNACK (0, 0)
Connected to 192.168.2.1:1883
Sending DISCONNECT
35
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
Received CONNACK (0, 0)
Connected to 192.168.2.1:1883
Sending DISCONNECT
36
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
Received CONNACK (0, 0)
Connected to 192.168.2.1:1883
Sending DISCONNECT
Disconnected from 192.168.2.1:
37
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
Received CONNACK (0, 0)
Connected to 192.168.2.1:1883
Sending DISCONNECT
Disconnected from 192.168.2.1:
38
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
Received CONNACK (0, 0)
Connected to 192.168.2.1:1883
Sending DISCONNECT
39
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
Received CONNACK (0, 0)
Connected to 192.168.2.1:1883
Sending DISCONNECT
40
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
Received CONNACK (0, 0)
Connected to 192.168.2.1:1883
Sending DISCONNECT
41
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
Received CONNACK (0, 0)
Connected to 192.168.2.1:1883
Sending DISCONNECT
42
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
Received CONNACK (0, 0)
Connected to 192.168.2.1:1883
Sending DISCONNECT
43
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
Received CONNACK (0, 0)
Connected to 192.168.2.1:1883
Sending DISCONNECT
44
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
Received CONNACK (0, 0)
Connected to 192.168.2.1:1883
Sending DISCONNECT
45
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
Received CONNACK (0, 0)
Connected to 192.168.2.1:1883
Sending DISCONNECT
46
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
Received CONNACK (0, 0)
Connected to 192.168.2.1:1883
Sending DISCONNECT
47
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
Received CONNACK (0, 0)
Connected to 192.168.2.1:1883
Sending DISCONNECT
48
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
Received CONNACK (0, 0)
Connected to 192.168.2.1:1883
Sending DISCONNECT
49
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
Received CONNACK (0, 0)
Connected to 192.168.2.1:1883
Sending DISCONNECT
Exception in thread Thread-49 (_thread_main):
Traceback (most recent call last):
  File "/usr/lib/python3.10/threading.py", line 1009, in _bootstrap_inner
  File "/usr/lib/python3.10/threading.py", line 946, in run
  File "/usr/lib/python3.10/site-packages/paho/mqtt/client.py", line 3591, in _thread_main
    self.loop_forever(retry_first_connection=True)
  File "/usr/lib/python3.10/site-packages/paho/mqtt/client.py", line 1756, in loop_forever
    rc = self._loop(timeout)
  File "/usr/lib/python3.10/site-packages/paho/mqtt/client.py", line 1176, in _loop
    self._sockpairR.recv(10000)
OSError: [Errno 9] Bad file descriptor
50
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
Received CONNACK (0, 0)
Connected to 192.168.2.1:1883
Sending DISCONNECT
51
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
Received CONNACK (0, 0)
Connected to 192.168.2.1:1883
Sending DISCONNECT
52
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
Received CONNACK (0, 0)
Connected to 192.168.2.1:1883
Sending DISCONNECT
53
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
Received CONNACK (0, 0)
Connected to 192.168.2.1:1883
Sending DISCONNECT
54
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
Sending DISCONNECT
55
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
Received CONNACK (0, 0)
Connected to 192.168.2.1:1883
Sending DISCONNECT
Exception in thread Thread-55 (_thread_main):
Traceback (most recent call last):
  File "/usr/lib/python3.10/threading.py", line 1009, in _bootstrap_inner
  File "/usr/lib/python3.10/threading.py", line 946, in run
  File "/usr/lib/python3.10/site-packages/paho/mqtt/client.py", line 3591, in _thread_main
    self.loop_forever(retry_first_connection=True)
  File "/usr/lib/python3.10/site-packages/paho/mqtt/client.py", line 1756, in loop_forever
    rc = self._loop(timeout)
  File "/usr/lib/python3.10/site-packages/paho/mqtt/client.py", line 1176, in _loop
    self._sockpairR.recv(10000)
OSError: [Errno 9] Bad file descriptor
56
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
Received CONNACK (0, 0)
Connected to 192.168.2.1:1883
Sending DISCONNECT
57
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
Received CONNACK (0, 0)
Connected to 192.168.2.1:1883
Sending DISCONNECT
58
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
Received CONNACK (0, 0)
Connected to 192.168.2.1:1883
Sending DISCONNECT
Disconnected from 192.168.2.1:
59
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
Received CONNACK (0, 0)
Connected to 192.168.2.1:1883
Sending DISCONNECT
60
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
Received CONNACK (0, 0)
Connected to 192.168.2.1:1883
Sending DISCONNECT
61
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
Received CONNACK (0, 0)
Connected to 192.168.2.1:1883
Sending DISCONNECT
62
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
Received CONNACK (0, 0)
Connected to 192.168.2.1:1883
Sending DISCONNECT
63
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
Received CONNACK (0, 0)
Connected to 192.168.2.1:1883
Sending DISCONNECT
64
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
Received CONNACK (0, 0)
Connected to 192.168.2.1:1883
Sending DISCONNECT
65
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
Received CONNACK (0, 0)
Connected to 192.168.2.1:1883
Sending DISCONNECT
Disconnected from 192.168.2.1:
66
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
Received CONNACK (0, 0)
Connected to 192.168.2.1:1883
Sending DISCONNECT
67
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
Received CONNACK (0, 0)
Connected to 192.168.2.1:1883
Sending DISCONNECT
68
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
Received CONNACK (0, 0)
Connected to 192.168.2.1:1883
Sending DISCONNECT
69
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
Received CONNACK (0, 0)
Connected to 192.168.2.1:1883
Sending DISCONNECT
70
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
Received CONNACK (0, 0)
Connected to 192.168.2.1:1883
Sending DISCONNECT
71
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
Received CONNACK (0, 0)
Connected to 192.168.2.1:1883
Sending DISCONNECT
72
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
Received CONNACK (0, 0)
Connected to 192.168.2.1:1883
Sending DISCONNECT
73
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
Received CONNACK (0, 0)
Connected to 192.168.2.1:1883
Sending DISCONNECT
74
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
Received CONNACK (0, 0)
Connected to 192.168.2.1:1883
Sending DISCONNECT
Exception in thread Thread-74 (_thread_main):
Traceback (most recent call last):
  File "/usr/lib/python3.10/threading.py", line 1009, in _bootstrap_inner
  File "/usr/lib/python3.10/threading.py", line 946, in run
  File "/usr/lib/python3.10/site-packages/paho/mqtt/client.py", line 3591, in _thread_main
    self.loop_forever(retry_first_connection=True)
  File "/usr/lib/python3.10/site-packages/paho/mqtt/client.py", line 1756, in loop_forever
    rc = self._loop(timeout)
  File "/usr/lib/python3.10/site-packages/paho/mqtt/client.py", line 1176, in _loop
    self._sockpairR.recv(10000)
OSError: [Errno 9] Bad file descriptor
75
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
Received CONNACK (0, 0)
Connected to 192.168.2.1:1883
Sending DISCONNECT
76
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
Received CONNACK (0, 0)
Connected to 192.168.2.1:1883
Sending DISCONNECT
77
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
Received CONNACK (0, 0)
Connected to 192.168.2.1:1883
Sending DISCONNECT
78
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
Received CONNACK (0, 0)
Connected to 192.168.2.1:1883
Sending DISCONNECT
79
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
Received CONNACK (0, 0)
Connected to 192.168.2.1:1883
Sending DISCONNECT
80
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
Received CONNACK (0, 0)
Connected to 192.168.2.1:1883
Sending DISCONNECT
81
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
Received CONNACK (0, 0)
Connected to 192.168.2.1:1883
Sending DISCONNECT
82
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
Received CONNACK (0, 0)
Connected to 192.168.2.1:1883
Sending DISCONNECT
83
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
Received CONNACK (0, 0)
Connected to 192.168.2.1:1883
Sending DISCONNECT
Disconnected from 192.168.2.1:
84
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
Received CONNACK (0, 0)
Connected to 192.168.2.1:1883
Sending DISCONNECT
85
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
86
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
Received CONNACK (0, 0)
Connected to 192.168.2.1:1883
Sending DISCONNECT
87
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
Sending DISCONNECT
88
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
Received CONNACK (0, 0)
Connected to 192.168.2.1:1883
Sending DISCONNECT
89
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
Sending DISCONNECT
90
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
Received CONNACK (0, 0)
Connected to 192.168.2.1:1883
Sending DISCONNECT
91
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
Sending DISCONNECT
92
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
Received CONNACK (0, 0)
Connected to 192.168.2.1:1883
Sending DISCONNECT
93
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
Sending DISCONNECT
94
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
Received CONNACK (0, 0)
Connected to 192.168.2.1:1883
Sending DISCONNECT
Exception in thread Thread-94 (_thread_main):
Traceback (most recent call last):
  File "/usr/lib/python3.10/threading.py", line 1009, in _bootstrap_inner
  File "/usr/lib/python3.10/threading.py", line 946, in run
  File "/usr/lib/python3.10/site-packages/paho/mqtt/client.py", line 3591, in _thread_main
    self.loop_forever(retry_first_connection=True)
  File "/usr/lib/python3.10/site-packages/paho/mqtt/client.py", line 1756, in loop_forever
    rc = self._loop(timeout)
  File "/usr/lib/python3.10/site-packages/paho/mqtt/client.py", line 1176, in _loop
    self._sockpairR.recv(10000)
OSError: [Errno 9] Bad file descriptor
95
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
Received CONNACK (0, 0)
Connected to 192.168.2.1:1883
Sending DISCONNECT
96
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
Received CONNACK (0, 0)
Connected to 192.168.2.1:1883
Sending DISCONNECT
97
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
98
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
Received CONNACK (0, 0)
Connected to 192.168.2.1:1883
Sending DISCONNECT
99
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
Received CONNACK (0, 0)
Connected to 192.168.2.1:1883
Sending DISCONNECT
Exception in thread Thread-99 (_thread_main):
Traceback (most recent call last):
  File "/usr/lib/python3.10/threading.py", line 1009, in _bootstrap_inner
  File "/usr/lib/python3.10/threading.py", line 946, in run
  File "/usr/lib/python3.10/site-packages/paho/mqtt/client.py", line 3591, in _thread_main
    self.loop_forever(retry_first_connection=True)
  File "/usr/lib/python3.10/site-packages/paho/mqtt/client.py", line 1756, in loop_forever
    rc = self._loop(timeout)
  File "/usr/lib/python3.10/site-packages/paho/mqtt/client.py", line 1176, in _loop
    self._sockpairR.recv(10000)
OSError: [Errno 9] Bad file descriptor
100
Sending CONNECT (u1, p1, wr0, wq0, wf1, c1, k60) client_id=b'testing_disconnect'
Sending DISCONNECT

Is there any way to prevent that error in the Linux machine or any other way to close the connection?

Thank you!

CamDavidsonPilon commented 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.

maovidal commented 2 years ago

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.

abhi1693 commented 2 years ago

@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()
maovidal commented 2 years ago

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?

abhi1693 commented 2 years ago

Yes, specifically inside alpine Linux 3.16