Skip to content

Commit

Permalink
fix potential concurrent modification exception in PendingPool (#7813)
Browse files Browse the repository at this point in the history
  • Loading branch information
tbenr committed Dec 11, 2023
1 parent 9b529f1 commit 24ba349
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ public synchronized void add(T item) {

final Bytes32 itemRoot = hashTreeRootFunction.apply(item);
final Collection<Bytes32> requiredRoots = requiredBlockRootsFunction.apply(item);
final ArrayList<Bytes32> newRequiredRoots = new ArrayList<>();

requiredRoots.forEach(
requiredRoot ->
Expand All @@ -106,12 +107,15 @@ public synchronized void add(T item) {
requiredRoot,
(key) -> {
final Set<Bytes32> dependants = new HashSet<>();
requiredBlockRootSubscribers.forEach(
c -> c.onRequiredBlockRoot(requiredRoot));
newRequiredRoots.add(requiredRoot);
return dependants;
})
.add(itemRoot));

newRequiredRoots.forEach(
requiredRoot ->
requiredBlockRootSubscribers.forEach(s -> s.onRequiredBlockRoot(requiredRoot)));

// Index item by root
if (pendingItems.putIfAbsent(itemRoot, item) == null) {
LOG.trace(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,25 @@ private void setSlot(final UInt64 slot) {
pendingPool.onSlot(slot);
}

@Test
public void add_shouldDeferSubscribersCallToAvoidConcurrentModificationException() {
final SignedBeaconBlock block =
dataStructureUtil.randomSignedBeaconBlock(currentSlot.longValue());

final SignedBeaconBlock block2 =
dataStructureUtil.randomSignedBeaconBlock(currentSlot.longValue());

pendingPool.subscribeRequiredBlockRoot(
blockRoot -> {
if (blockRoot.equals(block.getParentRoot())) {
pendingPool.add(block2);
}
});
pendingPool.add(block);

assertThat(pendingPool.contains(block2)).isTrue();
}

@Test
public void add_blockForCurrentSlot() {
final SignedBeaconBlock block =
Expand Down

0 comments on commit 24ba349

Please sign in to comment.