A lock is taken with lock() and the next statement is not a try that unlocks it in finally

tx0/kotlin:lock-without-finallyrecommendedwarningreliability/concurrencydepth fastbug, concurrency, cwe-667

What it finds

After lock.lock(), an exception before unlock() leaves the lock held forever. The pattern the Lock documentation gives is lock() followed at once by a try whose finally calls unlock(); withLock { } does the same in one call.

From the rule's source, lock_without_finally.rs.

Example from the rule's tests
class Counter {    private val lock = ReentrantLock()    private var count = 0     fun add() {        lock.lock()tx0 reports: lock.lock() is not followed by a try that unlocks in finally        count++        lock.unlock()    }     fun addSafely() {        lock.lock()        try {            count++        } finally {            lock.unlock()        }    }     fun take() {        lock.lock()    }     fun addWith() = lock.withLock { count++ }}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/kotlin:lock-without-finally" = "warning"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/kotlin:lock-without-finally] reason="..."

tx0 explain tx0/kotlin:lock-without-finally shows the same in the terminal. Found a false positive? Report it.