-
Notifications
You must be signed in to change notification settings - Fork 160
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
Async Mqtt3Client does not release threads created by publishes() when disconnected #633
Comments
I've observed the same behaviour. When one closes the connection "manually" ( hivemq-mqtt-client/src/main/java/com/hivemq/client/mqtt/lifecycle/MqttDisconnectSource.java Lines 27 to 30 in bb5dee5
the thread(s) are not stopped and one ends up with multiple com.hivemq.client.mqtt-*-* threads.
To me it seems as the culprit is the logic within: Lines 183 to 201 in bb5dee5
as channel.close() is not called when the disconnect source is USER .
I might be wrong though as I've seen |
In our case this is especially bad, since we have now two threads trying to reconnect, so each thread disconnects the other one when connection is established succesfully... |
I think this PR was created trying to fix this issue: #638
|
This is not what is causing this issue because the channel is actually closed, only later: Line 110 in bb5dee5
or here: Line 274 in bb5dee5
So you are right about:
|
This sounds strange, threads do not reconnect or disconnect any connections. Do you mean two MQTT client instances (instead of threads) that reconnect, one in a not-terminated process and one in a newly started process? |
@ASunc Your issue is probably that we keep the threads alive as long as the client has a session (if I remember this correctly). With MQTT 3 cleanSession=false means the session never expires. This might need to be improved. |
Maybe, I'm indeed using cleanSession=false. Still, the behavior this way is odd, as when the client is disconnected one would expect that everything is released. Using cleanSession=true is not a valid choice for me, because that would loose all the messages buffered by broker during reconnect. So, I agree that there is certainly room for improvement here. |
To avoid misunderstandings, I did not mean that your usage of cleanSession needs to be improved. Instead I mean that the handling of releasing the threads when using cleanSession=false and callbacks needs to be improved. Declaring threads as daemon threads (as proposed by @ViToni) is not a proper fix for that, but rather a workaround. It might change the behavior in a breaking way for other users of the library. import io.netty.util.concurrent.DefaultThreadFactory;
import io.netty.util.concurrent.ThreadPerTaskExecutor;
...
.executorConfig()
.nettyExecutor(new ThreadPerTaskExecutor(new DefaultThreadFactory(
"com.hivemq.client.mqtt.daemon", true, Thread.MAX_PRIORITY)))
.applyExecutorConfig()`
... |
Using daemon threads won't help me. Everything runs under application server and it is kept running unless there are software updates. If any connection drops and reconnects occur during this time the daemon threads will be laying around just like normal threads. |
If the application keeps running, then this should not be an issue, because the number of netty threads is bounded by the number of available processors (times 2). The number of netty threads is decoupled from how many MQTT clients you create. An MQTT client instance does not exclusively hold a thread, the threads are shared. Reading the following from the issue description, I think you might think that the number of
Do you see more of these threads than expected (available processors times 2)? |
Excellent! I have 24 logical processors according to windows task manager and indeed it seems that there are no more than 48 of those threads. The amount is just so high that I never tested things that far. This means that there is no major problem after all. |
Please bear with me, If I used the wrong wording.
which concurrently try to establish a connection. This happens when we try to close the current session and try to establish a new one with a changed configuration (and we can't use |
It was not meant as a fix but rather as a means to reduce impact of the created threads. I'd rather love to see it more fool proof to use so that it would be more difficult on my side to use it in an unexpected way. |
🐛 Bug Report
🔬 How To Reproduce
Steps to reproduce the behavior:
Code sample
public class HiveTest {
}
Environment
Where are you running/using this client? Windows 11
Hardware or Device? Dell Laptop
What version of this client are you using? 1.3.3
JVM version? 11.0.22.7
Operating System? Windows 11
Which MQTT protocol version is being used? 3
Which MQTT broker (name and version)? mosquitto 2.0.18
Screenshots
📈 Expected behavior
When disconnecting, I would expect that thread would be terminated. Don't confuse this
thread with various RxComputation threads, I know that number of those are capped.
📎 Additional context
The text was updated successfully, but these errors were encountered: