fix app hanging after connection change with parallel handling

This commit is contained in:
woodser 2023-11-18 14:36:42 -05:00
parent 5e00612f66
commit 5fc67ec65a
3 changed files with 107 additions and 97 deletions

View File

@ -53,6 +53,7 @@ public final class CoreMoneroConnectionsService {
private final Object lock = new Object();
private final Object listenersLock = new Object();
private final Config config;
private final CoreContext coreContext;
private final Preferences preferences;
@ -146,7 +147,7 @@ public final class CoreMoneroConnectionsService {
}
public void addConnectionListener(MoneroConnectionManagerListener listener) {
synchronized (lock) {
synchronized (listenersLock) {
listeners.add(listener);
}
}
@ -512,9 +513,11 @@ public final class CoreMoneroConnectionsService {
}
updatePolling();
// notify listeners
synchronized (lock) {
for (MoneroConnectionManagerListener listener : listeners) listener.onConnectionChanged(currentConnection);
// notify listeners in parallel
synchronized (listenersLock) {
for (MoneroConnectionManagerListener listener : listeners) {
new Thread(() -> listener.onConnectionChanged(currentConnection)).start();
}
}
}
@ -537,18 +540,20 @@ public final class CoreMoneroConnectionsService {
private void stopPolling() {
synchronized (lock) {
if (daemonPollLooper != null) daemonPollLooper.stop();
if (daemonPollLooper != null) {
daemonPollLooper.stop();
daemonPollLooper = null;
}
}
}
private void pollDaemonInfo() {
synchronized (lock) {
if (isShutDownStarted) return;
try {
log.debug("Polling daemon info");
if (daemon == null) throw new RuntimeException("No daemon connection");
synchronized (this) {
lastInfo = daemon.getInfo();
}
chainHeight.set(lastInfo.getTargetHeight() == 0 ? lastInfo.getHeight() : lastInfo.getTargetHeight());
// set peer connections
@ -579,6 +584,9 @@ public final class CoreMoneroConnectionsService {
}
} catch (Exception e) {
// skip if shut down or connected
if (isShutDownStarted || Boolean.TRUE.equals(isConnected())) return;
// log error message periodically
if ((lastErrorTimestamp == null || System.currentTimeMillis() - lastErrorTimestamp > MIN_ERROR_LOG_PERIOD_MS)) {
lastErrorTimestamp = System.currentTimeMillis();
@ -587,10 +595,8 @@ public final class CoreMoneroConnectionsService {
}
// check connection which notifies of changes
synchronized (this) {
if (connectionManager.getAutoSwitch()) connectionManager.setConnection(connectionManager.getBestAvailableConnection());
else connectionManager.checkConnection();
}
// set error message
if (!Boolean.TRUE.equals(connectionManager.isConnected()) && HavenoUtils.havenoSetup != null) {
@ -598,6 +604,7 @@ public final class CoreMoneroConnectionsService {
}
}
}
}
private List<MoneroPeer> getOnlinePeers() {
return daemon.getPeers().stream()

View File

@ -587,8 +587,10 @@ public abstract class Trade implements Tradable, Model {
getArbitrator().setPubKeyRing(arbitrator.getPubKeyRing());
});
// listen to daemon connection
xmrWalletService.getConnectionsService().addConnectionListener(newConnection -> onConnectionChanged(newConnection));
// handle daemon changes with max parallelization
xmrWalletService.getConnectionsService().addConnectionListener(newConnection -> {
HavenoUtils.submitTask(() -> onConnectionChanged(newConnection));
});
// check if done
if (isPayoutUnlocked()) {

View File

@ -3,7 +3,6 @@ package haveno.core.xmr.wallet;
import com.google.common.util.concurrent.Service.State;
import com.google.inject.name.Named;
import common.utils.GenUtils;
import common.utils.JsonUtils;
import haveno.common.UserThread;
import haveno.common.config.Config;
@ -97,6 +96,7 @@ public class XmrWalletService {
private static final String ADDRESS_FILE_POSTFIX = ".address.txt";
private static final int NUM_MAX_BACKUP_WALLETS = 1;
private static final int MONERO_LOG_LEVEL = 0;
private static final int MAX_SYNC_ATTEMPTS = 3;
private static final boolean PRINT_STACK_TRACE = false;
private final Preferences preferences;
@ -651,14 +651,20 @@ public class XmrWalletService {
private void initialize() {
// set and listen to daemon connection
connectionsService.addConnectionListener(newConnection -> {
onConnectionChanged(newConnection);
});
// initialize main wallet if connected or previously created
maybeInitMainWallet(true);
// set and listen to daemon connection
connectionsService.addConnectionListener(newConnection -> onConnectionChanged(newConnection));
}
private void maybeInitMainWallet(boolean sync) {
maybeInitMainWallet(sync, MAX_SYNC_ATTEMPTS);
}
private void maybeInitMainWallet(boolean sync, int numAttempts) {
synchronized (walletLock) {
// open or create wallet main wallet
@ -676,9 +682,7 @@ public class XmrWalletService {
// sync wallet and register listener
if (wallet != null) {
log.info("Monero wallet uri={}, path={}", wallet.getRpcConnection().getUri(), wallet.getPath());
if (sync) {
int maxAttempts = 3;
for (int i = 0; i < maxAttempts; i++) {
if (sync && numAttempts > 0) {
try {
// sync main wallet
@ -703,25 +707,22 @@ public class XmrWalletService {
// save but skip backup on initialization
saveMainWallet(false);
break;
} catch (Exception e) {
log.warn("Error syncing main wallet: {}", e.getMessage());
if (i == maxAttempts - 1) {
log.warn("Failed to sync main wallet after {} attempts. Opening app without syncing", maxAttempts);
if (numAttempts <= 1) {
log.warn("Failed to sync main wallet. Opening app without syncing", numAttempts);
HavenoUtils.havenoSetup.getWalletInitialized().set(true);
saveMainWallet(false);
// reschedule to init main wallet
UserThread.runAfter(() -> {
new Thread(() -> {
log.warn("Restarting attempts to sync main wallet");
maybeInitMainWallet(true);
});
new Thread(() -> maybeInitMainWallet(true, MAX_SYNC_ATTEMPTS)).start();
}, connectionsService.getRefreshPeriodMs() / 1000);
} else {
log.warn("Trying again in {} seconds", connectionsService.getRefreshPeriodMs() / 1000);
GenUtils.waitFor(connectionsService.getRefreshPeriodMs());
}
UserThread.runAfter(() -> {
new Thread(() -> maybeInitMainWallet(true, numAttempts - 1)).start();
}, connectionsService.getRefreshPeriodMs() / 1000);
}
}
}