Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

loop_start() before connect() #549

Closed
jps-aldridge opened this issue Jan 30, 2021 · 2 comments · Fixed by #796
Closed

loop_start() before connect() #549

jps-aldridge opened this issue Jan 30, 2021 · 2 comments · Fixed by #796
Labels
Status: Available No one has claimed responsibility for resolving this issue. Type: Bug

Comments

@jps-aldridge
Copy link

The documentation says that loop_start() can be called either before or after connect(), however this appears not to be true. Calling loop_start() after connect() seems to work fine

import paho.mqtt.client
print(paho.mqtt.__version__)

client = paho.mqtt.client.Client()

client.connect('keylimepi.lan')
client.loop_start()

mi = client.publish('test', 'testing')
mi.wait_for_publish()

client.disconnect()
client.loop_stop()

but if I call it before

import paho.mqtt.client
print(paho.mqtt.__version__)

client = paho.mqtt.client.Client()

client.loop_start()
client.connect('keylimepi.lan')

mi = client.publish('test', 'testing')
mi.wait_for_publish()

client.disconnect()
client.loop_stop()

then the message is not published and the wait_for_publish() call hangs.

This behaviour is observed with both version 1.4.0 and 1.5.1

@dachshund-digital
Copy link

The logic I use is to connect than loop. This makes sense to me, in that until you connect successfully looping serves little purpose. I also use connect_async and set a connect=true flag in the on_connect callback, so that I know when it is possible for loop to do any work.

I also call disconnect before loop_stop, since I want to let on_disconnect run. Even un_subscribe gets aborted when you do explicit unscribes and call loop_stop to fast for the broker to respond.

I don't tend to call wait_for_publish the way my code is designed, I use the on_publish call back to let my main loop know a publish was completed based on broker response.

@ralight ralight added Status: Available No one has claimed responsibility for resolving this issue. Type: Bug labels Aug 16, 2021
@PierreF
Copy link
Contributor

PierreF commented Jan 7, 2024

I'm not sure the message being dropped is a bug. You do a publish with QoS = 0 which is according to MQTTv3.1.1 standard:

The message is delivered according to the capabilities of the underlying network.

So in my interpretation, the library should drop the message is the connection is not yet established.

Having wait_for_publish that hang is a bug however.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Available No one has claimed responsibility for resolving this issue. Type: Bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants