You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
The RedisLockRegistry unlock race condition on the interrupted threadΒ #11049
@Override
public final void unlock() {
if (!this.localLock.isHeldByCurrentThread()) {
throw new IllegalStateException("You do not own lock at " + this.lockKey);
}
if (this.localLock.getHoldCount() > 1) {
this.localLock.unlock();
return;
}
try {
if (Thread.currentThread().isInterrupted()) {
RedisLockRegistry.this.executor.execute(this::removeLockKey);
}
else {
removeLockKey();
}
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Released lock; " + this);
}
}
catch (Exception e) {
ReflectionUtils.rethrowRuntimeException(e);
}
finally {
this.localLock.unlock();
}
}
Where it is very visible that this.localLock.unlock(); might be called before Redis key is removed in case of interrupted thread.
We cannot get rid of thread shifting since Redis driver does not allow to perform network operations on the interrupted thread. And we also cannot simply rely on the CountDownLatch or Future since they throw InterruptedException immediately in the access to them.
However there is "gotcha" in Java on the matter.
We can do like this:
The JVM implicitly CLEARS the thread's interrupt flag when throwing an InterruptedException. We record it here with the interrupted flag, loop back to the latch.await() and restore Thread.currentThread().interrupt() state.
Activity
spring-projectsGH-11049: Block unlock until key is removed
GH-11049: Block unlock until key is removed
GH-11049: Block unlock until key is removed
GH-11049: Block unlock until key is removed