From: Mateusz Warowny Date: Mon, 28 Mar 2022 13:50:14 +0000 (+0200) Subject: JAL-3878 Fix discoverer being stuck on BEGIN state if task was cancelled. X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=ba9a6162304ccbfbab2bdc7c4c0e7bf3d8d5895c;p=jalview.git JAL-3878 Fix discoverer being stuck on BEGIN state if task was cancelled. --- diff --git a/src/jalview/ws2/client/api/AbstractWebServiceDiscoverer.java b/src/jalview/ws2/client/api/AbstractWebServiceDiscoverer.java index 27a6660..6a81410 100644 --- a/src/jalview/ws2/client/api/AbstractWebServiceDiscoverer.java +++ b/src/jalview/ws2/client/api/AbstractWebServiceDiscoverer.java @@ -133,6 +133,7 @@ public abstract class AbstractWebServiceDiscoverer implements WebServiceDiscover @Override public synchronized final CompletableFuture>> 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>> task = oldTask .handleAsync((_r, _e) -> { @@ -161,17 +163,22 @@ public abstract class AbstractWebServiceDiscoverer implements WebServiceDiscover } return services = allServices; }); - task.thenAccept(services -> { + task.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; }