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 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
- Transaction call object to accept both `input` and `data` field simultaneously if they are set to equal values [#6702](https://github.com/hyperledger/besu/pull/6702)

### Bug fixes
- Fix txpool dump/restore race condition [#6665](https://github.com/hyperledger/besu/pull/6665)
- Make block transaction selection max time aware of PoA transitions [#6676](https://github.com/hyperledger/besu/pull/6676)
- Don't enable the BFT mining coordinator when running sub commands such as `blocks export` [#6675](https://github.com/hyperledger/besu/pull/6675)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,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 @@ -664,7 +663,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 @@ -685,25 +684,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