Search code examples
javafindbugsjava.util.concurrent

Probable false positive from Findbugs?


Please refer to the following code snippet(stripped off the redundant part to highlight the problematic case):

FindBugs is complaining that "Method does not release lock on all paths" . Is this a false positive? If not, how to fix this?

  try{
      someLock.lock();
     //do something
    } finally{
      if (someLock.isLocked())
        someLock.unlock();
    }

Solution

  • If isLocked() throws something, then you don't unlock.

    I don't think it's probable to have isLocked throwing an exception but when you lock, you must unlock, there is no point in testing. So why not use the standard pattern described in the javadoc :

    someLock.lock();
    try{
        //do something
    } finally{
        someLock.unlock();
    }
    

    So I'd said

    • it's a false positive in the sense your code can't fail to unlock
    • it's a true positive in the sense you should fix your code according to the recommendation