Possible FindBugs errors: UL_UNRELEASED_LOCK_EXCEPTION_PATH?

I have a code like this:

public class Thingy { private final Lock lock = new ReentrantLock(); private boolean shutdown; public void shutdown() { lock.lock(); shutdown = true; lock.unlock(); } } 

And FindBugs complains that "Thingy.shutdown () does not release the lock of all exception paths" and that I have to wrap the string shutdown = true; in try-finally, but as far as I can see, there is no way this can go wrong.

Am I wrong or is it false positive?

+4
source share
2 answers

To be fair, it is usually quite complex (without being a full compiler and analyzing) to determine if any given piece of code can throw an exception or not.

But I agree, in this case, this is a false positive. And even if itโ€™s not - even if there is some dark corner of the JLS / JVM specification that says that the assignment can somehow be excluded - if you fall into this state, you will be more worried than an un -released lock!

+5
source

This is true, but also the fact that using locks is wrong here. You only need volatile .

In fact, you donโ€™t even need to, since you never read the value, so you can completely exclude the variable and the lock.

The point is that for a simple record, findbugs warn something, probably right, it just warns about the wrong thing.

+2
source

Source: https://habr.com/ru/post/1344960/


All Articles