Skip to content

Commit

Permalink
DaoSnapshot: Fix resyncDaoStateFromResources race condition
Browse files Browse the repository at this point in the history
  • Loading branch information
alvasw committed Jan 9, 2025
1 parent 15f6b8e commit 4ec4477
Showing 1 changed file with 12 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;

import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -79,7 +80,7 @@ public class DaoStateSnapshotService implements DaoSetupService, DaoStateListene
@Setter
@Nullable
private Runnable resyncDaoStateFromResourcesHandler;
private int daoRequiresRestartHandlerAttempts = 0;
private final AtomicInteger daoRequiresRestartHandlerAttempts = new AtomicInteger();
private final AtomicBoolean persistingBlockInProgress = new AtomicBoolean();
private final AtomicBoolean isParseBlockChainComplete = new AtomicBoolean();
private final List<Integer> heightsOfLastAppliedSnapshots = new ArrayList<>();
Expand Down Expand Up @@ -369,7 +370,7 @@ private boolean isHeightBelowGenesisHeight(int height) {
private void resyncDaoStateFromResources() {
log.info("resyncDaoStateFromResources called");
if (resyncDaoStateFromResourcesHandler == null) {
if (++daoRequiresRestartHandlerAttempts <= 3) {
if (daoRequiresRestartHandlerAttempts.addAndGet(1) <= 3) {
log.warn("resyncDaoStateFromResourcesHandler has not been initialized yet, will try again in 10 seconds");
UserThread.runAfter(this::resyncDaoStateFromResources, 10); // a delay for the app to init
return;
Expand All @@ -378,12 +379,15 @@ private void resyncDaoStateFromResources() {
System.exit(1);
}
}
try {
daoStateStorageService.removeAndBackupAllDaoData();
// the restart handler informs the user of the need to restart bisq (in desktop mode)
resyncDaoStateFromResourcesHandler.run();
} catch (IOException e) {
log.error("Error at resyncDaoStateFromResources: {}", e.toString());

synchronized (this) {
try {
daoStateStorageService.removeAndBackupAllDaoData();
// the restart handler informs the user of the need to restart bisq (in desktop mode)
resyncDaoStateFromResourcesHandler.run();
} catch (IOException e) {
log.error("Error at resyncDaoStateFromResources: {}", e.toString());
}
}
}

Expand Down

0 comments on commit 4ec4477

Please sign in to comment.