From d889d6b94cdfd791eff119a78766ad867ef88f0b Mon Sep 17 00:00:00 2001 From: Thomas Lauterbach <2452988+DrRSatzteil@users.noreply.github.com> Date: Thu, 21 Oct 2021 00:08:18 +0200 Subject: [PATCH] [enocean] Fix concurrency exception on startup (#11408) * made listeners a ConcurrentHashMap * synchronized access to listeners and eventListeners collections of EnOceanTransceiver * reverted ConcurrentHashMap change as NULL-Key is required Fixes #11393 Also-by: Daniel Weber Signed-off-by: Thomas Lauterbach --- .../transceiver/EnOceanTransceiver.java | 21 ++++++++++++------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/bundles/org.openhab.binding.enocean/src/main/java/org/openhab/binding/enocean/internal/transceiver/EnOceanTransceiver.java b/bundles/org.openhab.binding.enocean/src/main/java/org/openhab/binding/enocean/internal/transceiver/EnOceanTransceiver.java index 55be4988b0259..625798e9703d4 100644 --- a/bundles/org.openhab.binding.enocean/src/main/java/org/openhab/binding/enocean/internal/transceiver/EnOceanTransceiver.java +++ b/bundles/org.openhab.binding.enocean/src/main/java/org/openhab/binding/enocean/internal/transceiver/EnOceanTransceiver.java @@ -220,6 +220,7 @@ public void ShutDown() { readingTask = null; timeOut = null; listeners.clear(); + eventListeners.clear(); teachInListener = null; errorListener = null; @@ -302,9 +303,11 @@ protected void informListeners(BasePacket packet) { } long s = Long.parseLong(HexUtils.bytesToHex(senderId), 16); - HashSet pl = listeners.get(s); - if (pl != null) { - pl.forEach(l -> l.packetReceived(msg)); + synchronized (this) { + HashSet pl = listeners.get(s); + if (pl != null) { + pl.forEach(l -> l.packetReceived(msg)); + } } } } else { @@ -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); @@ -374,13 +379,13 @@ public void sendBasePacket(BasePacket packet, ResponseListener 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 pl = listeners.get(senderIdToListenTo); if (pl != null) { pl.remove(listener); @@ -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); }