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.
  • Loading branch information
marcphilipp committed Sep 17, 2024
1 parent 88f8859 commit ccfbdf8
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);
//noinspection unused
try ( //
ResourceLock lock = resourceLock.acquire(); //
ThreadLock.NestedResourceLock nested = threadLock.withNesting(resourceLock) //
) {
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 ccfbdf8

Please sign in to comment.