apache / apache/lucene

ReferenceManager.release uses assertion to expect argument not null, also expects argument to be not null [LUCENE-6113]

Open
#7,175 4 comments 0 reactions 0 assignees View on GitHub
affects-version:4.10.1 legacy-jira-priority:Major type:bug
Dominant language
Java
Stars
3.6k
Forks
1.4k
Avg merge
2d 11h
Merged PRs (30d)
88

Description

A common use pattern for the Reference Manager looks like so:

```Java
IndexSearcher searcher = null;
try {
searcher = searcherManager.acquire();
// do real work
} finally {
searcherManager.release(searcher);
}
```

The problem with this code is if 'acquire' throws an exception, the finally block is called with a null reference for 'searcher'. There are two issues, one is this call release() uses assertion to check for argument validity, which is not recommended (http://docs.oracle.com/javase/8/docs/technotes/guides/language/assert.html) and secondly to fix this, we need to guard all calls to release with an if clause.

Why not have release() be a noop if it is passed null, instead of triggering an NPE? It would support this API usage pattern w/o any changes on the behalf of users.

Looking at the code, it appears that it is very unlikely that the acquire() call throws an exception.

---
Migrated from [LUCENE-6113](https://issues.apache.org/jira/browse/LUCENE-6113) by ryan rawson

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.