Buggy code:
public void testIsCurrent ( ) throws IOException { } public void testAddIndexes ( ) throws Exception { } public void testAddIndexes2 ( ) throws Exception { } public void testDeleteFromIndexWriter ( ) throws Exception { } public void testAddIndexesAndDoDeletesThreads ( ) throws Throwable { } private class AddDirectoriesThreads { Directory addDir ; static final int NUM_THREADS = 5 ; static final int NUM_INIT_DOCS = 100 ; int numDirs ; final Thread [ ] threads = new Thread [ TestIndexWriterReader . AddDirectoriesThreads . NUM_THREADS ] ; IndexWriter mainWriter ; final List < Throwable > failures = new ArrayList < Throwable > ( ) ; IndexReader [ ] readers ; boolean didClose = false ; AtomicInteger count = new AtomicInteger ( 0 ) ; AtomicInteger numaddIndexes = new AtomicInteger ( 0 ) ; public AddDirectoriesThreads ( int numDirs , IndexWriter mainWriter ) throws Throwable { } void joinThreads ( ) { } void close ( boolean doWait ) throws Throwable { } void closeDir ( ) throws Throwable { } void handle ( Throwable t ) { } void launchThreads ( final int numIter ) { } void doBody ( int j , Directory [ ] dirs ) throws Throwable { } } public void testIndexWriterReopenSegmentOptimize ( ) throws Exception { } public void testIndexWriterReopenSegment ( ) throws Exception { } public void doTestIndexWriterReopenSegment ( boolean optimize ) throws Exception { } public static void createIndex ( Random random , Directory dir1 , String indexName , boolean multiSegment ) throws IOException { } public static void createIndexNoClose ( boolean multiSegment , String indexName , IndexWriter w ) throws IOException { } private static class MyWarmer extends IndexWriter . IndexReaderWarmer { int warmCount ; @ Override public void warm ( IndexReader reader ) throws IOException { } } public void testMergeWarmer ( ) throws Exception { } public void testAfterCommit ( ) throws Exception { } public void testAfterClose ( ) throws Exception { } public void testDuringAddIndexes ( ) throws Exception { MockDirectoryWrapper dir1 = newDirectory ( ) ; final IndexWriter writer = new IndexWriter ( dir1 , newIndexWriterConfig ( TEST_VERSION_CURRENT , new org . apache . lucene . analysis . MockAnalyzer ( random ) ) . setMergePolicy ( newLogMergePolicy ( 2 ) ) ) ; writer . setInfoStream ( TestIndexWriterReader . infoStream ) ; ( ( LogMergePolicy ) ( writer . getConfig ( ) . getMergePolicy ( ) ) ) . setMergeFactor ( 2 ) ; TestIndexWriterReader . createIndexNoClose ( false , "test" , writer ) ; writer . commit ( ) ; final Directory [ ] dirs = new Directory [ 10 ] ; for ( int i = 0 ; i < 10 ; i ++ ) { dirs [ i ] = new MockDirectoryWrapper ( random , new org . apache . lucene . store . RAMDirectory ( dir1 ) ) ; } IndexReader r = writer . getReader ( ) ; final int NUM_THREAD = 5 ; final float SECONDS = 0.5F ; final long endTime = ( ( long ) ( ( System . currentTimeMillis ( ) ) + ( 1000.0 * SECONDS ) ) ) ; final List < Throwable > excs = Collections . synchronizedList ( new ArrayList < Throwable > ( ) ) ; final Thread [ ] threads = new Thread [ NUM_THREAD ] ; for ( int i = 0 ; i < NUM_THREAD ; i ++ ) { threads [ i ] = new Thread ( ) { @ Override public void run ( ) { do { try { writer . addIndexes ( dirs ) ; writer . maybeMerge ( ) ; } catch ( Throwable t ) { excs . add ( t ) ; throw new RuntimeException ( t ) ; } } while ( ( System . currentTimeMillis ( ) ) < endTime ) ; } } ; threads [ i ] . setDaemon ( true ) ; threads [ i ] . start ( ) ; } int lastCount = 0 ; while ( ( System . currentTimeMillis ( ) ) < endTime ) { IndexReader r2 = IndexReader . openIfChanged ( r ) ; if ( r2 != null ) { r . close ( ) ; r = r2 ; } Query q = new org . apache . lucene . search . TermQuery ( new Term ( "indexname" , "test" ) ) ; IndexSearcher searcher = newSearcher ( r ) ; final int count = searcher . search ( q , 10 ) . totalHits ; searcher . close ( ) ; assertTrue ( ( count >= lastCount ) ) ; lastCount = count ; } for ( int i = 0 ; i < NUM_THREAD ; i ++ ) { threads [ i ] . join ( ) ; } IndexReader r2 = IndexReader . openIfChanged ( r ) ; <START_BUG> if ( r2 != r ) { <END_BUG> r . close ( ) ; r = r2 ; } Query q = new org . apache . lucene . search . TermQuery ( new Term ( "indexname" , "test" ) ) ; IndexSearcher searcher = newSearcher ( r ) ; final int count = searcher . search ( q , 10 ) . totalHits ; searcher . close ( ) ; assertTrue ( ( count >= lastCount ) ) ; assertEquals ( 0 , excs . size ( ) ) ; r . close ( ) ; assertEquals ( 0 , dir1 . getOpenDeletedFiles ( ) . size ( ) ) ; writer . close ( ) ; dir1 . close ( ) ; } public void testDuringAddDelete ( ) throws Exception { } public void testExpungeDeletes ( ) throws Throwable { } public void testDeletesNumDocs ( ) throws Throwable { } public void testEmptyIndex ( ) throws Exception { } public void testSegmentWarmer ( ) throws Exception { } public void testNoTermsIndex ( ) throws Exception { } public void testReopenAfterNoRealChange ( ) throws Exception { } }

Fix:
if ( r2 != null ) {