You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When not using loop_forever or loop_start and running the loop in the main thread, a hard disconnect gets captured by the client but doesn't change the output of is_connected() method.
Steps to reproduce:
Run an mqtt server, I use emqx:
docker run -d --name emqx -p 18083:18083 -p 1883:1883 emqx/emqx:v4.0.0
Run the code below:
importtimeimportloggingimportpaho.mqtt.clientasmqttlogging.basicConfig(level=logging.DEBUG)
logger=logging.getLogger(__name__)
defon_connect(client, userdata, flags, rc):
logger.debug('Connected with result code '+str(rc))
# subscribeclient.subscribe('tienda/#')
defon_message(client, userdata, msg):
logger.debug(msg.topic+" "+str(msg.payload))
# if b'stop' in msg.payload:# client.disconnect()defon_disconnect(client, flags, rc):
logger.debug('Disconnected with result code '+str(rc))
# client.reconnect()# Build the clientclient=mqtt.Client(clean_session=True)
logger.error('Client initialized with status: %s', client._state)
# Specify callback functionclient.on_connect=on_connectclient.on_message=on_messageclient.on_disconnect=on_disconnectclient.enable_logger(logger)
# client.username_pw_set('topse', 'cret')# Establish a connectionclient.connect('localhost', 1883, 60)
# wait for connectionwhilenotclient.is_connected():
logger.debug('Connecting')
client.loop(timeout=1)
logger.debug('Connected')
# This loops never ends even after there is no connection at allwhileclient.is_connected():
client.loop()
time.sleep(1)
logger.debug('In main loop: %s', client.is_connected())
The result is that you will see the Disconnected with result code: 0 message, but the program never exits and keeps printing:
DEBUG:__main__:In main loop: True
DEBUG:__main__:In main loop: True
DEBUG:__main__:In main loop: True
DEBUG:__main__:In main loop: True
DEBUG:__main__:In main loop: True
The problem is the internal field _state doesn't changes to disconnected or any other states when we are not calling the disconnect method directly. This causes the is_connected() method return true and the loop never exits.
paho-mqtt version: 1.5.1
The text was updated successfully, but these errors were encountered:
I'm able to reproduce this with the sample code your provided and killing the broken after log of In main loop.
I've used Mosquitto rather than emqx but this shouldn't matter.
When not using loop_forever or loop_start and running the loop in the main thread, a hard disconnect gets captured by the client but doesn't change the output of
is_connected()
method.Steps to reproduce:
The result is that you will see the
Disconnected with result code: 0
message, but the program never exits and keeps printing:The problem is the internal field
_state
doesn't changes to disconnected or any other states when we are not calling the disconnect method directly. This causes theis_connected()
method return true and the loop never exits.paho-mqtt version: 1.5.1
The text was updated successfully, but these errors were encountered: