Skip to content

Commit

Permalink
[lifx] Improve InterruptedException handling (openhab#11653)
Browse files Browse the repository at this point in the history
When the binding is stopped sleeping threads are interrupted by design.
By throwing the InterruptedException, it should be caught in LifxSelectorUtil.sendPacket (which is waiting for the packet interval to elapse), which will then abort sending a packet.

This prevents:

```
[ERROR] [lifx.internal.util.LifxThrottlingUtil] - An exception occurred while putting the thread to sleep : 'sleep interrupted'
```

Signed-off-by: Wouter Born <github@maindrain.net>
  • Loading branch information
wborn authored and volkmarnissen committed Feb 17, 2022
1 parent f7b6667 commit be3a24c
Showing 1 changed file with 4 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public long getTimestamp() {

private static Map<MACAddress, LifxLightCommunicationTracker> macTrackerMapping = new ConcurrentHashMap<>();

public static void lock(@Nullable MACAddress mac) {
public static void lock(@Nullable MACAddress mac) throws InterruptedException {
if (mac != null) {
LifxLightCommunicationTracker tracker = getOrCreateTracker(mac);
tracker.lock();
Expand All @@ -108,14 +108,10 @@ private static LifxLightCommunicationTracker getOrCreateTracker(MACAddress mac)
return tracker;
}

private static void waitForNextPacketInterval(long timestamp) {
private static void waitForNextPacketInterval(long timestamp) throws InterruptedException {
long timeToWait = Math.max(PACKET_INTERVAL - (System.currentTimeMillis() - timestamp), 0);
if (timeToWait > 0) {
try {
Thread.sleep(timeToWait);
} catch (InterruptedException e) {
LOGGER.error("An exception occurred while putting the thread to sleep : '{}'", e.getMessage());
}
Thread.sleep(timeToWait);
}
}

Expand All @@ -130,7 +126,7 @@ public static void unlock(@Nullable MACAddress mac) {
}
}

public static void lock() {
public static void lock() throws InterruptedException {
long lastStamp = 0;
for (LifxLightCommunicationTracker tracker : trackers) {
tracker.lock();
Expand Down

0 comments on commit be3a24c

Please sign in to comment.