Expire negotiation on event loop
[netconf.git] / netconf / netconf-netty-util / src / main / java / org / opendaylight / netconf / nettyutil / AbstractNetconfSessionNegotiator.java
index 9c713f1b5c3c5f50feaa542b9a06b3d57b390f0d..21fb3b8e8626df031fea574e08a9623ac6654c86 100644 (file)
@@ -23,6 +23,7 @@ import io.netty.util.concurrent.GenericFutureListener;
 import io.netty.util.concurrent.Promise;
 import java.util.concurrent.TimeUnit;
 import org.checkerframework.checker.lock.qual.GuardedBy;
+import org.checkerframework.checker.lock.qual.Holding;
 import org.eclipse.jdt.annotation.Nullable;
 import org.opendaylight.netconf.api.NetconfDocumentedException;
 import org.opendaylight.netconf.api.NetconfMessage;
@@ -63,8 +64,8 @@ public abstract class AbstractNetconfSessionNegotiator<P extends NetconfSessionP
     private final L sessionListener;
     private final Timer timer;
 
+    @GuardedBy("this")
     private Timeout timeoutTask;
-
     @GuardedBy("this")
     private State state = State.IDLE;
 
@@ -118,15 +119,26 @@ public abstract class AbstractNetconfSessionNegotiator<P extends NetconfSessionP
         sendMessage(helloMessage);
 
         replaceHelloMessageOutboundHandler();
-        changeState(State.OPEN_WAIT);
 
-        timeoutTask = this.timer.newTimeout(this::timeoutExpired, connectionTimeoutMillis, TimeUnit.MILLISECONDS);
+        synchronized (this) {
+            lockedChangeState(State.OPEN_WAIT);
+
+            // Service the timeout on channel's eventloop, so that we do not get state transition problems
+            timeoutTask = timer.newTimeout(unused -> channel.eventLoop().execute(this::timeoutExpired),
+                connectionTimeoutMillis, TimeUnit.MILLISECONDS);
+        }
     }
 
-    private synchronized void timeoutExpired(final Timeout timeout) {
+    private synchronized void timeoutExpired() {
+        if (timeoutTask == null) {
+            // cancelTimeout() between expiry and execution on the loop
+            return;
+        }
+        timeoutTask = null;
+
         if (state != State.ESTABLISHED) {
-            LOG.debug("Connection timeout after {}, session backed by channel {} is in state {}", timeout, channel,
-                state);
+            LOG.debug("Connection timeout after {}ms, session backed by channel {} is in state {}",
+                connectionTimeoutMillis, channel, state);
 
             // Do not fail negotiation if promise is done or canceled
             // It would result in setting result of the promise second time and that throws exception
@@ -150,9 +162,10 @@ public abstract class AbstractNetconfSessionNegotiator<P extends NetconfSessionP
 
     @SuppressFBWarnings(value = "UPM_UNCALLED_PRIVATE_METHOD",
             justification = "https://github.com/spotbugs/spotbugs/issues/811")
-    private void cancelTimeout() {
-        if (timeoutTask != null) {
-            timeoutTask.cancel();
+    private synchronized void cancelTimeout() {
+        if (timeoutTask != null && !timeoutTask.cancel()) {
+            // Late-coming cancel: make sure
+            timeoutTask = null;
         }
     }
 
@@ -226,6 +239,11 @@ public abstract class AbstractNetconfSessionNegotiator<P extends NetconfSessionP
             throws NetconfDocumentedException;
 
     private synchronized void changeState(final State newState) {
+        lockedChangeState(newState);
+    }
+
+    @Holding("this")
+    private void lockedChangeState(final State newState) {
         LOG.debug("Changing state from : {} to : {} for channel: {}", state, newState, channel);
         checkState(isStateChangePermitted(state, newState),
                 "Cannot change state from %s to %s for channel %s", state, newState, channel);