-
Notifications
You must be signed in to change notification settings - Fork 41
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
Memory leak when clients connected and disconnected #73
Comments
Erronenous analysis below, for longevity (I removed this from the description because I found it's incorrect) Looking at the TinyMqtt code, I'm not familiar with Arduino/ESP32 networking, but I'm confused that I don't see any code that ever appears to remove a client. Walking through the add code, for the normal mode (without TINY_MQTT_ASYNC):
In the disconnect case, this cleanup code never seems to actually remove the client from the clients list:
|
yes, this is what I found too, that seems to be the root cause and solved it in my fork (long time ago, PR23 AFAIR). The "release" of the client is done somewhere else, but sometimes the pointers got lost. My version runs on two devices for weeks, the official version stops working after less than one day (reproducible). I have seen that someone has tried to fix this through use of auto pointers, but that seems not to work reliably either. |
Can you link me the branch/tag/... you're using? |
When disconnecting, the client needs to be disabled and assuming it is a client created by the broker, it needs to be deleted. Previously the client was directly removed from the broker, thereby losing any reference to it and it could never be deleted. This changes that behavior to purely invalidate the client, and the next time through the broker loop it will be properly cleaned up. This also fixes the loop code to properly erase the client from the list, and not break out early the first time it finds a disconnected client. This doesn't fix everything, in that it seems like there's another bug that it is deleting the client without a flag check. However I'm leaving that for a separate fix. I have tested this on my ESP32 by uploading this version of the code, running a client in an infinite loop which connects, publishes, disconnects. With the old code I can steadily see memory usage linearly leaking over time. With the new version it stays stable after the first couple times. More in the linked issue. Fixes hsaturn#73
Debugging more, I think at least part of the issue is this:
that final Diff incoming. Diff is #74 |
|
When disconnecting, the client needs to be disabled and assuming it is a client created by the broker, it needs to be deleted. Previously the client was directly removed from the broker, thereby losing any reference to it and it could never be deleted. This changes that behavior to purely invalidate the client, and the next time through the broker loop it will be properly cleaned up. This also fixes the loop code to properly erase the client from the list, and not break out early the first time it finds a disconnected client. This doesn't fix everything, in that it seems like there's another bug that it is deleting the client without a flag check. However I'm leaving that for a separate fix. I have tested this on my ESP32 by uploading this version of the code, running a client in an infinite loop which connects, publishes, disconnects. With the old code I can steadily see memory usage linearly leaking over time. With the new version it stays stable after the first couple times. More in the linked issue. Fixes hsaturn#73
Ok, with some more minor tweaks to my branch, at least for me it's completely stable at the moment. I have two remote clients ( Also I should note, for me to be able to work cleanly I needed #72 and #74. My main branch has both of those on it, and otherwise are on top of @dg6nee give my branch (richievos/main) a try at some point. |
Follow-up 24hrs later. I haven't been fully stress testing the device like I was yesterday, but I have not seen any anomalous memory behavior since introducing #74 (on top of #72). I'm sure there's other memory leaks in here, but at least for the use-case of lots of short-lived tcp clients, I believe my PR fixes the issue. |
Hello Thanks for the report. |
... working on it right now ! |
With examples/tinyqtt-tests.ino + MobaXTerm, I clearly saw that each client trying to connect and then disconnect was making a memory leak. TinyMqtt tests shows periodically the number of clients connected as well as free memory. The free mem goes down when connecting and up when disconnecting. With the main branch (since 6b9d764) I cannot see any memory leak. |
I haven't tested this yet, but can you clarify if you fixed a different leak in that commit you referenced or of you're saying you fixed the leak I reported and fixed in #74 ? I'm assuming you're fixing a different bug based on the code changes and your earlier statements. However your recent comment leads me to believe you're saying it fixes both. |
I should note, I don't think that commit would fix the bug I was encountering and I fixed in #74, but I'm happy to be proven wrong. |
Hello Richie I do not consider yet this issue (#73) as closed. I've fixed a memory leak when multiple clients are connecting and disconnecting. |
Hi Everyone! I'm very intrested about this library. I used it long mounths ago with enc28j60 /tobozo with cable connection between my devices. Sometimes when i pull out a clients cable, the broker is freezing and stop working. I knoew this is not a normally using of tinymqtt beacuse i paired this with enc library. I begin to listen the free heap and when the client disconnect the memory is begin decraese until it totaly stop working. Still anyone similar problem? |
I have a simple MqttBroker setup, listening on a socket. I have been doing some testing that requires repeated connection opening/closing and I'm getting weird behavior. I added some more debugging and found there appears to be a memory leak related to TinyMqtt.
To reproduce this I am doing the following:
I then have my esp periodically dumping memory stats. Effectively:
With that, I'm seeing every time I connect I have less memory available.
EDIT: I had some thoughts below on what was causing this, but I now found I was incorrect. I'm still seeing the memory leak, but I'm not clear what's causing it.
My guess is it's not as simple as the
clients
list not getting purged (I think I understand how that's happening now), but that the MqttClient itself isn't getting cleaned up.The text was updated successfully, but these errors were encountered: