google / google/error-prone

GurardedBy bug pattern doesn't consider lock held if it is acquired via the Lock#tryLock methods

Open
#943 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
7.2k
Forks
820
Avg merge
5h 9m
Merged PRs (30d)
50

Description

The [GuardedBy](http://errorprone.info/bugpattern/GuardedBy) bug pattern doesn't seem to consider a lock held if it is acquired via the `Lock#tryLock` methods.

### What version of Error Prone are you using?
2.2.0

### Does this issue reproduce with the latest release?
I believe that 2.2.0 is the latest version.

### What did you do?
```java
public class MyClass {
private final Lock lock = new ReentrantLock(true);

@GuardedBy("lock")
private Set set = new HashSet();

public void method() throws InterruptedException {
if (lock.tryLock(5, TimeUnit.MINUTES)) {
set.add(currentTimeMillis());
}
}
}
```
### What did you expect to see?
When acquiring a reentrant lock via the `tryLock` method, as long as the method returns `true` the lock should be considered as acquired.

### What did you see instead?
```
Compilation failure
[ERROR] .../src/MyClass.java:[23,13] [GuardedBy] This access should be guarded by 'this.lock', which is not currently held
[ERROR] (see http://errorprone.info/bugpattern/GuardedBy)
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.