Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix concurrent access of node permissioning allow list #7920

Merged
merged 2 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.function.Consumer;
import java.util.stream.Collectors;

Expand All @@ -48,7 +49,7 @@ public class NodeLocalConfigPermissioningController implements NodeConnectionPer
private LocalPermissioningConfiguration configuration;
private final List<EnodeURL> fixedNodes;
private final Bytes localNodeId;
private final List<EnodeURL> nodesAllowlist = new ArrayList<>();
private final List<EnodeURL> nodesAllowlist = new CopyOnWriteArrayList<>();
private final AllowlistPersistor allowlistPersistor;
private final Subscribers<Consumer<NodeAllowlistUpdatedEvent>> nodeAllowlistUpdatedObservers =
Subscribers.create();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Consumer;

import com.google.common.collect.Lists;
Expand Down Expand Up @@ -316,6 +317,56 @@ public void whenCheckingIfNodeIsPermittedOrderDoesNotMatter() {
.isTrue();
}

@Test
public void whenCallingIsPermittedAndRemovingEntryInAnotherThreadShouldNotThrowException()
throws InterruptedException {
// Add a node to the allowlist
controller.addNodes(Lists.newArrayList(enode1));

// Atomic flag to detect exceptions
AtomicBoolean exceptionOccurred = new AtomicBoolean(false);

// Create a thread to call isPermitted
Thread isPermittedThread =
new Thread(
() -> {
try {
for (int i = 0; i < 1000; i++) {
controller.isPermitted(enode1);
}
} catch (Exception e) {
exceptionOccurred.set(true);
e.printStackTrace();
}
});

// Create a thread to modify the allowlist
Thread modifyAllowlistThread =
new Thread(
() -> {
try {
for (int i = 0; i < 1000; i++) {
controller.removeNodes(Lists.newArrayList(enode1));
controller.addNodes(Lists.newArrayList(enode1));
}
} catch (Exception e) {
exceptionOccurred.set(true);
e.printStackTrace();
}
});

// Start both threads
isPermittedThread.start();
modifyAllowlistThread.start();

// Wait for both threads to complete
isPermittedThread.join();
modifyAllowlistThread.join();

// Assert no exceptions were thrown
assert (!exceptionOccurred.get());
}

@Test
public void stateShouldRevertIfAllowlistPersistFails()
throws IOException, AllowlistFileSyncException {
Expand Down
Loading