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

Two different threads have reported acquiring a lock with the same key at the same time #5563

Closed
OuDale opened this issue Jan 14, 2024 · 6 comments
Labels

Comments

@OuDale
Copy link

OuDale commented Jan 14, 2024

Redisson version :

<dependency>
    <groupId>org.redisson</groupId>
    <artifactId>redisson-spring-boot-starter</artifactId>
    <version>3.17.4</version>
</dependency>

Here is my code:

private void generate() {
        RLock lock = redisson.getLock(key);
        try {
            String threadName = Thread.currentThread().getName();
            boolean locked = lock.tryLock();
            if (locked && shouldGenerateTask() {
                log.info("locked by key:{} with thread:{}", key, threadName);
                saveTask();
            }
        } catch (Exception e) {
            Thread.currentThread().interrupt();
        } finally {
            if (lock.isHeldByCurrentThread()) {
                lock.unlock();
            }
        }
    }

2024-01-12T12:00:07.514288425Z 2024-01-12 12:00:07.514 INFO 7 --- [ XNIO-1 task-4] .s.p.a.t.c.h.i.test : locked by key:App:Procurement:PENDING_PRICING:1400_65_2 with thread:XNIO-1 task-4

2024-01-12T12:00:07.542088285Z 2024-01-12 12:00:07.541 INFO 7 --- [ XNIO-1 task-2] .s.p.a.t.c.h.i.test : locked by key:App:Procurement:PENDING_PRICING:1400_65_2 with thread:XNIO-1 task-2

@mrniko
Copy link
Member

mrniko commented Jan 15, 2024

Please share the code to reproduce the issue.

@mrniko
Copy link
Member

mrniko commented Jan 15, 2024

Make sure that the lock wasn't unlocked by the time when the second thread acquired it.

@OuDale
Copy link
Author

OuDale commented Jan 15, 2024

Make sure that the lock wasn't unlocked by the time when the second thread acquired it.

Thanks reply,

  1. the log shows that the two threads acquired the lock in a very short period of time. I don't think it is the case of releasing and acquiring the lock again.
  2. I didn't assigned a leaseTime and unlocked in finally body.
private void generate() {
        RLock lock = redisson.getLock(key);
        try {
            String threadName = Thread.currentThread().getName();
            boolean locked = lock.tryLock();
            if (locked && shouldGenerateTask() {
                log.info("locked by key:{} with thread:{}", key, threadName);
                saveTask();
            }
        } catch (Exception e) {
            Thread.currentThread().interrupt();
        } finally {
            if (lock.isHeldByCurrentThread()) {
                lock.unlock();
            }
        }
    }
2024-01-12T12:00:07.514288425Z 2024-01-12 12:00:07.514 INFO 7 --- [ XNIO-1 task-4] .s.p.a.t.c.h.i.test : locked by key:App:Procurement:PENDING_PRICING:1400_65_2 with thread:XNIO-1 task-4

2024-01-12T12:00:07.542088285Z 2024-01-12 12:00:07.541 INFO 7 --- [ XNIO-1 task-2] .s.p.a.t.c.h.i.test : locked by key:App:Procurement:PENDING_PRICING:1400_65_2 with thread:XNIO-1 task-2

@OuDale
Copy link
Author

OuDale commented Jan 15, 2024

This is occurring in a concurrent scenario where the real situation involves listening to a database change data capture (CDC). Then, two threads simultaneously acquire the lock and save two separate task data.

@mrniko
Copy link
Member

mrniko commented Jan 15, 2024

Can you add log.info("unlocked1 by key:{} with thread:{}", key, threadName); after if (lock.isHeldByCurrentThread()) {

add log.info("unlocked2 by key:{} with thread:{}", key, threadName); after lock.unlock();

@OuDale
Copy link
Author

OuDale commented Jan 16, 2024

thanks for your help

@mrniko mrniko closed this as completed Jan 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

2 participants