From 1a1b96ae3d9191e8309289e037d64d59fdbfe3ec 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 | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 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 ca1b79af06..964eb196fd 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 @@ -27,8 +27,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(); // This conversion is known to be safe. @SuppressWarnings({ "rawtypes", "unchecked" }) private final Set> unmodifiableView = (Set) Collections.unmodifiableSet(listeners); @@ -53,7 +52,7 @@ public final class ListenerRegistry implements Mutable } public void clear() { - listeners.stream().forEach(ListenerRegistration::close); + listeners.stream().forEach(RegistrationImpl::close); } public boolean isEmpty() { @@ -61,11 +60,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; } @@ -78,11 +77,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