0

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 ???

geunyoung
  • 23
  • 4
  • I don't know reddison, but given you have a try....finally without a catch, is there a danger that something in the try block throws and exception and you never call get? – matt freake Jun 09 '20 at 12:47
  • You defined lease time = 5sec, and delay 10sec. So lock would be released automatically by the end of delay. – Nikita Koksharov Jun 19 '20 at 15:28

1 Answers1

0

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

Debop
  • 1,035
  • 10
  • 9