A lock is released outside a finally block, so an exception leaves it held
What it finds
lock.lock(); work(); lock.unlock(); never releases the lock when work() throws: every thread that asks for it afterwards blocks forever. The release belongs in a finally block of a try that starts right after lock().
Reported is a lock() whose method releases the same lock, but nowhere in a finally block. A method that only acquires, leaving the release to another, is left alone.
From the rule's source, unlock_outside_finally.rs.
import java.util.concurrent.locks.Lock; class Account { private final Lock lock = null; private long balance; void deposit(long amount) { lock.lock();tx0 reports: lock is not released in a finally block balance += amount; lock.unlock(); } void withdraw(long amount) { lock.lock(); try { balance -= amount; } finally { lock.unlock(); } } void acquire() { lock.lock(); }} 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:
[rules]
"tx0/java:unlock-outside-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/java:unlock-outside-finally] reason="..."tx0 explain tx0/java:unlock-outside-finally shows the same in the terminal. Found a false positive? Report it.