Buggy code:
} TestUtil . checkIndex ( this , getCrossCheckTermVectorsOnClose ( ) ) ; if ( assertNoUnreferencedFilesOnClose ) { Set < String > allFiles = new HashSet < > ( Arrays . asList ( listAll ( ) ) ) ; allFiles . removeAll ( pendingDeletions ) ; String [ ] startFiles = allFiles . toArray ( new String [ 0 ] ) ; IndexWriterConfig iwc = new IndexWriterConfig ( LuceneTestCase . TEST_VERSION_CURRENT , null ) ; iwc . setIndexDeletionPolicy ( INSTANCE ) ; new IndexWriter ( in , iwc ) . rollback ( ) ; String [ ] endFiles = in . listAll ( ) ; Set < String > startSet = new TreeSet < > ( Arrays . asList ( startFiles ) ) ; Set < String > endSet = new TreeSet < > ( Arrays . asList ( endFiles ) ) ; if ( ( pendingDeletions . contains ( "segments.gen" ) ) && ( endSet . contains ( "segments.gen" ) ) ) { startSet . add ( "segments.gen" ) ; if ( LuceneTestCase . VERBOSE ) { System . out . println ( "MDW:<seq2seq4repair_space>Unreferenced<seq2seq4repair_space>check:<seq2seq4repair_space>Ignoring<seq2seq4repair_space>segments.gen<seq2seq4repair_space>that<seq2seq4repair_space>we<seq2seq4repair_space>could<seq2seq4repair_space>not<seq2seq4repair_space>delete." ) ; } } for ( String file : pendingDeletions ) { if ( ( ( file . startsWith ( "segments" ) ) && ( ! ( file . equals ( "segments.gen" ) ) ) ) && ( endSet . contains ( file ) ) ) { startSet . add ( file ) ; if ( LuceneTestCase . VERBOSE ) { System . out . println ( ( ( "MDW:<seq2seq4repair_space>Unreferenced<seq2seq4repair_space>check:<seq2seq4repair_space>Ignoring<seq2seq4repair_space>segments<seq2seq4repair_space>file:<seq2seq4repair_space>" + file ) + "<seq2seq4repair_space>that<seq2seq4repair_space>we<seq2seq4repair_space>could<seq2seq4repair_space>not<seq2seq4repair_space>delete." ) ) ; } SegmentInfos sis = new SegmentInfos ( ) ; try { sis . read ( in , file ) ; } catch ( IOException ioe ) { } try { Set < String > ghosts = new HashSet ( sis . files ( in , false ) ) ; for ( String s : ghosts ) { if ( ( endSet . contains ( s ) ) && ( ! ( startSet . contains ( s ) ) ) ) { assert pendingDeletions . contains ( s ) ; if ( LuceneTestCase . VERBOSE ) { System . out . println ( ( ( ( ( ( "MDW:<seq2seq4repair_space>Unreferenced<seq2seq4repair_space>check:<seq2seq4repair_space>Ignoring<seq2seq4repair_space>referenced<seq2seq4repair_space>file:<seq2seq4repair_space>" + s ) + "<seq2seq4repair_space>" ) + "from<seq2seq4repair_space>" ) + file ) + "<seq2seq4repair_space>that<seq2seq4repair_space>we<seq2seq4repair_space>could<seq2seq4repair_space>not<seq2seq4repair_space>delete." ) ) ; } startSet . add ( s ) ; } } } catch ( Throwable t ) { System . err . println ( ( ( "ERROR<seq2seq4repair_space>processing<seq2seq4repair_space>leftover<seq2seq4repair_space>segments<seq2seq4repair_space>file<seq2seq4repair_space>" + file ) + ":" ) ) ; t . printStackTrace ( ) ; } } } startFiles = startSet . toArray ( new String [ 0 ] ) ; endFiles = endSet . toArray ( new String [ 0 ] ) ; if ( ! ( Arrays . equals ( startFiles , endFiles ) ) ) { List < String > removed = new ArrayList < > ( ) ; for ( String fileName : startFiles ) { if ( ! ( endSet . contains ( fileName ) ) ) { removed . add ( fileName ) ; } } List < String > added = new ArrayList < > ( ) ; for ( String fileName : endFiles ) { if ( ! ( startSet . contains ( fileName ) ) ) { added . add ( fileName ) ; } } String extras ; if ( ( removed . size ( ) ) != 0 ) { extras = "\n\nThese<seq2seq4repair_space>files<seq2seq4repair_space>were<seq2seq4repair_space>removed:<seq2seq4repair_space>" + removed ; } else { extras = "" ; } if ( ( added . size ( ) ) != 0 ) { extras += "\n\nThese<seq2seq4repair_space>files<seq2seq4repair_space>were<seq2seq4repair_space>added<seq2seq4repair_space>(waaaaaaaaaat!):<seq2seq4repair_space>" + added ; } if ( ( pendingDeletions . size ( ) ) != 0 ) { extras += "\n\nThese<seq2seq4repair_space>files<seq2seq4repair_space>we<seq2seq4repair_space>had<seq2seq4repair_space>previously<seq2seq4repair_space>tried<seq2seq4repair_space>to<seq2seq4repair_space>delete,<seq2seq4repair_space>but<seq2seq4repair_space>couldn\'t:<seq2seq4repair_space>" + pendingDeletions ; } assert false : ( ( ( "unreferenced<seq2seq4repair_space>files:<seq2seq4repair_space>before<seq2seq4repair_space>delete:\n<seq2seq4repair_space>" + ( Arrays . toString ( startFiles ) ) ) + "\n<seq2seq4repair_space>after<seq2seq4repair_space>delete:\n<seq2seq4repair_space>" ) + ( Arrays . toString ( endFiles ) ) ) + extras ; } DirectoryReader ir1 = DirectoryReader . open ( this ) ; int numDocs1 = ir1 . numDocs ( ) ; ir1 . close ( ) ; <START_BUG> new IndexWriter ( this , new IndexWriterConfig ( LuceneTestCase . TEST_VERSION_CURRENT , null ) ) . close ( ) ; <END_BUG> DirectoryReader ir2 = DirectoryReader . open ( this ) ; int numDocs2 = ir2 . numDocs ( ) ; ir2 . close ( ) ; assert numDocs1 == numDocs2 : ( ( "numDocs<seq2seq4repair_space>changed<seq2seq4repair_space>after<seq2seq4repair_space>opening//closing<seq2seq4repair_space>IW:<seq2seq4repair_space>before=" + numDocs1 ) + "<seq2seq4repair_space>after=" ) + numDocs2 ; } } } in . close ( ) ; } synchronized void removeOpenFile ( Closeable c , String name ) { } public synchronized void removeIndexOutput ( IndexOutput out , String name ) { } public synchronized void removeIndexInput ( IndexInput in , String name ) { } public static class Failure { public void eval ( MockDirectoryWrapper dir ) throws IOException { } public MockDirectoryWrapper . Failure reset ( ) { } protected boolean doFail ; public void setDoFail ( ) { } public void clearDoFail ( ) { } } ArrayList < MockDirectoryWrapper . Failure > failures ; public synchronized void failOn ( MockDirectoryWrapper . Failure fail ) { } synchronized void maybeThrowDeterministicException ( ) throws IOException { } @ Override public synchronized String [ ] listAll ( ) throws IOException { } @ Override public synchronized long fileLength ( String name ) throws IOException { } @ Override public synchronized Lock makeLock ( String name ) { } @ Override public synchronized void clearLock ( String name ) throws IOException { } @ Override public synchronized void setLockFactory ( LockFactory lockFactory ) throws IOException { } @ Override public synchronized LockFactory getLockFactory ( ) { } @ Override public synchronized String getLockID ( ) { } @ Override public synchronized void copy ( Directory to , String src , String dest , IOContext context ) throws IOException { } @ Override public IndexInputSlicer createSlicer ( final String name , IOContext context ) throws IOException { }

Fix:
new IndexWriter ( this , new IndexWriterConfig ( LuceneTestCase . TEST_VERSION_CURRENT , null ) ) . shutdown ( ) ;