Buggy code:
} else { Document d = new Document ( ) ; d . add ( newStringField ( "id" , Integer . toString ( id ) , YES ) ) ; d . add ( newField ( field , Long . toString ( nextVal ) , storedOnlyType ) ) ; if ( VERBOSE ) { System . out . println ( ( ( ( ( ( "TEST:<seq2seq4repair_space>" + ( Thread . currentThread ( ) . getName ( ) ) ) + ":<seq2seq4repair_space>u<seq2seq4repair_space>id:" ) + id ) + "<seq2seq4repair_space>val=" ) + nextVal ) ) ; } writer . updateDocument ( new Term ( "id" , Integer . toString ( id ) ) , d ) ; if ( tombstones ) { writer . deleteDocuments ( new Term ( "id" , ( "-" + ( Integer . toString ( id ) ) ) ) ) ; } model . put ( id , nextVal ) ; } } if ( ! before ) { lastId = id ; } } } } catch ( Throwable e ) { System . out . println ( ( ( Thread . currentThread ( ) . getName ( ) ) + ":<seq2seq4repair_space>FAILED:<seq2seq4repair_space>unexpected<seq2seq4repair_space>exception" ) ) ; e . printStackTrace ( System . out ) ; throw new RuntimeException ( e ) ; } } } ; threads . add ( thread ) ; } for ( int i = 0 ; i < nReadThreads ; i ++ ) { Thread thread = new Thread ( ( "READER" + i ) ) { Random rand = new Random ( random ( ) . nextInt ( ) ) ; @ Override public void run ( ) { try { IndexReader lastReader = null ; IndexSearcher lastSearcher = null ; while ( ( operations . decrementAndGet ( ) ) >= 0 ) { int id = ( ( rand . nextInt ( 100 ) ) < 25 ) ? lastId : rand . nextInt ( ndocs ) ; long val ; DirectoryReader r ; synchronized ( TestStressNRT . this ) { val = committedModel . get ( id ) ; r = reader ; r . incRef ( ) ; } if ( VERBOSE ) { System . out . println ( ( ( ( ( ( ( ( "TEST:<seq2seq4repair_space>" + ( Thread . currentThread ( ) . getName ( ) ) ) + ":<seq2seq4repair_space>s<seq2seq4repair_space>id=" ) + id ) + "<seq2seq4repair_space>val=" ) + val ) + "<seq2seq4repair_space>r=" ) + ( r . getVersion ( ) ) ) ) ; } IndexSearcher searcher ; if ( r == lastReader ) { searcher = lastSearcher ; } else { searcher = newSearcher ( r ) ; lastReader = r ; lastSearcher = searcher ; } Query q = new org . apache . lucene . search . TermQuery ( new Term ( "id" , Integer . toString ( id ) ) ) ; TopDocs results = searcher . search ( q , 10 ) ; if ( ( ( results . totalHits ) == 0 ) && tombstones ) { q = new org . apache . lucene . search . TermQuery ( new Term ( "id" , ( "-" + ( Integer . toString ( id ) ) ) ) ) ; results = searcher . search ( q , 1 ) ; if ( ( results . totalHits ) == 0 ) { if ( val == ( - 1L ) ) { r . decRef ( ) ; continue ; } fail ( ( ( ( ( ( "No<seq2seq4repair_space>documents<seq2seq4repair_space>or<seq2seq4repair_space>tombstones<seq2seq4repair_space>found<seq2seq4repair_space>for<seq2seq4repair_space>id<seq2seq4repair_space>" + id ) + ",<seq2seq4repair_space>expected<seq2seq4repair_space>at<seq2seq4repair_space>least<seq2seq4repair_space>" ) + val ) + "<seq2seq4repair_space>reader=" ) + r ) ) ; } } if ( ( ( results . totalHits ) == 0 ) && ( ! tombstones ) ) { } else { if ( ( results . totalHits ) != 1 ) { System . out . println ( ( ( ( "FAIL:<seq2seq4repair_space>hits<seq2seq4repair_space>id:" + id ) + "<seq2seq4repair_space>val=" ) + val ) ) ; for ( ScoreDoc sd : results . scoreDocs ) { final StoredDocument doc = r . document ( sd . doc ) ; System . out . println ( ( ( ( ( ( "<seq2seq4repair_space>docID=" + ( sd . doc ) ) + "<seq2seq4repair_space>id:" ) + ( doc . get ( "id" ) ) ) + "<seq2seq4repair_space>foundVal=" ) + ( doc . get ( field ) ) ) ) ; } fail ( ( ( ( ( ( "id=" + id ) + "<seq2seq4repair_space>reader=" ) + r ) + "<seq2seq4repair_space>totalHits=" ) + ( results . totalHits ) ) ) ; } StoredDocument doc = searcher . doc ( results . scoreDocs [ 0 ] . doc ) ; long foundVal = Long . parseLong ( doc . get ( field ) ) ; if ( foundVal < ( Math . abs ( val ) ) ) { fail ( ( ( ( ( ( ( ( "foundVal=" + foundVal ) + "<seq2seq4repair_space>val=" ) + val ) + "<seq2seq4repair_space>id=" ) + id ) + "<seq2seq4repair_space>reader=" ) + r ) ) ; } } r . decRef ( ) ; } } catch ( Throwable e ) { operations . set ( ( - 1L ) ) ; System . out . println ( ( ( Thread . currentThread ( ) . getName ( ) ) + ":<seq2seq4repair_space>FAILED:<seq2seq4repair_space>unexpected<seq2seq4repair_space>exception" ) ) ; e . printStackTrace ( System . out ) ; throw new RuntimeException ( e ) ; } } } ; threads . add ( thread ) ; } for ( Thread thread : threads ) { thread . start ( ) ; } for ( Thread thread : threads ) { thread . join ( ) ; } <START_BUG> writer . close ( ) ; <END_BUG> if ( VERBOSE ) { System . out . println ( ( "TEST:<seq2seq4repair_space>close<seq2seq4repair_space>reader=" + ( reader ) ) ) ; } reader . close ( ) ; dir . close ( ) ; } }

Fix:
writer . shutdown ( ) ;