mirror of
https://github.com/boldsuck/haveno.git
synced 2025-01-05 15:49:23 +00:00
shut down XmrWalletService with timeout
This commit is contained in:
parent
e629a8c63a
commit
2ba37d98fe
1 changed files with 38 additions and 9 deletions
|
@ -128,6 +128,7 @@ public class XmrWalletService {
|
||||||
private static final int MAX_SYNC_ATTEMPTS = 3;
|
private static final int MAX_SYNC_ATTEMPTS = 3;
|
||||||
private static final boolean PRINT_STACK_TRACE = false;
|
private static final boolean PRINT_STACK_TRACE = false;
|
||||||
private static final String THREAD_ID = XmrWalletService.class.getSimpleName();
|
private static final String THREAD_ID = XmrWalletService.class.getSimpleName();
|
||||||
|
private static final long SHUTDOWN_TIMEOUT_MS = 60000;
|
||||||
|
|
||||||
private final User user;
|
private final User user;
|
||||||
private final Preferences preferences;
|
private final Preferences preferences;
|
||||||
|
@ -822,20 +823,48 @@ public class XmrWalletService {
|
||||||
public void shutDown() {
|
public void shutDown() {
|
||||||
log.info("Shutting down {}", getClass().getSimpleName());
|
log.info("Shutting down {}", getClass().getSimpleName());
|
||||||
|
|
||||||
// remove listeners which stops polling wallet
|
// create task to shut down
|
||||||
// TODO monero-java: wallet.stopPolling()?
|
Runnable shutDownTask = () -> {
|
||||||
synchronized (walletLock) {
|
|
||||||
|
// remove listeners
|
||||||
|
synchronized (walletLock) {
|
||||||
|
if (wallet != null) {
|
||||||
|
for (MoneroWalletListenerI listener : new HashSet<>(wallet.getListeners())) {
|
||||||
|
wallet.removeListener(listener);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
walletListeners.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
// shut down threads
|
||||||
|
synchronized (this) {
|
||||||
|
List<Runnable> shutDownThreads = new ArrayList<>();
|
||||||
|
shutDownThreads.add(() -> ThreadUtils.shutDown(THREAD_ID));
|
||||||
|
ThreadUtils.awaitTasks(shutDownThreads);
|
||||||
|
}
|
||||||
|
|
||||||
|
// shut down main wallet
|
||||||
if (wallet != null) {
|
if (wallet != null) {
|
||||||
for (MoneroWalletListenerI listener : new HashSet<>(wallet.getListeners())) {
|
try {
|
||||||
wallet.removeListener(listener);
|
closeMainWallet(true);
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.warn("Error closing main wallet: {}. Was Haveno stopped manually with ctrl+c?", e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// shut down with timeout
|
||||||
|
try {
|
||||||
|
ThreadUtils.awaitTask(shutDownTask, SHUTDOWN_TIMEOUT_MS);
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.warn("Error shutting down {}: {}", getClass().getSimpleName(), e.getMessage());
|
||||||
|
e.printStackTrace();
|
||||||
|
|
||||||
|
// force close wallet
|
||||||
|
forceCloseWallet(wallet, getWalletPath(MONERO_WALLET_NAME));
|
||||||
}
|
}
|
||||||
|
|
||||||
// shut down main wallet
|
log.info("Done shutting down {}", getClass().getSimpleName());
|
||||||
walletListeners.clear();
|
|
||||||
closeMainWallet(true);
|
|
||||||
log.info("Done shutting down main wallet");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ------------------------------ PRIVATE HELPERS -------------------------
|
// ------------------------------ PRIVATE HELPERS -------------------------
|
||||||
|
|
Loading…
Reference in a new issue