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 txpool dump/restore race condition #6665

Merged
merged 3 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
- Update commons-compress to 1.26.0 [#6648](https://github.com/hyperledger/besu/pull/6648)

### Bug fixes
- Fix txpool dump/restore race condition [#6665](https://github.com/hyperledger/besu/pull/6665)

### Download Links

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,11 @@
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand Down Expand Up @@ -671,7 +670,7 @@ private void onAdded(final Transaction transaction) {
}

class SaveRestoreManager {
private final Lock diskAccessLock = new ReentrantLock();
private final Semaphore diskAccessLock = new Semaphore(1, true);
private final AtomicReference<CompletableFuture<Void>> writeInProgress =
new AtomicReference<>(CompletableFuture.completedFuture(null));
private final AtomicReference<CompletableFuture<Void>> readInProgress =
Expand All @@ -692,25 +691,20 @@ private CompletableFuture<Void> serializeAndDedupOperation(
final AtomicReference<CompletableFuture<Void>> operationInProgress) {
if (configuration.getEnableSaveRestore()) {
try {
if (diskAccessLock.tryLock(1, TimeUnit.MINUTES)) {
try {
if (!operationInProgress.get().isDone()) {
isCancelled.set(true);
try {
operationInProgress.get().get();
} catch (ExecutionException ee) {
// nothing to do
}
if (diskAccessLock.tryAcquire(1, TimeUnit.MINUTES)) {
if (!operationInProgress.get().isDone()) {
isCancelled.set(true);
try {
operationInProgress.get().get();
} catch (ExecutionException ee) {
// nothing to do
}

isCancelled.set(false);
operationInProgress.set(CompletableFuture.runAsync(operation));
return operationInProgress.get();
} catch (InterruptedException ie) {
isCancelled.set(false);
} finally {
diskAccessLock.unlock();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we not need the equivalent of releasing in finally

Copy link
Contributor Author

@fab-10 fab-10 Mar 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no and this was the error, since the operation is run async, we can't know here when it is done, and so it is not up to us to release the lock, but the lock must be released after the async operation is done with this code CompletableFuture.runAsync(operation).thenRun(diskAccessLock::release));

}

isCancelled.set(false);
operationInProgress.set(
CompletableFuture.runAsync(operation).thenRun(diskAccessLock::release));
return operationInProgress.get();
} else {
CompletableFuture.failedFuture(
new TimeoutException("Timeout waiting for disk access lock"));
Expand Down
Loading