eclipse-paho / eclipse-paho/paho.mqtt.java

Resource/threads leak if interrupted while connecting which prevents application shutdown

Open
#399 3 comments 1 reaction 0 assignees View on GitHub
Dominant language
Java
Stars
2.3k
Forks
919
PR merge metrics
No merged PRs in 30d

Description

- [X] Bug exists Release Version 1.2.0 ( Master Branch)
- [X] Bug exists Release Version 1.1.1

I'm still new to paho but I think I found a case where there are some resource leaks:
You do a client connect and before it finishes connecting you trigger an interrupt on the thread. This is happening in a Spring Boot application but I looked at the code and it looks independent of Spring.

The issue is that if the client is interrupted while connecting but before it connects, there are some threads left running and the application doesn't end because of these paho threads still running (MQTT Call, MQTT Ping, MQTT Rec, MQTT Snd).
This is the relevant stack trace for the interrupt
```
org.eclipse.paho.client.mqttv3.MqttException: MqttException
at org.eclipse.paho.client.mqttv3.internal.Token.waitForResponse(Token.java:148)
at org.eclipse.paho.client.mqttv3.internal.Token.waitForCompletion(Token.java:108)
at org.eclipse.paho.client.mqttv3.MqttToken.waitForCompletion(MqttToken.java:67)
at org.eclipse.paho.client.mqttv3.MqttClient.connect(MqttClient.java:333)
...
Caused by: java.lang.InterruptedException: null
at java.lang.Object.wait(Native Method)
at java.lang.Object.wait(Object.java:502)
at org.eclipse.paho.client.mqttv3.internal.Token.waitForResponse(Token.java:143)
... 14 common frames omitted
```

I figured it was because as part of my cleanup procedure I wasn't calling `MqttClient.close()` to terminate those threads. But if I try to do that, `close` itself throws an exception because the `ClientComms` is left forever in the _Connecting_ state due to the previous interrupt. Any attempt to close it ends with
```
Caused by: org.eclipse.paho.client.mqttv3.MqttException: Connect already in progress
at org.eclipse.paho.client.mqttv3.internal.ClientComms.close(ClientComms.java:220)
at org.eclipse.paho.client.mqttv3.MqttAsyncClient.close(MqttAsyncClient.java:1595)
at org.eclipse.paho.client.mqttv3.MqttClient.close(MqttClient.java:626)
```
This ends up in a resource leak where paho is just uncloseable.
This happens both in 1.2.0 and 1.1.1

Any workarounds would be welcomed

Thank you

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.