paho.mqtt.python icon indicating copy to clipboard operation
paho.mqtt.python copied to clipboard

memory leak on publish with paho

Open dogmatic69 opened this issue 2 years ago • 2 comments

I've got an app that is publishing a message every 1-5 seconds and after each message overall system memory increases by around 0.1mb. After around a day it will be killed by OOM.

If I uncommment the line client.publish('topic', data) there is no more memory increase.

Any idea how I can figure this out? Does not seem to be any related issues on google.

sample of my code.

mqtt_client = mqtt.Client()
mqtt_client.username_pw_set(MQTT_USERNAME, password=MQTT_PASSWORD)
mqtt_client.connect(MQTT_BROKER_ADDR, MQTT_BROKER_PORT, 60)
infot = mqtt_client.publish(sensor, payload=data, qos=0, retain=False)
infot.wait_for_publish()

dogmatic69 avatar Sep 23 '21 11:09 dogmatic69

Calling publish() and wait_for_publish() isn't enough to guarantee that the network traffic is fully handled - you need to use loop_start() or loop_forever(). You're also not disconnecting the client.

In your case I'd suggest something like this:

import paho.mqtt.client as mqtt

# Initialise and connect
mqtt_client = mqtt.Client()
mqtt_client.username_pw_set(MQTT_USERNAME, password=MQTT_PASSWORD)
mqtt_client.connect(MQTT_BROKER_ADDR, MQTT_BROKER_PORT, 60)
mqtt_client.loop_start()

# The main loop
while True:
    data = get_data()
    mqtt_client.publish(sensor, payload=data, qos=0, retain=False

# Cleanup
mqtt_client.disconnect()
mqtt_client.loop_stop()

This means you aren't creating a new connection for each message, which will be a lot more efficient, especially if you move to TLS encryption.

ralight avatar Sep 23 '21 23:09 ralight

I can try disconnect and reconnect. if that fails I'll try the loop_start (it's a forever running script though). Will report back in a few days

dogmatic69 avatar Sep 27 '21 12:09 dogmatic69

I'm going to close this due to inactivity. If you are still experiencing the issue then please feel free to reopen. This is part of a general project to clean-up issues (which should make it simpler to identify priorities going forward).

MattBrittan avatar Dec 07 '23 21:12 MattBrittan