Re: Findbugs and locks?



Knute Johnson wrote:
Jeff Higgins wrote:
Knute Johnson wrote:
Findbugs gives the warning "Method does not release lock on all exception paths" on a method like the one below. Could it be because the lock is from an array of locks and it can't determine which? Or is it because you could put code outside of the try/finally block that could leave without unlocking the lock? Any other ideas? It can't leave the method without unlocking can it?


Produces no bug reports using FindBugs 1.3.2.20080222
in Eclipse 3.3

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

public class SSCCE {

ReentrantReadWriteLock lockArray[] =
new ReentrantReadWriteLock[5];

void method(int n) throws IOException {

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

public static void main(String[] args) {

}

}

Putting the lock() call inside the try/finally block does stop findbugs from complaining. I put it on the outside because that is the way that Goetz showed in his book, Java Concurrency In Practice. He does mention that you must consider what happens if an exception is thrown outside of the try block. I suppose findbugs complains about my code is it is possible to throw an exception between the lock and the try even though I have no code there.

I don't know for sure what the ramifications are of putting the lock inside the try block but I can't think of any at the moment.


Sorry for the confusion, I too quickly posted an experimental version.
I can't figure out what exception path the following might close, but FindBugs does not produce a bug report for the following:

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

public class SSCCE {

static ReentrantReadWriteLock lockArray[];
static {
lockArray =
new ReentrantReadWriteLock[5];
for (int i=0; i<lockArray.length; i++)
lockArray[i] = new ReentrantReadWriteLock();
}

static void method(int n) throws IOException {

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

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

.



Relevant Pages

  • Re: [RFC] tcp: race in receive part
    ... The customer has been able to reproduce this problem only on one CPU model: ... Memory operations issued after the LOCK will be completed after the LOCK ... static void sock_def_readable(struct sock *sk, ...
    (Linux-Kernel)
  • Re: RT Mutex patch and tester [PREEMPT_RT]
    ... I hope you didn't remove my comment in the rt.c about BKL ... buffer = task_show_regs; #endif ... To keep from having a single lock for PI, ... } -static inline void +static void account_mutex_owner_up{if { ...
    (Linux-Kernel)
  • RT Mutex patch and tester [PREEMPT_RT]
    ... When it is unblocked it has the lock. ... owner, which in turn can boost and unblock the next in the lock chain... ... buffer = task_show_regs; #endif ... } -static inline void +static void account_mutex_owner_up{if { ...
    (Linux-Kernel)
  • queued spinlock code and results
    ... I made some tests of the queued spinlock code using userspace test code on ... max number of times in a row that a lock is acquired, ... static void unlock ... static int xlock_is_locked ...
    (Linux-Kernel)
  • [patch 1/8] uml: fix sigio spinlock [for 2.6.12]
    ... I just saw a "take twice spinlock" deadlock with the Spinlock debugging ... Added some comments to mark where this function needs the lock, ... static void tty_close ... send the line "unsubscribe linux-kernel" in ...
    (Linux-Kernel)