eclipse / paho.mqtt.python

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

Disconnection with error code 16 #683

Open ivanvaccarics opened 1 year ago

ivanvaccarics commented 1 year ago

Dear all, I encountered an issue on my client. Specifically, the client is connected to a MQTT broker v3.1.1 with username, password and certs. The client is able to correctly connect and receive messages for some time but then the client enters in this loop:

INFO:root:Device disconnected with result code: 16 INFO:root:Device connected with result code: 0 INFO:root:Device disconnected with result code: 16 INFO:root:Device connected with result code: 0

In this way, I can't parse the data. How can i manage this? What means the error code 16? This is my code:

    import json
    import logging
    from paho.mqtt import client as mqtt
    import ssl
    import os
    import copy
    logging.getLogger().setLevel(logging.INFO)

    USERNAME_BROKER = os.getenv('USERNAME_BROKER')
    PWD_BROKER = os.getenv('PWD_BROKER')
    BROKER_URL = os.getenv('BROKER_URL')
    TOPIC = 'sub/Test/#'
    QOS = 0

    def on_connect(client, userdata, flags, rc):
        logging.info("Device connected with result code: " + str(rc))

    def on_disconnect(client, userdata, rc):
        logging.info("Device disconnected with result code: " + str(rc))

    def on_publish(client, userdata, mid):
        logging.info("Device sent message")

    def on_message(client, userdata, message):
        data = json.loads(message.payload.decode("utf-8"))
        print(data)

    client = mqtt.Client(client_id=USERNAME_BROKER, protocol=mqtt.MQTTv311)
    client.on_connect = on_connect
    client.on_disconnect = on_disconnect
    client.on_publish = on_publish
    client.on_message = on_message
    client.username_pw_set(username=USERNAME_BROKER, password=PWD_BROKER)
    client.tls_set(ca_certs="./certs/cert1.pem", certfile="./certs/cert2.pem", keyfile="./certs/key.pem",
    cert_reqs=ssl.CERT_REQUIRED, tls_version=ssl.PROTOCOL_TLSv1_2, ciphers=None)
    client.tls_insecure_set(False)
    client.connect(BROKER_URL, port=8883, keepalive=30)
    client.subscribe(TOPIC, QOS)
    client.loop_forever()

Thanks in advance.

RobertDeRose commented 1 year ago

@ivanvaccarics I'm experiencing a similar issue, I've noticed that the issue seems to be related to receiving a large number of MQTT message in quick succession and it appears that the Client library sends a PINGREQ but either never gets the PINGRESP or fails to process it during the influx of incoming message.

  1. Have you enable the paho.mqtt logging and debugging output?
  2. Do you also notice the issue when you receive a large number of incoming MQTT messages from the broker?

For me, I reproduced the issue using code like yours above and the using the mosquitto_pub cli tool to flood the broker

while [ 1 ];
do
mosquitto_pub -t test -m ''
done

This easily results in the Disconnect loop you are having for me

raomin commented 1 year ago

Same here. When receiving a lot of messages.... Cannot get it to reconnect btw.

raomin commented 1 year ago

did anyone got a solution for this?

RobertDeRose commented 1 year ago

did anyone got a solution for this?

The only solution I have been able to come up with was to ensure my working thread that is also handling the broker connection does it's tasks as fast as possible and if it can't do it fast, spawn off a worker thread to do the work. This could add extra complexity depending on your scenario though.

raomin commented 1 year ago

Indeed that could explain but would be much too complex to change. Eventually I used gmqtt which is working fine.

MattBrittan commented 7 months ago

Error code 16 is probably (sorry there may be other alternatives) MQTT_ERR_KEEPALIVE; this can sometimes be due to user code (if your message handler takes too long) possibly mixed with high message volumes (see issue #328).

My feeling is that this is a duplicate of #328 (so should be closed in favour of that issue); however I'd welcome feedback on that (for now will tag this as info needed).

Note: This is part of an exercise to clean up old issues so that the project can move forwards.

fly2016-1-12 commented 4 months ago

hi, MattBrittan. when will this issue be fixed?

MattBrittan commented 4 months ago

@fly2016-1-12 is this still an issue in v2? This is an open source project, so the issue will be fixed when someone feels like working on it (please feel free to raise a PR if you have a solution). Genrally the more info in the issue, the more likely it is that someone will put effort into resolving the issue (if you can provide a simple way to replicate the issue that would probably help).

dberardo-com commented 1 month ago

same issue here ... any hints ?