lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Trejkaz <>
Subject ReaderManager, more drama with things not being closed before closing the Directory
Date Thu, 20 Oct 2016 05:57:39 GMT
Hi all.

I seem to have a situation where ReaderManager is reducing a refCount
to 0 before it actually releases all its references.

It's difficult because it's all mixed up in our framework for multiple
ReaderManagers, which I'm still not convinced works because the
concurrency is impossible to figure out, and probably won't be allowed
to publish in order to have anyone at Lucene look at it either. (Which
is why I hope that someone at Lucene figures out how to manage more
than one index reliably one day...)

The stack trace trying to close the directory is just trying to
refresh the reader, but I guess this reader was the last one using a
Directory, so now we're closing that as well:

    java.lang.RuntimeException: Resources inside the directory did not
get closed before closing the directory
        at org.apache.lucene.index.IndexReader.decRef(

The stack trace which opened the resource and didn't close it is
apparently the first reader which ReaderManager:

    Caused by: java.lang.RuntimeException: unclosed IndexInput: _7d.tvd
        at org.apache.lucene.codecs.compressing.CompressingTermVectorsReader.<init>(
        at org.apache.lucene.codecs.compressing.CompressingTermVectorsFormat.vectorsReader(
        at org.apache.lucene.index.SegmentCoreReaders.<init>(
        at org.apache.lucene.index.SegmentReader.<init>(
        at org.apache.lucene.index.StandardDirectoryReader$1.doBody(
        at org.apache.lucene.index.StandardDirectoryReader$1.doBody(
        at org.apache.lucene.index.SegmentInfos$

But if it's the first reader held by the ReaderManager, I wouldn't
expect the refCount to be 0, so it shouldn't be closing the directory.

I can't reproduce this myself, so I can't just dump out conveniently
placed messages to figure out how it's happening...

But has anyone else seen something like this?

CustomReaderManager is probably shareable, it just does this:

    private static class CustomReaderManager extends
ReferenceManager<DirectoryReader> {
        private CustomReaderManager(Directory directory) throws IOException {
            current =

        protected void decRef(DirectoryReader reference) throws IOException {

        protected DirectoryReader refreshIfNeeded(DirectoryReader
referenceToRefresh) throws IOException {
            return DirectoryReader.openIfChanged(referenceToRefresh);

        protected boolean tryIncRef(DirectoryReader reference) {
            return reference.tryIncRef();

        protected int getRefCount(DirectoryReader reference) {
            return reference.getRefCount();

So basically the same as the normal one, except that it wraps the
reader in an UnInvertingDirectoryReader. The only reason we're forced
to subclass the manager to do this is that if we don't, each
UnInvertingDirectoryReader becomes a new instance, and basic caching
stuff stops working in some way.

DefaultIndexReaderSharer#release() is like this:

    private synchronized void release(DirectoryReader reader) {
        try {
            if (readerManager == null) {
                reader.decRef();          // assumes we don't own it anymore

            if (reader.getRefCount() == 1) {
                readerManager.close();              //  👈  close
happens from here
                readerManager = null;

        } catch (IOException e) {
            Logger.getLogger(getClass()).warn("Error closing reader", e);

So it's releasing the reader and then checking the ref count. If the
count is now 1, it closes it, but who knows, maybe this can race,
despite the synchronized keyword put here explicitly to prevent that,
based on the advice last time.

Basically, it is still essentially impossible to manage indexes
without getting weird errors like this. I can't even tell whether
we're doing something wrong or whether Lucene is simply not closing
the files it's opening. :/


To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message