Skip to content

Commit

Permalink
[enocean] Fix concurrency exception on startup (openhab#11408)
Browse files Browse the repository at this point in the history
* made listeners a ConcurrentHashMap
* synchronized access to listeners and eventListeners collections of EnOceanTransceiver
* reverted ConcurrentHashMap change as NULL-Key is required

Fixes openhab#11393

Also-by: Daniel Weber <uni@fruggy.de>
Signed-off-by: Thomas Lauterbach <lauterbachthomas@gmail.com>
  • Loading branch information
DrRSatzteil authored and volkmarnissen committed Feb 17, 2022
1 parent eb214c8 commit b6b99d7
Showing 1 changed file with 13 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ public void ShutDown() {
readingTask = null;
timeOut = null;
listeners.clear();
eventListeners.clear();
teachInListener = null;
errorListener = null;

Expand Down Expand Up @@ -302,9 +303,11 @@ protected void informListeners(BasePacket packet) {
}

long s = Long.parseLong(HexUtils.bytesToHex(senderId), 16);
HashSet<PacketListener> pl = listeners.get(s);
if (pl != null) {
pl.forEach(l -> l.packetReceived(msg));
synchronized (this) {
HashSet<PacketListener> pl = listeners.get(s);
if (pl != null) {
pl.forEach(l -> l.packetReceived(msg));
}
}
}
} else {
Expand All @@ -331,7 +334,9 @@ protected void informListeners(BasePacket packet) {
}
}

eventListeners.forEach(l -> l.eventReceived(event));
synchronized (this) {
eventListeners.forEach(l -> l.eventReceived(event));
}
}
} catch (Exception e) {
logger.error("Exception in informListeners", e);
Expand Down Expand Up @@ -374,13 +379,13 @@ public void sendBasePacket(BasePacket packet, ResponseListener<? extends Respons

protected abstract byte[] serializePacket(BasePacket packet) throws EnOceanException;

public void addPacketListener(PacketListener listener, long senderIdToListenTo) {
public synchronized void addPacketListener(PacketListener listener, long senderIdToListenTo) {
if (listeners.computeIfAbsent(senderIdToListenTo, k -> new HashSet<>()).add(listener)) {
logger.debug("Listener added: {}", senderIdToListenTo);
}
}

public void removePacketListener(PacketListener listener, long senderIdToListenTo) {
public synchronized void removePacketListener(PacketListener listener, long senderIdToListenTo) {
HashSet<PacketListener> pl = listeners.get(senderIdToListenTo);
if (pl != null) {
pl.remove(listener);
Expand All @@ -390,11 +395,11 @@ public void removePacketListener(PacketListener listener, long senderIdToListenT
}
}

public void addEventMessageListener(EventListener listener) {
public synchronized void addEventMessageListener(EventListener listener) {
eventListeners.add(listener);
}

public void removeEventMessageListener(EventListener listener) {
public synchronized void removeEventMessageListener(EventListener listener) {
eventListeners.remove(listener);
}

Expand Down

0 comments on commit b6b99d7

Please sign in to comment.