From ba9a6162304ccbfbab2bdc7c4c0e7bf3d8d5895c Mon Sep 17 00:00:00 2001 From: Mateusz Warowny Date: Mon, 28 Mar 2022 15:50:14 +0200 Subject: [PATCH] JAL-3878 Fix discoverer being stuck on BEGIN state if task was cancelled. --- .../ws2/client/api/AbstractWebServiceDiscoverer.java | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) 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; } -- 1.7.10.2