From 30e199c56eba9d70286940b4f5678d0d6b8ab352 Mon Sep 17 00:00:00 2001 From: woodser Date: Sun, 17 Dec 2023 06:51:51 -0500 Subject: [PATCH] shut down trades before main wallet to finish processing --- .../haveno/core/app/HavenoExecutable.java | 9 ++++--- .../app/misc/ExecutableForAppWithP2p.java | 13 +++++++-- .../core/xmr/wallet/XmrWalletService.java | 27 ++++++++++--------- 3 files changed, 31 insertions(+), 18 deletions(-) diff --git a/core/src/main/java/haveno/core/app/HavenoExecutable.java b/core/src/main/java/haveno/core/app/HavenoExecutable.java index 4d222985f6..cfa844b684 100644 --- a/core/src/main/java/haveno/core/app/HavenoExecutable.java +++ b/core/src/main/java/haveno/core/app/HavenoExecutable.java @@ -346,6 +346,7 @@ public abstract class HavenoExecutable implements GracefulShutDownHandler, Haven e.printStackTrace(); } + injector.getInstance(TradeManager.class).shutDown(); injector.getInstance(PriceFeedService.class).shutDown(); injector.getInstance(ArbitratorManager.class).shutDown(); injector.getInstance(TradeStatisticsManager.class).shutDown(); @@ -362,11 +363,13 @@ public abstract class HavenoExecutable implements GracefulShutDownHandler, Haven injector.getInstance(P2PService.class).shutDown(() -> { log.info("Done shutting down OpenOfferManager, OfferBookService, and P2PService"); + // shut down connections pool + log.info("Shutting down connections"); + Connection.shutDownExecutor(30); + // shut down monero wallets and connections injector.getInstance(WalletsSetup.class).shutDownComplete.addListener((ov, o, n) -> { - log.info("Shutting down connections"); - Connection.shutDownExecutor(30); - + // done shutting down log.info("Graceful shutdown completed. Exiting now."); module.close(injector); diff --git a/core/src/main/java/haveno/core/app/misc/ExecutableForAppWithP2p.java b/core/src/main/java/haveno/core/app/misc/ExecutableForAppWithP2p.java index 0b8c25d5b7..44a1e46933 100644 --- a/core/src/main/java/haveno/core/app/misc/ExecutableForAppWithP2p.java +++ b/core/src/main/java/haveno/core/app/misc/ExecutableForAppWithP2p.java @@ -27,12 +27,15 @@ import haveno.common.persistence.PersistenceManager; import haveno.common.setup.GracefulShutDownHandler; import haveno.common.util.Profiler; import haveno.core.api.XmrConnectionService; +import haveno.core.app.AvoidStandbyModeService; import haveno.core.app.HavenoExecutable; import haveno.core.offer.OfferBookService; import haveno.core.offer.OpenOfferManager; +import haveno.core.provider.price.PriceFeedService; import haveno.core.support.dispute.arbitration.arbitrator.ArbitratorManager; import haveno.core.trade.HavenoUtils; import haveno.core.trade.TradeManager; +import haveno.core.trade.statistics.TradeStatisticsManager; import haveno.core.xmr.setup.WalletsSetup; import haveno.core.xmr.wallet.BtcWalletService; import haveno.core.xmr.wallet.XmrWalletService; @@ -107,7 +110,11 @@ public abstract class ExecutableForAppWithP2p extends HavenoExecutable { } JsonFileManager.shutDownAllInstances(); + injector.getInstance(TradeManager.class).shutDown(); + injector.getInstance(PriceFeedService.class).shutDown(); injector.getInstance(ArbitratorManager.class).shutDown(); + injector.getInstance(TradeStatisticsManager.class).shutDown(); + injector.getInstance(AvoidStandbyModeService.class).shutDown(); // shut down open offer manager log.info("Shutting down OpenOfferManager, OfferBookService, and P2PService"); @@ -120,12 +127,14 @@ public abstract class ExecutableForAppWithP2p extends HavenoExecutable { injector.getInstance(P2PService.class).shutDown(() -> { log.info("Done shutting down OpenOfferManager, OfferBookService, and P2PService"); + // shut down connections pool + log.info("Shutting down connections"); + Connection.shutDownExecutor(30); + // shut down monero wallets and connections injector.getInstance(WalletsSetup.class).shutDownComplete.addListener((ov, o, n) -> { module.close(injector); PersistenceManager.flushAllDataToDiskAtShutdown(() -> { - log.info("Shutting down connections"); - Connection.shutDownExecutor(30); // done shutting down log.info("Graceful shutdown completed. Exiting now."); diff --git a/core/src/main/java/haveno/core/xmr/wallet/XmrWalletService.java b/core/src/main/java/haveno/core/xmr/wallet/XmrWalletService.java index cd668449ad..6470908e38 100644 --- a/core/src/main/java/haveno/core/xmr/wallet/XmrWalletService.java +++ b/core/src/main/java/haveno/core/xmr/wallet/XmrWalletService.java @@ -664,9 +664,11 @@ public class XmrWalletService { // remove listeners which stops polling wallet // TODO monero-java: wallet.stopPolling()? - if (wallet != null) { - for (MoneroWalletListenerI listener : new HashSet<>(wallet.getListeners())) { - wallet.removeListener(listener); + synchronized (walletLock) { + if (wallet != null) { + for (MoneroWalletListenerI listener : new HashSet<>(wallet.getListeners())) { + wallet.removeListener(listener); + } } } } @@ -676,10 +678,7 @@ public class XmrWalletService { // shut down trade and main wallets at same time walletListeners.clear(); - List tasks = new ArrayList(); - if (tradeManager != null) tasks.add(() -> tradeManager.shutDown()); - tasks.add(() -> closeMainWallet(true)); - HavenoUtils.awaitTasks(tasks); + closeMainWallet(true); log.info("Done shutting down all wallets"); } @@ -984,13 +983,15 @@ public class XmrWalletService { } private void closeMainWallet(boolean save) { - try { - if (wallet != null) { - closeWallet(wallet, true); - wallet = null; + synchronized (walletLock) { + try { + if (wallet != null) { + closeWallet(wallet, true); + wallet = null; + } + } catch (Exception e) { + log.warn("Error closing main monero-wallet-rpc subprocess: " + e.getMessage() + ". Was Haveno stopped manually with ctrl+c?"); } - } catch (Exception e) { - log.warn("Error closing main monero-wallet-rpc subprocess: " + e.getMessage() + ". Was Haveno stopped manually with ctrl+c?"); } }