Skip to content

Commit

Permalink
Fix remaining data race in BurningManAccountingStore
Browse files Browse the repository at this point in the history
Add missing synchronisation to the 'toProtoMessage' method, by first
copying the internal list of blocks inside a read-lock, prior to
serialisation (still outside the lock, to maximise concurrency). Since
we only make a shallow copy, this should be fast and take no more than a
MB or so of extra memory.

This prevents a race seen to cause a ConcurrentModificationException
during store persistence, that sometimes occurred when the application
resumed from a long suspension.
  • Loading branch information
stejbac committed Feb 24, 2024
1 parent 2b4fb78 commit 02ee5bc
Showing 1 changed file with 9 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

package bisq.core.dao.burningman.accounting.storage;


import bisq.core.dao.burningman.accounting.blockchain.AccountingBlock;
import bisq.core.dao.burningman.accounting.exceptions.BlockHashNotConnectingException;
import bisq.core.dao.burningman.accounting.exceptions.BlockHeightNotConnectingException;
Expand Down Expand Up @@ -160,9 +159,17 @@ private void purgeLast10Blocks() {
}

public Message toProtoMessage() {
List<AccountingBlock> blocksCopy;
Lock readLock = readWriteLock.readLock();
readLock.lock();
try {
blocksCopy = new ArrayList<>(blocks);
} finally {
readLock.unlock();
}
return protobuf.PersistableEnvelope.newBuilder()
.setBurningManAccountingStore(protobuf.BurningManAccountingStore.newBuilder()
.addAllBlocks(blocks.stream()
.addAllBlocks(blocksCopy.stream()
.map(AccountingBlock::toProtoMessage)
.collect(Collectors.toList())))
.build();
Expand Down

0 comments on commit 02ee5bc

Please sign in to comment.