'it seems that redisson distributed lock does not work

I'm testing whether distributed lock of redisson does work correctly, with kotlin and coroutine.

    runBlocking {
        repeat(1000) {
            launch {
                val lock = nonReactiveClient.getLock("lock")
                if(lock.tryLock(5, 5, TimeUnit.SECONDS)) {
                    try {
                        val value = test.get()
                        delay(10L)
                        test.set(value + 3)
                    } finally {
                        lock.unlock()
                    }
                }
            }
        }
    }

I think that the result value should be 3000, because the distributed lock guarantees that the 'get' and 'set' operation would be executed together, atomically.

But when I tried to get value, I got the following result :

127.0.0.1:6379> get test
"3"

What am I doing wrong ???



Solution 1:[1]

In Coroutines, after delay, thread context can be changed.

lock.tryLock - thread id - 77

lock.unlock - thread id - 88 (wait infinitely)

you can use forceUnlock() under diffent thread id

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1