From eb323f6d6596cf6880893a744c7d65b5288f7b13 Mon Sep 17 00:00:00 2001 From: woodser Date: Thu, 25 Jan 2024 11:57:26 -0500 Subject: [PATCH] force restart trade wallet on connection refused during poll --- .../main/java/haveno/core/trade/Trade.java | 39 ++++++++++++++----- .../core/xmr/wallet/XmrWalletService.java | 2 +- 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/core/src/main/java/haveno/core/trade/Trade.java b/core/src/main/java/haveno/core/trade/Trade.java index 4d86d1db..17cc760e 100644 --- a/core/src/main/java/haveno/core/trade/Trade.java +++ b/core/src/main/java/haveno/core/trade/Trade.java @@ -125,6 +125,7 @@ public abstract class Trade implements Tradable, Model { private MoneroWallet wallet; boolean wasWalletSynced; boolean pollInProgress; + boolean restartInProgress; /////////////////////////////////////////////////////////////////////////////////////////// // Enums @@ -897,6 +898,14 @@ public abstract class Trade implements Tradable, Model { wallet = null; } } + + public void forceStopWallet() { + if (wallet != null) { + log.warn("Force stopping wallet for {} {}", getClass().getSimpleName(), getId()); + xmrWalletService.stopWallet(wallet, wallet.getPath(), true); + wallet = null; + } + } public void deleteWallet() { synchronized (walletLock) { @@ -1321,11 +1330,7 @@ public abstract class Trade implements Tradable, Model { e.printStackTrace(); // force stop wallet - if (wallet != null) { - log.warn("Force stopping wallet for {} {}", getClass().getSimpleName(), getId()); - xmrWalletService.stopWallet(wallet, wallet.getPath(), true); - wallet = null; - } + forceStopWallet(); } // de-initialize @@ -2151,11 +2156,15 @@ public abstract class Trade implements Tradable, Model { } } } catch (Exception e) { - boolean isWalletConnected = isWalletConnectedToDaemon(); - if (!isWalletConnected) xmrConnectionService.checkConnection(); // check connection if wallet is not connected - if (!isShutDownStarted && wallet != null && isWalletConnected) { - log.warn("Error polling trade wallet for {} {}: {}. Monerod={}", getClass().getSimpleName(), getId(), e.getMessage(), getXmrWalletService().getConnectionService().getConnection()); - //e.printStackTrace(); + boolean isConnectionRefused = e.getMessage() != null && e.getMessage().contains("Connection refused"); + if (isConnectionRefused) forceRestartTradeWallet(); + else { + boolean isWalletConnected = isWalletConnectedToDaemon(); + if (!isWalletConnected) xmrConnectionService.checkConnection(); // check connection if wallet is not connected + if (!isShutDownStarted && wallet != null && isWalletConnected) { + log.warn("Error polling trade wallet for {} {}: {}. Monerod={}", getClass().getSimpleName(), getId(), e.getMessage(), getXmrWalletService().getConnectionService().getConnection()); + //e.printStackTrace(); + } } } finally { pollInProgress = false; @@ -2163,6 +2172,16 @@ public abstract class Trade implements Tradable, Model { } } + private void forceRestartTradeWallet() { + log.warn("Force restarting trade wallet for {} {}", getClass().getSimpleName(), getId()); + if (isShutDownStarted || restartInProgress) return; + restartInProgress = true; + forceStopWallet(); + if (!isShutDownStarted) wallet = getWallet(); + restartInProgress = false; + if (!isShutDownStarted) ThreadUtils.execute(() -> tryInitSyncing(), getId()); + } + private long getWalletRefreshPeriod() { if (isIdling()) return IDLE_SYNC_PERIOD_MS; return xmrConnectionService.getRefreshPeriodMs(); 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 323e609a..9111aad0 100644 --- a/core/src/main/java/haveno/core/xmr/wallet/XmrWalletService.java +++ b/core/src/main/java/haveno/core/xmr/wallet/XmrWalletService.java @@ -754,7 +754,7 @@ public class XmrWalletService { } } - // shut down trade and main wallets at same time + // shut down main wallet walletListeners.clear(); closeMainWallet(true); log.info("Done shutting down main wallet");