Skip to content

Commit

Permalink
Ensure nesting is only decremented when lock was acquired successfully
Browse files Browse the repository at this point in the history
When running Spock's tests with the latest snapshot, `pop()` seems to
have been called without a prior call to `push()` on the `Deque` of
resource locks.

(cherry picked from commit 73c8ca2)
  • Loading branch information
marcphilipp committed Sep 19, 2024
1 parent 8e0e13f commit e136b95
Showing 1 changed file with 12 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -253,17 +253,17 @@ public boolean exec() {
// this means that .join() will wait.
return false;
}
try (ResourceLock lock = resourceLock.acquire()) {
threadLock.incrementNesting(lock);
try ( //
ResourceLock lock = resourceLock.acquire(); //
@SuppressWarnings("unused")
ThreadLock.NestedResourceLock nested = threadLock.withNesting(lock) //
) {
testTask.execute();
return true;
}
catch (InterruptedException e) {
throw ExceptionUtils.throwAsUncheckedException(e);
}
finally {
threadLock.decrementNesting();
}
}

@Override
Expand Down Expand Up @@ -300,18 +300,19 @@ void addDeferredTask(ExclusiveTask task) {
deferredTasks.add(task);
}

void incrementNesting(ResourceLock lock) {
NestedResourceLock withNesting(ResourceLock lock) {
locks.push(lock);
}

@SuppressWarnings("resource")
void decrementNesting() {
locks.pop();
return locks::pop;
}

boolean areAllHeldLocksCompatibleWith(ResourceLock lock) {
return locks.stream().allMatch(l -> l.isCompatible(lock));
}

interface NestedResourceLock extends AutoCloseable {
@Override
void close();
}
}

interface TaskEventListener {
Expand Down

0 comments on commit e136b95

Please sign in to comment.