From e2e2963b99d4d3368368d4a8093fab426e14dafa Mon Sep 17 00:00:00 2001 From: woodser Date: Fri, 29 Dec 2023 10:28:45 -0500 Subject: [PATCH] ignore error sending message after shut down --- .../main/java/haveno/common/UserThread.java | 5 ++-- .../network/p2p/network/NetworkNode.java | 24 +++++++++---------- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/common/src/main/java/haveno/common/UserThread.java b/common/src/main/java/haveno/common/UserThread.java index 57b6e635a8..a08d6f9545 100644 --- a/common/src/main/java/haveno/common/UserThread.java +++ b/common/src/main/java/haveno/common/UserThread.java @@ -58,7 +58,7 @@ public class UserThread { } public static void execute(Runnable command) { - UserThread.executor.execute(() -> { + executor.execute(() -> { Thread.currentThread().setName(USER_THREAD_NAME); command.run(); }); @@ -79,7 +79,8 @@ public class UserThread { } } - public static boolean isUserThread(Thread thread) { + // TODO: better way to determine if on UserThread, since this is not reliable + private static boolean isUserThread(Thread thread) { return USER_THREAD_NAME.equals(thread.getName()); } 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 16ad234127..a0a8bbf07d 100644 --- a/p2p/src/main/java/haveno/network/p2p/network/NetworkNode.java +++ b/p2p/src/main/java/haveno/network/p2p/network/NetworkNode.java @@ -306,27 +306,25 @@ public abstract class NetworkNode implements MessageListener { } public void onFailure(@NotNull Throwable throwable) { - UserThread.execute(() -> { - if (!resultFuture.setException(throwable)) { - // In case the setException returns false we need to cancel the future. - resultFuture.cancel(true); - } - }); + UserThread.execute(() -> resolveWithException(resultFuture, throwable)); } }, MoreExecutors.directExecutor()); } catch (RejectedExecutionException exception) { - log.error("RejectedExecutionException at sendMessage: ", exception); - UserThread.execute(() -> { - if (!resultFuture.setException(exception)) { - // In case the setException returns false we need to cancel the future. - resultFuture.cancel(true); - } - }); + if (!executor.isShutdown()) { + log.error("RejectedExecutionException at sendMessage: ", exception); + UserThread.execute(() -> resolveWithException(resultFuture, exception)); + } } return resultFuture; } + private void resolveWithException(SettableFuture future, Throwable exception) { + if (!future.setException(exception)) { + future.cancel(true); // In case the setException returns false we need to cancel the future. + } + } + public ReadOnlyObjectProperty nodeAddressProperty() { return nodeAddressProperty; }