JAL-3878 Fix discoverer being stuck on BEGIN state if task was cancelled.
authorMateusz Warowny <mmzwarowny@dundee.ac.uk>
Mon, 28 Mar 2022 13:50:14 +0000 (15:50 +0200)
committerMateusz Warowny <mmzwarowny@dundee.ac.uk>
Mon, 28 Mar 2022 13:52:00 +0000 (15:52 +0200)
src/jalview/ws2/client/api/AbstractWebServiceDiscoverer.java

index 27a6660..6a81410 100644 (file)
@@ -133,6 +133,7 @@ public abstract class AbstractWebServiceDiscoverer implements WebServiceDiscover
   @Override
   public synchronized final CompletableFuture<List<WebService<?>>> startDiscoverer()
   {
+    Cache.log.debug("Requesting service discovery");
     while (true)
     {
       if (state.get() == AGAIN)
@@ -141,6 +142,7 @@ public abstract class AbstractWebServiceDiscoverer implements WebServiceDiscover
       }
       if (state.compareAndSet(END, BEGIN) || state.compareAndSet(BEGIN, AGAIN))
       {
+        Cache.log.debug("State changed to " + state.get());
         final var oldTask = discoveryTask;
         CompletableFuture<List<WebService<?>>> task = oldTask
             .handleAsync((_r, _e) -> {
@@ -161,17 +163,22 @@ public abstract class AbstractWebServiceDiscoverer implements WebServiceDiscover
               }
               return services = allServices;
             });
-        task.thenAccept(services -> {
+        task.<Void>handle((services, exception) -> {
           while (true)
           {
             if (state.get() == END)
               // should never happen, throw exception to break the loop just in case
               throw new AssertionError();
             if (state.compareAndSet(BEGIN, END) || state.compareAndSet(AGAIN, BEGIN))
+              Cache.log.debug("Discovery ended, state is " + state.get());
               break;
           }
-          fireServicesChanged(services);
+          if (services != null)
+            fireServicesChanged(services);
+          return null;
         });
+        Cache.log.debug("Spawned task " + task);
+        Cache.log.debug("Killing task " + oldTask);
         oldTask.cancel(false);
         return discoveryTask = task;
       }