>> Then, the error is something strange (eg confusing NullPointerException) which doesn't make it clear what's happened.
> I think that depends on context. Many cases of NPEs are perfectly clear when you look at the stack trace.
Well for people familiar with Lucene's sources, yes. But most of our
users are not and so seeing an "AlreadyClosedException" can make a big
difference over [possibly rather nested, and, rather delayed] NPE or
EG look at the poor user that led to my opening this issue (link in
opening comment). The user was understandably confused by the NPE
inside the RAMDirectory.
> Adding ensureOpen() to something like maxDoc() does not ensure
> correctness though - an exception may or may not be thrown in the
> reader is already closed (because of those thread-safety issues). It
> actually increases the variability of behavior. We need to be
> careful not to guarantee the throwing of the exception.
On the thread safety issue: are you saying if one thread closes the
reader while another thread is using it, there is uncertainty excactly
when the 2nd thread will hit the AlreadyClosedException (because of
how the JVM schedules the threads)? I think this kind of thread
behavior is normal/expected?
Or is the thread safety issue something else?
>> especially common seems to be iterating through Hits after the reader is closed
> Good point, for document() esp. I'm OK with the heavyweight methods.
>> Maybe we could remove checking for clearly frequently called methods (eg isDeleted)?
> That's one of the ones I had in mind... isDeleted() can be called millions of times for a single query. Probably numDoc(), maxDoc(), etc, are also candidates.
OK how about we do not call ensureOpen() in these IndexReader methods?:
I think even without checking in those methods we still catch a number
of common cases:
- Close reader then try to run a search (termDocs()/termPositions()
catch the close)
- Run a search, close reader, then try to iterate through Hits
(document() catches the close)
- Close a reader then try to delete docs or setNorms
(deleteDocument()/setNorm() catch the close)
> Earlier detection of bugs when the cost is nil is good though...
Yes I think we in general want "fail-fast" here.
> what about setting more things to null when a reader is closed?
Well ... I would prefer not to increase the frequency of getting
"undefined" NPEs out of the reader. If we are going to cause
additional exceptions here, I'd like to make them intelligible to the
user (ie, AlreadyClosedException). EG, take SegmentReader's "si"
(SegmentInfo). If we set this to null on close, then numDoc() and
maxDoc() would hit NPE instead of just returning the correct answer.
I think I'd prefer returning the correct answer for such cases.