Skip to content

Commit

Permalink
[tibber] fix possible resource leak (openhab#8033)
Browse files Browse the repository at this point in the history
Signed-off-by: Jan N. Klug <[email protected]>
  • Loading branch information
J-N-K authored and markus7017 committed Sep 18, 2020
1 parent 476d761 commit 8400422
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
4 changes: 3 additions & 1 deletion bundles/org.openhab.binding.tibber/pom.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.net.URISyntaxException;
import java.util.Properties;
import java.util.concurrent.Executor;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

Expand Down Expand Up @@ -75,6 +76,7 @@ public class TibberHandler extends BaseThingHandler {
private @Nullable Session session;
private @Nullable WebSocketClient client;
private @Nullable ScheduledFuture<?> pollingJob;
private @Nullable Future<?> sessionFuture;
private String rtEnabled = "false";

public TibberHandler(Thing thing) {
Expand Down Expand Up @@ -326,7 +328,7 @@ public void open() {
}
try {
logger.debug("Connecting Websocket connection");
client.connect(socket, new URI(SUBSCRIPTION_URL), newRequest);
sessionFuture = client.connect(socket, new URI(SUBSCRIPTION_URL), newRequest);
} catch (IOException e) {
logger.warn("Websocket Connect Exception: {}", e.getMessage());
} catch (URISyntaxException e) {
Expand Down Expand Up @@ -354,6 +356,16 @@ public void close() {
this.session = null;
this.socket = null;
}
if (sessionFuture != null && !sessionFuture.isDone()) {
sessionFuture.cancel(true);
}
if (client != null && client.isStarted()) {
try {
client.stop();
} catch (Exception e) {
logger.warn("Failed to stop websocket client: {}", e.getMessage());
}
}
}

public boolean isConnected() {
Expand Down

0 comments on commit 8400422

Please sign in to comment.