MQTTnet
MQTTnet copied to clipboard
Unknown memory leak
Describe the bug
Memory usage runaway.
Which component is your bug related to?
Server
To Reproduce
Steps to reproduce the behavior:
- Using version v4.3.0.858
- Set up a simple server application with the following events ValidatingConnectionAsync - Checks clients against database InterceptingPublishAsync - Just to increase a counter to keep stats on number of msg handled LoadingRetainedMessageAsync - Uses code from samples RetainedMessageChangedAsync - Uses code from samples
- The server has the following options:
var options = new MqttServerOptionsBuilder().
WithPersistentSessions(true).
WithConnectionBacklog(2000).
WithDefaultEndpointPort(1883).
WithDefaultEndpoint().
WithKeepAlive().
WithMaxPendingMessagesPerClient(20000);
- See error.
Expected behavior
The memory should vary with the number of connected clients and number of persistent messages in memory.
Screenshots
Additional context / logging
The server also has a timer, that once a sec updates the GUI with info about: connected clients, numb of processed msg and msg/sec. Before I also displayed all connected clients in a ListView (WPF). Has worked without issue, but that was before everything was made asynchronous.
I made a client-app that spams the server with both connections and messages that should be persistent to try to find the leak. I have not used the memory profiler before, but if I understand it correctly it has something to do with getting the connected client count.
Let me know if I'm missing something.
Anyone has any idea what is going on here? Am I reading the memory profiling correctly?
How many clients to you connect? Please also share the code how you publish the messages.
hm, during a normal day there about 120-150 stable connections, meaning clients that says connected for longer periods of time. I've also got a PHP backend that connects to it using the MQTT client from karpy47
The environment where I use your broker, is where I have a web based portal and an android app. Changes made on the portal are pushed out to the app, so the app dont need to be polling the server. The app can affect each other, but they need to notify the server of the change and it will then push it out the whom ever needs that info.
So the backend only connects, publishes and then disconnects, and the code is very simple:
$client = new MQTTClient($server, $port);
function connectToMQTT() {
return $client->sendConnect(uniqid());
}
function sendMsg($topic, $dataArr) {
$client->sendPublish($topic, json_encode($dataArr));
}
function closeConnection() {
$client->sendDisconnect();
$client->close();
}
Then it's used like:
include 'pushMsgHandler.php';
if (connectToMQTT()) {
foreach($newCandy as $candy) {
*/ Some logic and stuff */
$dataOut = [];
$dataOut['NAME']=$candy['NAME'];
$dataOut['TYPE']=$candy['TYPE'];
sendMsg('*some ID*/eventName', $dataOut);
}
}
closeConnection();
It's sent with QoS 1 (default).
The server also has a timer that publishes to all clients 3 times per day, telling them to sync up with the server, in case something did get missed. The is published with this:
var message = new MqttApplicationMessageBuilder()
.WithTopic(topic)
.WithPayload(msg)
.WithQualityOfServiceLevel(MqttQualityOfServiceLevel.ExactlyOnce)
.Build();
mqttServer.InjectApplicationMessage(
new InjectedMqttApplicationMessage(message)
{
SenderClientId = "broker"
});
Any ideas or additional info I can provide to help sort this out?
Could persistent messages be the issue here? I do need that feature, so it there anyway to implement a feature where the persistent messages are saved to a file instead of to memory? I feel like retaining does this, but I also get a feeling that that is different from persistent. Correct?