You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently, GuardedByChecker reports false positives if a lock is being held in a try-with-resources block. There's a TODO in the codebase for this, but I didn't see any tickets for this issue so I thought I'd start a discussion.
// TODO(cushon) - recognize common try-with-resources patterns. Currently there is no
// standard implementation of an AutoCloseable lock resource to detect.
When JDK 7 introduced the try-with-resources block, there was a lot of discussion about whether various locks should be supported. Eventually, it was decided that it would not be supported in the standard Java class library; however, the choice was controversial enough that I'm sure many people are using one of the many third party libraries to solve this or rolling their own Autoclosable solution. Would it be possible to add a heuristic to detect these AutoCloseable wrappers? Maybe by allowing users to explicitly annotate them?
Maybe something that looks like this?
classExample {
privatefinalLocklock = newLock();
@GuardedBy("lock")
privateintcounter;
voidtest() {
try (varu = LockUtil.lock(lock)) {
++counter;
}
}
}
classLockUtil {
@MustBeClosed@LockHolder("delegate") // Informs HeldLockAnalyzer to add 'delegate' to heldset. Maybe even verify unlock is called by close?publicstaticAutoCloseablelock(Lockdelegate) {
delegate.lock();
returndelegate::unlock;
}
}
The text was updated successfully, but these errors were encountered:
Currently,
GuardedByChecker
reports false positives if a lock is being held in a try-with-resources block. There's a TODO in the codebase for this, but I didn't see any tickets for this issue so I thought I'd start a discussion.error-prone/core/src/main/java/com/google/errorprone/bugpatterns/threadsafety/HeldLockAnalyzer.java
Lines 205 to 206 in 7acf133
When JDK 7 introduced the try-with-resources block, there was a lot of discussion about whether various locks should be supported. Eventually, it was decided that it would not be supported in the standard Java class library; however, the choice was controversial enough that I'm sure many people are using one of the many third party libraries to solve this or rolling their own Autoclosable solution. Would it be possible to add a heuristic to detect these AutoCloseable wrappers? Maybe by allowing users to explicitly annotate them?
Maybe something that looks like this?
The text was updated successfully, but these errors were encountered: