[Solved] Redisson distributed lock error: attempt to unlock lock, not locked by current thread by node id

java.lang.IllegalMonitorStateException: attempt to unlock lock, not locked by current thread by node id: 193fa0a4-1df0-445f-8737-18b7bb464f64 thread-id: 80

When the Redission distributed lock performs the unlock operation, there is an abnormal source code as follows:

public void unlock() {
        try {
            this.get(this.unlockAsync(Thread.currentThread().getId()));
        } catch (RedisException var2) {
            if (var2.getCause() instanceof IllegalMonitorStateException) {
                throw (IllegalMonitorStateException)var2.getCause();
            } else {
                throw var2;
            }
        }
    }
public RFuture<Void> unlockAsync(long threadId) {
        RFuture<Boolean> future = this.unlockInnerAsync(threadId);
        CompletionStage<Void> f = future.handle((opStatus, e) -> {
            this.cancelExpirationRenewal(threadId);
            if (e != null) {
                throw new CompletionException(e);
            } else if (opStatus == null) {
                IllegalMonitorStateException cause = new IllegalMonitorStateException("attempt to unlock lock, not locked by current thread by node id: " + this.id + " thread-id: " + threadId);
                throw new CompletionException(cause);
            } else {
                return null;
            }
        });
        return new CompletableFutureWrapper(f);
    }

Why is this happening, because when the lock operation is performed, a time is set

1. After you complete the lock, when the execution time of the business code in it is greater than the lock time, and unlock it, the exception will be thrown

2. The problem of multi-thread competition. When the first thread completes the lock, it is not unlocked at this time. In this way, the second thread tries to acquire the lock and perform the lock operation, and this exception will be thrown.

Solution:

Before locking or unlocking, it is enough to judge the legality of the state, instead of directly performing the locking and unlocking operation.

Read More:

Leave a Reply

Your email address will not be published. Required fields are marked *