From ec9f91e014a337c1baf21eeddf232f1f88672e09 Mon Sep 17 00:00:00 2001 From: woodser Date: Sun, 30 Jun 2024 11:54:26 -0400 Subject: [PATCH] fix error on shut down broadcasting to peers --- .../java/haveno/network/p2p/network/NetworkNode.java | 10 ++++++---- .../haveno/network/p2p/peers/BroadcastHandler.java | 8 +++++++- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/p2p/src/main/java/haveno/network/p2p/network/NetworkNode.java b/p2p/src/main/java/haveno/network/p2p/network/NetworkNode.java index d74ef1fd..f24fdf6f 100644 --- a/p2p/src/main/java/haveno/network/p2p/network/NetworkNode.java +++ b/p2p/src/main/java/haveno/network/p2p/network/NetworkNode.java @@ -38,6 +38,7 @@ import com.google.common.util.concurrent.SettableFuture; import javafx.beans.property.ObjectProperty; import javafx.beans.property.ReadOnlyObjectProperty; import javafx.beans.property.SimpleObjectProperty; +import lombok.Getter; import java.net.ServerSocket; import java.net.Socket; @@ -82,7 +83,8 @@ public abstract class NetworkNode implements MessageListener { private final ListeningExecutorService sendMessageExecutor; private Server server; - private volatile boolean shutDownInProgress; + @Getter + private volatile boolean isShutDownStarted; // accessed from different threads private final CopyOnWriteArraySet outBoundConnections = new CopyOnWriteArraySet<>(); protected final ObjectProperty nodeAddressProperty = new SimpleObjectProperty<>(); @@ -181,7 +183,7 @@ public abstract class NetworkNode implements MessageListener { try { socket.close(); } catch (Throwable throwable) { - if (!shutDownInProgress) { + if (!isShutDownStarted) { log.error("Error at closing socket " + throwable); } } @@ -362,8 +364,8 @@ public abstract class NetworkNode implements MessageListener { public void shutDown(Runnable shutDownCompleteHandler) { log.info("NetworkNode shutdown started"); - if (!shutDownInProgress) { - shutDownInProgress = true; + if (!isShutDownStarted) { + isShutDownStarted = true; if (server != null) { server.shutDown(); server = null; diff --git a/p2p/src/main/java/haveno/network/p2p/peers/BroadcastHandler.java b/p2p/src/main/java/haveno/network/p2p/peers/BroadcastHandler.java index 238a509b..fca906ff 100644 --- a/p2p/src/main/java/haveno/network/p2p/peers/BroadcastHandler.java +++ b/p2p/src/main/java/haveno/network/p2p/peers/BroadcastHandler.java @@ -352,7 +352,13 @@ public class BroadcastHandler implements PeerManager.Listener { sendMessageFutures.stream() .filter(future -> !future.isCancelled() && !future.isDone()) - .forEach(future -> future.cancel(true)); + .forEach(future -> { + try { + future.cancel(true); + } catch (Exception e) { + if (!networkNode.isShutDownStarted()) throw e; + } + }); sendMessageFutures.clear(); peerManager.removeListener(this);