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

Fix memory leak in MqttClient #1742

Merged
merged 2 commits into from
Jul 7, 2019
Merged

Conversation

mikee47
Copy link
Contributor

@mikee47 mikee47 commented Jul 4, 2019

If request queue is full due to non-responsive server then allocated message will never be freed.

A new MqttRequestQueue class handles all message queueing. Only one connect message may be queued, and it always takes priority over others.

Should fix #1740

@verybadsoldier
Copy link
Contributor

verybadsoldier commented Jul 4, 2019

This patch stops the reconnect attempts entirely for me. The first tries are there but are still decreasing memory (which is expected as far as I understand the change). Then the queue check kicks in no more connection attempts happen.

NOTE: Tested on 3.8.0!

ip:192.168.2.28,mask:255.255.255.0,gw:192.168.2.1
23558093 ip:192.168.2.28,mask:255.255.255.0,gw:192.168.2.1
23558381 

Let's publish message now. Memory free=49744
Connecting to 	mqtt://192.168.5.2:555/
pm open,type:2 0
MQTT Broker Unreachable!!
Let's publish message now. Memory free=48704
Connecting to 	mqtt://192.168.5.2:555/
MQTT Broker Unreachable!!
Let's publish message now. Memory free=48200
Connecting to 	mqtt://192.168.5.2:555/
MQTT Broker Unreachable!!
Let's publish message now. Memory free=47792
Connecting to 	mqtt://192.168.5.2:555/
MQTT Broker Unreachable!!
Let's publish message now. Memory free=47360
Connecting to 	mqtt://192.168.5.2:555/
MQTT Broker Unreachable!!
Let's publish message now. Memory free=46912
Connecting to 	mqtt://192.168.5.2:555/
70534007 MQTT request queue full!

message->common.type = MQTT_TYPE_SUBSCRIBE;
message->subscribe.topics = (mqtt_topicpair_t*)malloc(sizeof(mqtt_topicpair_t));
memset(message->subscribe.topics, 0, sizeof(mqtt_topicpair_t));
mqtt_message_t message;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are creating here a local variable and then you are passing the address of that local variable to be used later from the requestQueue. I am not sure this is a good idea. By the time the message is dequeued the memory address will most certainly point to something unexpected.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've taken another go at this, much happier with it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've taken another go at this, much happier with it.

Now it looks much better :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On the Host it works like charm.

55018194 MQTT replacing connect message
MQTT Broker Disconnected!!
Connecting to 	mqtt://attachix.zom:1883/
57112580 MQTT replacing connect message
MQTT Broker Disconnected!!
Connecting to 	mqtt://attachix.zom:1883/
59217835 MQTT replacing connect message
MQTT Broker Disconnected!!
Connecting to 	mqtt://attachix.zom:1883/
61317774 MQTT replacing connect message
MQTT Broker Disconnected!!
...

and the final summary

==5606== LEAK SUMMARY:
==5606==    definitely lost: 0 bytes in 0 blocks
==5606==    indirectly lost: 0 bytes in 0 blocks
==5606==      possibly lost: 0 bytes in 0 blocks

mikee47 added 2 commits July 5, 2019 22:50
* Handle connect message outside of queue, always give it priority
* In `copyString`, free destination buffer first to avoid memory leaks
* Use helper functions to create, delete and clear messages
* Use `MQTT_MALLOC` instead of `malloc` for messages and other items
@mikee47 mikee47 force-pushed the fix/mqtt_memleak branch from 94e7335 to 457a655 Compare July 5, 2019 22:10
@slaff
Copy link
Contributor

slaff commented Jul 6, 2019

@mikee47 Can you try again with the latest code from this PR?

@mikee47
Copy link
Contributor Author

mikee47 commented Jul 6, 2019

@verybadsoldier Can you try again with the latest code from this PR?

@slaff slaff added this to the 3.9.0 milestone Jul 6, 2019
@verybadsoldier
Copy link
Contributor

Sorry but still leaking for me:

Connecting to 	mqtt://192.165.5.3:883/
MQTT Broker Unreachable!!
Let's publish message now. Memory free=49176
pm open,type:2 0
Connecting to 	mqtt://192.165.5.3:883/
27310057 MQTT replacing connect message
MQTT Broker Unreachable!!
Let's publish message now. Memory free=48472
Connecting to 	mqtt://192.165.5.3:883/
36385085 MQTT replacing connect message
MQTT Broker Unreachable!!
Let's publish message now. Memory free=48296
Connecting to 	mqtt://192.165.5.3:883/
45760119 MQTT replacing connect message
MQTT Broker Unreachable!!
Let's publish message now. Memory free=48112
Connecting to 	mqtt://192.165.5.3:883/
54960107 MQTT replacing connect message
MQTT Broker Unreachable!!
Let's publish message now. Memory free=47936
Connecting to 	mqtt://192.165.5.3:883/
64060168 MQTT replacing connect message
MQTT Broker Unreachable!!
Let's publish message now. Memory free=47752
Connecting to 	mqtt://192.165.5.3:883/
73111161 MQTT replacing connect message
MQTT Broker Unreachable!!
Let's publish message now. Memory free=47584
Connecting to 	mqtt://192.165.5.3:883/
82360126 MQTT replacing connect message
MQTT Broker Unreachable!!
Let's publish message now. Memory free=47392
Connecting to 	mqtt://192.165.5.3:883/
91535142 MQTT replacing connect message

@mikee47
Copy link
Contributor Author

mikee47 commented Jul 6, 2019

Wait a bit, the queue holds 10 messages so memory will stop incrementing once that's full.

@verybadsoldier
Copy link
Contributor

Ah, thanks, right. Stable for a while then one drop later. But that might have had other reasons.

Let's publish message now. Memory free=47584
Connecting to 	mqtt://192.165.5.3:883/
82360126 MQTT replacing connect message
MQTT Broker Unreachable!!
Let's publish message now. Memory free=47392
Connecting to 	mqtt://192.165.5.3:883/
91535142 MQTT replacing connect message
MQTT Broker Unreachable!!
Let's publish message now. Memory free=47232
Connecting to 	mqtt://192.165.5.3:883/
100635114 MQTT replacing connect message
MQTT Broker Unreachable!!
Let's publish message now. Memory free=47064
Connecting to 	mqtt://192.165.5.3:883/
109835156 MQTT replacing connect message
MQTT Broker Unreachable!!
Let's publish message now. Memory free=47064
Connecting to 	mqtt://192.165.5.3:883/
118960126 MQTT replacing connect message
MQTT Broker Unreachable!!
Let's publish message now. Memory free=47064
Connecting to 	mqtt://192.165.5.3:883/
128260198 MQTT replacing connect message
MQTT Broker Unreachable!!
Let's publish message now. Memory free=47064
Connecting to 	mqtt://192.165.5.3:883/
137260130 MQTT replacing connect message
MQTT Broker Unreachable!!
Let's publish message now. Memory free=47064
Connecting to 	mqtt://192.165.5.3:883/
146510155 MQTT replacing connect message
MQTT Broker Unreachable!!
Let's publish message now. Memory free=47064
Connecting to 	mqtt://192.165.5.3:883/
155635159 MQTT replacing connect message
MQTT Broker Unreachable!!
Let's publish message now. Memory free=47064
Connecting to 	mqtt://192.165.5.3:883/
165010131 MQTT replacing connect message
MQTT Broker Unreachable!!
Let's publish message now. Memory free=47064
Connecting to 	mqtt://192.165.5.3:883/
174185476 MQTT replacing connect message
MQTT Broker Unreachable!!
Let's publish message now. Memory free=47064
Connecting to 	mqtt://192.165.5.3:883/
183460184 MQTT replacing connect message
MQTT Broker Unreachable!!
Let's publish message now. Memory free=47064
Connecting to 	mqtt://192.165.5.3:883/
192585184 MQTT replacing connect message
MQTT Broker Unreachable!!
Let's publish message now. Memory free=47064
Connecting to 	mqtt://192.165.5.3:883/
201860123 MQTT replacing connect message
MQTT Broker Unreachable!!
Let's publish message now. Memory free=47064
Connecting to 	mqtt://192.165.5.3:883/
211010409 MQTT replacing connect message
MQTT Broker Unreachable!!
Let's publish message now. Memory free=47064
Connecting to 	mqtt://192.165.5.3:883/
220210126 MQTT replacing connect message
MQTT Broker Unreachable!!
Let's publish message now. Memory free=47064
Connecting to 	mqtt://192.165.5.3:883/
229310153 MQTT replacing connect message
MQTT Broker Unreachable!!
Let's publish message now. Memory free=47064
Connecting to 	mqtt://192.165.5.3:883/
238435150 MQTT replacing connect message
MQTT Broker Unreachable!!
Let's publish message now. Memory free=47064
Connecting to 	mqtt://192.165.5.3:883/
247560185 MQTT replacing connect message
MQTT Broker Unreachable!!
Let's publish message now. Memory free=47064
Connecting to 	mqtt://192.165.5.3:883/
256610119 MQTT replacing connect message
MQTT Broker Unreachable!!
Let's publish message now. Memory free=47064
Connecting to 	mqtt://192.165.5.3:883/
265810128 MQTT replacing connect message
MQTT Broker Unreachable!!
Let's publish message now. Memory free=47064
Connecting to 	mqtt://192.165.5.3:883/
275010131 MQTT replacing connect message
MQTT Broker Unreachable!!
Let's publish message now. Memory free=47064
Connecting to 	mqtt://192.165.5.3:883/
284335185 MQTT replacing connect message
MQTT Broker Unreachable!!
Let's publish message now. Memory free=47064
Connecting to 	mqtt://192.165.5.3:883/
293335162 MQTT replacing connect message
MQTT Broker Unreachable!!
Let's publish message now. Memory free=47064
Connecting to 	mqtt://192.165.5.3:883/
302735126 MQTT replacing connect message
MQTT Broker Unreachable!!
Let's publish message now. Memory free=47064
Connecting to 	mqtt://192.165.5.3:883/
312160129 MQTT replacing connect message
MQTT Broker Unreachable!!
Let's publish message now. Memory free=47064
Connecting to 	mqtt://192.165.5.3:883/
321285200 MQTT replacing connect message
MQTT Broker Unreachable!!
Let's publish message now. Memory free=47064
Connecting to 	mqtt://192.165.5.3:883/
330435127 MQTT replacing connect message
MQTT Broker Unreachable!!
Let's publish message now. Memory free=47064
Connecting to 	mqtt://192.165.5.3:883/
339560148 MQTT replacing connect message
MQTT Broker Unreachable!!
Let's publish message now. Memory free=47064
Connecting to 	mqtt://192.165.5.3:883/
348685130 MQTT replacing connect message
MQTT Broker Unreachable!!
Let's publish message now. Memory free=47064
Connecting to 	mqtt://192.165.5.3:883/
358085117 MQTT replacing connect message
MQTT Broker Unreachable!!
Let's publish message now. Memory free=47064
Connecting to 	mqtt://192.165.5.3:883/
367385196 MQTT replacing connect message
MQTT Broker Unreachable!!
Let's publish message now. Memory free=47064
Connecting to 	mqtt://192.165.5.3:883/
376560152 MQTT replacing connect message
MQTT Broker Unreachable!!
Let's publish message now. Memory free=47064
Connecting to 	mqtt://192.165.5.3:883/
385835125 MQTT replacing connect message
MQTT Broker Unreachable!!
Let's publish message now. Memory free=47064
Connecting to 	mqtt://192.165.5.3:883/
395110124 MQTT replacing connect message
MQTT Broker Unreachable!!
Let's publish message now. Memory free=47064
Connecting to 	mqtt://192.165.5.3:883/
404385160 MQTT replacing connect message
MQTT Broker Unreachable!!
Let's publish message now. Memory free=47064
Connecting to 	mqtt://192.165.5.3:883/
413710162 MQTT replacing connect message
MQTT Broker Unreachable!!
Let's publish message now. Memory free=47064
Connecting to 	mqtt://192.165.5.3:883/
423010131 MQTT replacing connect message
MQTT Broker Unreachable!!
Let's publish message now. Memory free=47064
Connecting to 	mqtt://192.165.5.3:883/
432285201 MQTT replacing connect message
MQTT Broker Unreachable!!
Let's publish message now. Memory free=47064
Connecting to 	mqtt://192.165.5.3:883/
441310156 MQTT replacing connect message
MQTT Broker Unreachable!!
Let's publish message now. Memory free=47064
Connecting to 	mqtt://192.165.5.3:883/
450360134 MQTT replacing connect message
MQTT Broker Unreachable!!
Let's publish message now. Memory free=47064
Connecting to 	mqtt://192.165.5.3:883/
459535135 MQTT replacing connect message
MQTT Broker Unreachable!!
Let's publish message now. Memory free=47064
Connecting to 	mqtt://192.165.5.3:883/
468935117 MQTT replacing connect message
MQTT Broker Unreachable!!
Let's publish message now. Memory free=47064
Connecting to 	mqtt://192.165.5.3:883/
478135174 MQTT replacing connect message
MQTT Broker Unreachable!!
Let's publish message now. Memory free=47064
Connecting to 	mqtt://192.165.5.3:883/
487385197 MQTT replacing connect message
MQTT Broker Unreachable!!
Let's publish message now. Memory free=47064
Connecting to 	mqtt://192.165.5.3:883/
496310119 MQTT replacing connect message
MQTT Broker Unreachable!!
Let's publish message now. Memory free=47064
Connecting to 	mqtt://192.165.5.3:883/
505510197 MQTT replacing connect message
MQTT Broker Unreachable!!
Let's publish message now. Memory free=47064
Connecting to 	mqtt://192.165.5.3:883/
514535152 MQTT replacing connect message
MQTT Broker Unreachable!!
Let's publish message now. Memory free=46888
Connecting to 	mqtt://192.165.5.3:883/
523735191 MQTT replacing connect message
MQTT Broker Unreachable!!
Let's publish message now. Memory free=46888
Connecting to 	mqtt://192.165.5.3:883/
532735142 MQTT replacing connect message
MQTT Broker Unreachable!!
Let's publish message now. Memory free=46888
Connecting to 	mqtt://192.165.5.3:883/
541760153 MQTT replacing connect message
MQTT Broker Unreachable!!
Let's publish message now. Memory free=46888
Connecting to 	mqtt://192.165.5.3:883/
550910125 MQTT replacing connect message
MQTT Broker Unreachable!!
Let's publish message now. Memory free=46888
Connecting to 	mqtt://192.165.5.3:883/
560310159 MQTT replacing connect message
MQTT Broker Unreachable!!
Let's publish message now. Memory free=46888
Connecting to 	mqtt://192.165.5.3:883/
569485131 MQTT replacing connect message
MQTT Broker Unreachable!!
Let's publish message now. Memory free=46888
Connecting to 	mqtt://192.165.5.3:883/
578560152 MQTT replacing connect message

@slaff
Copy link
Contributor

slaff commented Jul 6, 2019

Stable for a while then one drop later. But that might have had other reasons.

The first is expected and the second is something that I have noticed multiple times. The updated PR addresses the memory leak issue and I plan to merge it later today or tomorrow if there are no objections.

@slaff slaff merged commit 157cb65 into SmingHub:develop Jul 7, 2019
@mikee47 mikee47 deleted the fix/mqtt_memleak branch July 7, 2019 08:19
slaff pushed a commit to slaff/Sming that referenced this pull request Jul 7, 2019
* Handle connect message outside of queue, always give it priority
* In `copyString`, free destination buffer first to avoid memory leaks
* Use helper functions to create, delete and clear messages
* Use `MQTT_MALLOC` instead of `malloc` for messages and other items

* Remove COPY_STRING macro and handle allocation failures
slaff pushed a commit to slaff/Sming that referenced this pull request Jul 7, 2019
* Handle connect message outside of queue, always give it priority
* In `copyString`, free destination buffer first to avoid memory leaks
* Use helper functions to create, delete and clear messages
* Use `MQTT_MALLOC` instead of `malloc` for messages and other items

* Remove COPY_STRING macro and handle allocation failures
@slaff slaff removed the 0 - Backlog label Jul 7, 2019
slaff added a commit that referenced this pull request Jul 7, 2019
* Handle connect message outside of queue, always give it priority
* In `copyString`, free destination buffer first to avoid memory leaks
* Use helper functions to create, delete and clear messages
* Use `MQTT_MALLOC` instead of `malloc` for messages and other items

* Remove COPY_STRING macro and handle allocation failures
@slaff slaff mentioned this pull request Sep 28, 2019
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Memory leak in MqttClient connect
3 participants