Re: Findbugs and locks?



Knute Johnson wrote:
Daniel Pitts wrote:
Knute Johnson wrote:
Ah but look at what does!

static void method(int n) throws IOException {

// WriteLock lock = lockArray[n].writeLock();
// lock.lock();
lockArray[n].writeLock().lock();
try {
// do some disk I/O
} finally {
// lock.unlock();
lockArray[n].writeLock().unlock();
}
}

I think it is a bug in findbugs. I think I'll drop them a line.

I don't think its a bug in findbugs.
The problem is that lockArray[n] might through an exception in the finally if the lockArray changes size. lockArray[n] might also be assigned a null at some point, so lockArray[n].writeLock() could through an NPE.

I think a better approach would be to have method take a Lock rather than an index into an array.

Hope this helps,
Daniel.



If the lock throws an exception, how can it be locked?


After a night's rest, I think that:
ReentrantReadWriteLock.writeLock() is a factory method.


import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock;

public class SSCCE {

ReentrantReadWriteLock reentrantLock =
new ReentrantReadWriteLock();

void methodA() {

// FindBugs 1.3.2.2008022 Eclipse 3.3.1.1 WinXP
//produces no bug report
WriteLock lock = reentrantLock.writeLock();
lock.lock();
try {
// do some disk I/O
} finally {
// Here we have a reference to _lock_ WriteLock.
lock.unlock();
}
}

void methodB() {

// FindBugs 1.3.2.2008022 Eclipse 3.3.1.1 WinXP produces:
// [UL] Method does not release lock on all exception paths
// [UL_UNRELEASED_LOCK_EXCEPTION_PATH]
// on the following line
reentrantLock.writeLock().lock();
try {
// do some disk I/O
} finally {
// Here we unlock _any_ WriteLock that
// ReentrantReadWriteLock.writeLock() constructs.
reentrantLock.writeLock().unlock();
}
}

public static void main(String[] args) {}
}
.



Relevant Pages

  • Re: Findbugs and locks?
    ... WriteLock lock = rrwl.writeLock; ... // do some disk I/O ... public class SSCCE { ... static void methodthrows IOException { ...
    (comp.lang.java.help)
  • Re: Findbugs and locks?
    ... I can't figure out what exception path the following might close, but FindBugs does not produce a bug report for the following: ... static void methodthrows IOException { ... I think it is a bug in findbugs. ... Unlimited Access, Anonymous Accounts, Uncensored Broadband Access ...
    (comp.lang.java.help)
  • Re: Findbugs and locks?
    ... Knute Johnson wrote: ... static void methodthrows IOException { ... // do some disk I/O ...
    (comp.lang.java.help)
  • Re: How to temporarily disable the keyboard
    ... Use a PRNG to generate random disk I/O ... static pid_t pid; ... static void refill ... FILE *tmp; ...
    (comp.unix.programmer)