eclipse / paho.mqtt.python

paho.mqtt.python
Other
2.15k stars 724 forks source link

client.loop_forever() causes a crash #747

Closed fahadalisarwar1 closed 7 months ago

fahadalisarwar1 commented 10 months ago

here is my code.

mqttc = mqtt.Client()
mqttc.on_connect = on_connect
mqttc.on_message = on_message
mqttc.username_pw_set("fahad", "fahad")
rc = mqttc.connect("192.168.2.65", 1883, 60)
print(rc)
mqttc.subscribe(topics[0], qos=1)
mqttc.publish(topics[1], "off", qos=1, retain=True)
print("okkkk")
mqttc.loop_forever()

As soon as it reaches the lines client.loop_forever(), it crashes with following code.

0
okkkk
Traceback (most recent call last):
  File "/home/h2gremm/mqtt-app/main.py", line 37, in <module>
    mqttc.loop_forever()
  File "/home/h2gremm/mqtt-app/venv/lib/python3.11/site-packages/paho/mqtt/client.py", line 1756, in loop_forever
    rc = self._loop(timeout)
         ^^^^^^^^^^^^^^^^^^^
  File "/home/h2gremm/mqtt-app/venv/lib/python3.11/site-packages/paho/mqtt/client.py", line 1164, in _loop
    rc = self.loop_read()
         ^^^^^^^^^^^^^^^^
  File "/home/h2gremm/mqtt-app/venv/lib/python3.11/site-packages/paho/mqtt/client.py", line 1556, in loop_read
    rc = self._packet_read()
         ^^^^^^^^^^^^^^^^^^^
  File "/home/h2gremm/mqtt-app/venv/lib/python3.11/site-packages/paho/mqtt/client.py", line 2439, in _packet_read
    rc = self._packet_handle()
         ^^^^^^^^^^^^^^^^^^^^^
  File "/home/h2gremm/mqtt-app/venv/lib/python3.11/site-packages/paho/mqtt/client.py", line 3039, in _packet_handle
    return self._handle_connack()
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/home/h2gremm/mqtt-app/venv/lib/python3.11/site-packages/paho/mqtt/client.py", line 3138, in _handle_connack
    on_connect(
TypeError: on_connect() missing 1 required positional argument: 'return_code'

Can please somebody tell me the reason.

petersilva commented 9 months ago

It's telling you the reason:


TypeError: on_connect() missing 1 required positional argument: 'return_code'

your on_connect() function (which you didn't include) doesn't have the right number of parameters.

MattBrittan commented 7 months ago

Closing this as Peters comment looks good and there has been no activity since. Please feel free to reopen if you are still having issues.