0

I am using paho.mqtt.client.

Below is a simplified version of the code but it stills shows the issue.

# -*- coding: utf-8 -*-

import sys
import os
import time
import logging

from time import sleep

import paho.mqtt.client as mqtt

mqtt_server_ip      = "10.42.0.1"
mqtt_server_port    = 1884

subscriptions_qos =[("doorStatus/status", 0),
                    ("doorStatus/eol",0)]

def callback_door_status(client, userdata, message):
    logging.debug("Received %s", message.payload)

def on_log(client, userdata, level, buf):
    logging.debug("%s", buf)

def on_connect(client, userdata, flags, rc):
    logging.info("Successfully connected to MQTT with result code %s", str(rc))
    print("before message_callback_add 1")
    client.message_callback_add("doorStatus", callback_door_status)
    print("after message_callback_add")

    (result, _) = client.subscribe(subscriptions_qos)
    if (result == mqtt.MQTT_ERR_SUCCESS):
        logging.info("Successfully subscribed to MQTT topics with result code %s", str(result))

def on_message(client, userdata, msg):
    logging.debug("Received: Topic: %s Body: %s", msg.topic, msg.payload)

def main():
    logger = logging.getLogger('root')
    logging.basicConfig(format='[%(asctime)s %(levelname)s: %(funcName)20s] %(message)s', level=logging.DEBUG)

    client = mqtt.Client("master")
    client.on_log = on_log
    client.on_connect = on_connect
    client.on_message = on_message
    client.connect(mqtt_server_ip, mqtt_server_port, 60)

    client.loop_forever()

if __name__ == '__main__':
    main()

And this is the output from the program:

[2017-09-20 07:06:40,562 DEBUG:               on_log] Received CONNACK (0, 0)
[2017-09-20 07:06:40,562 INFO:           on_connect] Successfully connected to MQTT with result code 0
before message_callback_add 1

As you can see from the output, the connection to the broker is established successfully, but right after the first call to message_callback_add the code gets stuck and nothing else happens. At this point the only thing I can do is to kill the process. I have no idea why the code is getting stuck, I have also looked at the documentation and still couldn't find anything.

theAlse
  • 5,577
  • 11
  • 68
  • 110
  • I appear to be able to reproduce this and I can't see why it should behave this way. I'd be tempted to raise a issue against the python paho code as described here: https://pypi.python.org/pypi/paho-mqtt/1.1#reporting-bugs – hardillb Sep 20 '17 at 08:58

1 Answers1

2

I managed to solve the issue myself. Posting the answer here and it might help someone in the future.

The reason why the code gets stuck is I beleive a mutex (dead lock) issue. message_callback_add() must be performed before calling on_connect(). This was not that trivial reading the documentation.

theAlse
  • 5,577
  • 11
  • 68
  • 110
  • 1
    I'd still raise a bug as that sounds like you end up "doing" a subscription before you've connected, which is wrong, – hardillb Sep 20 '17 at 19:23
  • I have reported as a bug – theAlse Sep 22 '17 at 05:18
  • I just ran into the same issue again. Maybe I should check if I can just submit a PR with the documentation fix... For reference, the bug report is here: https://github.com/eclipse/paho.mqtt.python/issues/233 – Tanuva Dec 26 '19 at 09:17