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:deadlock when reentrant exclusive lock #2905 #2879 #2961

Merged
merged 3 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
15 changes: 14 additions & 1 deletion src/main/java/io/lettuce/core/protocol/SharedLock.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class SharedLock {

private final Lock lock = new ReentrantLock();

private final ThreadLocal<Integer> sharedCnt = ThreadLocal.withInitial(() -> 0);
Roiocam marked this conversation as resolved.
Show resolved Hide resolved

private volatile long writers = 0;

private volatile Thread exclusiveLockOwner;
Expand All @@ -45,6 +47,7 @@ void incrementWriters() {

if (WRITERS.get(this) >= 0) {
WRITERS.incrementAndGet(this);
sharedCnt.set(sharedCnt.get() + 1);
return;
}
}
Expand All @@ -63,6 +66,7 @@ void decrementWriters() {
}

WRITERS.decrementAndGet(this);
sharedCnt.set(sharedCnt.get() - 1);
}

/**
Expand Down Expand Up @@ -125,6 +129,11 @@ private void lockWritersExclusive() {
exclusiveLockOwner = Thread.currentThread();
return;
}
// reentrant exclusive lock
if (WRITERS.compareAndSet(this, sharedCnt.get(), -1)) {
Roiocam marked this conversation as resolved.
Show resolved Hide resolved
exclusiveLockOwner = Thread.currentThread();
return;
}
}
} finally {
lock.unlock();
Expand All @@ -137,9 +146,13 @@ private void lockWritersExclusive() {
private void unlockWritersExclusive() {

if (exclusiveLockOwner == Thread.currentThread()) {
if (WRITERS.incrementAndGet(this) == 0) {
// check exclusive look not reentrant first
if (WRITERS.compareAndSet(this, -1, sharedCnt.get())) {
exclusiveLockOwner = null;
return;
}
// otherwise unlock until no more reentrant left
WRITERS.incrementAndGet(this);
}
}

Expand Down
57 changes: 57 additions & 0 deletions src/test/java/io/lettuce/core/protocol/SharedLockTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package io.lettuce.core.protocol;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

public class SharedLockTest {

@Test
public void safety_on_reentrant_lock_exclusive_on_writers() throws InterruptedException {
final SharedLock sharedLock = new SharedLock();
CountDownLatch cnt = new CountDownLatch(1);
try {
sharedLock.incrementWriters();

String result = sharedLock.doExclusive(() -> {
return sharedLock.doExclusive(() -> {
return "ok";
});
});
if ("ok".equals(result)) {
cnt.countDown();
}
} finally {
sharedLock.decrementWriters();
}

boolean await = cnt.await(1, TimeUnit.SECONDS);
Assertions.assertTrue(await);

// verify writers won't be negative after finally decrementWriters
String result = sharedLock.doExclusive(() -> {
return sharedLock.doExclusive(() -> {
return "ok";
});
});

Assertions.assertEquals("ok", result);

// and other writers should be passed after exclusive lock released
CountDownLatch cntOtherThread = new CountDownLatch(1);
new Thread(() -> {
try {
sharedLock.incrementWriters();
cntOtherThread.countDown();
} finally {
sharedLock.decrementWriters();
}
}).start();

await = cntOtherThread.await(1, TimeUnit.SECONDS);
Assertions.assertTrue(await);
}

}
Loading