Skip to content

Commit

Permalink
DaoSnapshot: Fix persistingBlockInProgress data race
Browse files Browse the repository at this point in the history
The persistingBlockInProgress field is read by the block parsing thread.
However, the block parsing thread and user thread write to the
persistingBlockInProgress field. The block parsing thread might see the
update too late and trigger another snapshot before the previous was
done.
  • Loading branch information
alvasw committed Jan 9, 2025
1 parent f079e26 commit 3b5ccd2
Showing 1 changed file with 5 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -79,7 +80,7 @@ public class DaoStateSnapshotService implements DaoSetupService, DaoStateListene
@Nullable
private Runnable resyncDaoStateFromResourcesHandler;
private int daoRequiresRestartHandlerAttempts = 0;
private boolean persistingBlockInProgress;
private final AtomicBoolean persistingBlockInProgress = new AtomicBoolean();
private boolean isParseBlockChainComplete;
private final List<Integer> heightsOfLastAppliedSnapshots = new ArrayList<>();

Expand Down Expand Up @@ -233,7 +234,7 @@ public void maybeCreateSnapshot(Block block) {
// We protect to get called while we are not completed with persisting the daoState. This can take about
// 20 seconds, and it is not expected that we get triggered another snapshot event in that period, but this
// check guards that we would skip such calls.
if (persistingBlockInProgress) {
if (persistingBlockInProgress.get()) {
if (preferences.isUseFullModeDaoMonitor()) {
// In case we don't use isUseFullModeDaoMonitor we might get called here too often as the parsing is much
// faster than the persistence, and we likely create only 1 snapshot during initial parsing, so
Expand All @@ -256,7 +257,7 @@ public void maybeCreateSnapshot(Block block) {

private void persist() {
long ts = System.currentTimeMillis();
persistingBlockInProgress = true;
persistingBlockInProgress.set(true);
daoStateStorageService.requestPersistence(daoStateCandidate,
blocksCandidate,
hashChainCandidate,
Expand All @@ -265,7 +266,7 @@ private void persist() {
snapshotHeight, System.currentTimeMillis() - ts);

createSnapshot();
persistingBlockInProgress = false;
persistingBlockInProgress.set(false);
});
}

Expand Down

0 comments on commit 3b5ccd2

Please sign in to comment.