...
This means that an unreleased lock in any thread will stop other threads from acquiring the same lock.
Noncompliant Code Example
This noncompliant code example protects a resource by using a ReentrantLock
but on an exceptional condition, fails to release the lock. Control flow transfers to the catch
block and the call to unlock()
does not execute.
...
Note that the lock is not released even when the doSomething()
method returns.
Compliant Solution
This compliant solution uses a try
-finally
block immediately after acquiring the lock. This ensures that the lock is appropriately released even in the event of an exceptional condition.
Code Block | ||
---|---|---|
| ||
public void doSomething() { final Lock lock = new ReentrantLock(); lock.lock(); try { // ... } finally { lock.unlock(); } // perform some time consuming operation } |
Risk Assessment
Failing to release a lock on an exceptional condition may lead to thread starvation and deadlock.
Rule | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
CON42-J | low | likely | low | P9 | L2 |
Automated Detection
TODO
Related Vulnerabilities
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
References
Wiki Markup |
---|
\[[API 06|AA. Java References#API 06]\] Class {{ReentrantLock}} |
...