Fix for ONOS-5783.java.lang.IllegalStateException: session is expired

Change-Id: I540b3adb1a098b51bb8231ef724d5f4e19a24cc9
diff --git a/core/store/primitives/src/main/java/org/onosproject/store/primitives/resources/impl/AtomixLeaderElectorState.java b/core/store/primitives/src/main/java/org/onosproject/store/primitives/resources/impl/AtomixLeaderElectorState.java
index 33414e7..cb7d63a 100644
--- a/core/store/primitives/src/main/java/org/onosproject/store/primitives/resources/impl/AtomixLeaderElectorState.java
+++ b/core/store/primitives/src/main/java/org/onosproject/store/primitives/resources/impl/AtomixLeaderElectorState.java
@@ -125,9 +125,21 @@
      * @param commit listen commit
      */
     public void listen(Commit<? extends Listen> commit) {
+        Long sessionId = commit.session().id();
         if (listeners.putIfAbsent(commit.session().id(), commit) != null) {
             commit.close();
         }
+        commit.session()
+                .onStateChange(
+                        state -> {
+                            if (state == ServerSession.State.CLOSED
+                                    || state == ServerSession.State.EXPIRED) {
+                                Commit<? extends Listen> listener = listeners.remove(sessionId);
+                                if (listener != null) {
+                                    listener.close();
+                                }
+                            }
+                        });
     }
 
     /**