From 8efcc9005424dced445a6fc78231baccdc2b9c58 Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Sat, 29 Feb 2020 12:18:28 +0100 Subject: [PATCH] Filter registered listeners There is a small race window where the registration could be marked as unregistered and not removed from the map. Check that condition before letting the listener through. Change-Id: I1938e6459faaaadf69bc7907ba6ba71e65fe9e8f Signed-off-by: Robert Varga --- .../yangtools/util/ListenerRegistry.java | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/common/util/src/main/java/org/opendaylight/yangtools/util/ListenerRegistry.java b/common/util/src/main/java/org/opendaylight/yangtools/util/ListenerRegistry.java index d042b8be61..100acc9eb9 100644 --- a/common/util/src/main/java/org/opendaylight/yangtools/util/ListenerRegistry.java +++ b/common/util/src/main/java/org/opendaylight/yangtools/util/ListenerRegistry.java @@ -26,7 +26,7 @@ import org.opendaylight.yangtools.concepts.Mutable; * @param Type of listeners this registry handles */ public final class ListenerRegistry implements Mutable { - private final Set> listeners = ConcurrentHashMap.newKeySet(); + private final Set> listeners = ConcurrentHashMap.newKeySet(); private final String name; private ListenerRegistry(final String name) { @@ -42,7 +42,7 @@ public final class ListenerRegistry implements Mutable } public void clear() { - listeners.stream().forEach(ListenerRegistration::close); + listeners.stream().forEach(RegistrationImpl::close); } public boolean isEmpty() { @@ -50,11 +50,11 @@ public final class ListenerRegistry implements Mutable } public Stream streamListeners() { - return listeners.stream().map(ListenerRegistration::getInstance); + return listeners.stream().filter(RegistrationImpl::notClosed).map(RegistrationImpl::getInstance); } public @NonNull ListenerRegistration register(final L listener) { - final ListenerRegistration ret = new ListenerRegistrationImpl<>(listener, listeners); + final RegistrationImpl ret = new RegistrationImpl<>(listener, listeners); listeners.add(ret); return ret; } @@ -67,11 +67,10 @@ public final class ListenerRegistry implements Mutable .toString(); } - private static final class ListenerRegistrationImpl - extends AbstractListenerRegistration { + private static final class RegistrationImpl extends AbstractListenerRegistration { private Collection removeFrom; - ListenerRegistrationImpl(final T instance, final Collection removeFrom) { + RegistrationImpl(final T instance, final Collection removeFrom) { super(instance); this.removeFrom = requireNonNull(removeFrom); } -- 2.36.6