Buggy code:
IndexWriterConfig iwc = newIndexWriterConfig ( TEST_VERSION_CURRENT , new org . apache . lucene . analysis . MockAnalyzer ( random ( ) ) ) ; final IndexWriter w = new IndexWriter ( dir , iwc ) ; final SearcherManager mgr = new SearcherManager ( w , true , new SearcherFactory ( ) { @ Override public IndexSearcher newSearcher ( IndexReader r ) { return new IndexSearcher ( r ) ; } } ) ; final Integer missing = - 1 ; final LiveFieldValues < IndexSearcher , Integer > rt = new LiveFieldValues < IndexSearcher , Integer > ( mgr , missing ) { @ Override protected Integer lookupFromSearcher ( IndexSearcher s , String id ) throws IOException { TermQuery tq = new TermQuery ( new Term ( "id" , id ) ) ; TopDocs hits = s . search ( tq , 1 ) ; assertTrue ( ( ( hits . totalHits ) <= 1 ) ) ; if ( ( hits . totalHits ) == 0 ) { return null ; } else { StoredDocument doc = s . doc ( hits . scoreDocs [ 0 ] . doc ) ; return ( ( Integer ) ( doc . getField ( "field" ) . numericValue ( ) ) ) ; } } } ; int numThreads = TestUtil . nextInt ( random ( ) , 2 , 5 ) ; if ( VERBOSE ) { System . out . println ( ( numThreads + "<seq2seq4repair_space>threads" ) ) ; } final CountDownLatch startingGun = new CountDownLatch ( 1 ) ; List < Thread > threads = new ArrayList < > ( ) ; final int iters = atLeast ( 1000 ) ; final int idCount = TestUtil . nextInt ( random ( ) , 100 , 10000 ) ; final double reopenChance = ( random ( ) . nextDouble ( ) ) * 0.01 ; final double deleteChance = ( random ( ) . nextDouble ( ) ) * 0.25 ; final double addChance = ( random ( ) . nextDouble ( ) ) * 0.5 ; for ( int t = 0 ; t < numThreads ; t ++ ) { final int threadID = t ; final Random threadRandom = new Random ( random ( ) . nextLong ( ) ) ; Thread thread = new Thread ( ) { @ Override public void run ( ) { try { Map < String , Integer > values = new HashMap < > ( ) ; List < String > allIDs = Collections . synchronizedList ( new ArrayList < String > ( ) ) ; startingGun . await ( ) ; for ( int iter = 0 ; iter < iters ; iter ++ ) { Document doc = new Document ( ) ; if ( ( threadRandom . nextDouble ( ) ) <= addChance ) { String id = String . format ( Locale . ROOT , "%d_%04x" , threadID , threadRandom . nextInt ( idCount ) ) ; Integer field = threadRandom . nextInt ( Integer . MAX_VALUE ) ; doc . add ( new org . apache . lucene . document . StringField ( "id" , id , Store . YES ) ) ; doc . add ( new org . apache . lucene . document . IntField ( "field" , field . intValue ( ) , Store . YES ) ) ; w . updateDocument ( new Term ( "id" , id ) , doc ) ; rt . add ( id , field ) ; if ( ( values . put ( id , field ) ) == null ) { allIDs . add ( id ) ; } } if ( ( ( allIDs . size ( ) ) > 0 ) && ( ( threadRandom . nextDouble ( ) ) <= deleteChance ) ) { String randomID = allIDs . get ( threadRandom . nextInt ( allIDs . size ( ) ) ) ; w . deleteDocuments ( new Term ( "id" , randomID ) ) ; rt . delete ( randomID ) ; values . put ( randomID , missing ) ; } if ( ( ( threadRandom . nextDouble ( ) ) <= reopenChance ) || ( ( rt . size ( ) ) > 10000 ) ) { mgr . maybeRefresh ( ) ; if ( VERBOSE ) { IndexSearcher s = mgr . acquire ( ) ; try { System . out . println ( ( "TEST:<seq2seq4repair_space>reopen<seq2seq4repair_space>" + s ) ) ; } finally { mgr . release ( s ) ; } System . out . println ( ( ( "TEST:<seq2seq4repair_space>" + ( values . size ( ) ) ) + "<seq2seq4repair_space>values" ) ) ; } } if ( ( threadRandom . nextInt ( 10 ) ) == 7 ) { assertEquals ( null , rt . get ( "foo" ) ) ; } if ( ( allIDs . size ( ) ) > 0 ) { String randomID = allIDs . get ( threadRandom . nextInt ( allIDs . size ( ) ) ) ; Integer expected = values . get ( randomID ) ; if ( expected == missing ) { expected = null ; } assertEquals ( ( "id=" + randomID ) , expected , rt . get ( randomID ) ) ; } } } catch ( Throwable t ) { throw new RuntimeException ( t ) ; } } } ; threads . add ( thread ) ; thread . start ( ) ; } startingGun . countDown ( ) ; for ( Thread thread : threads ) { thread . join ( ) ; } mgr . maybeRefresh ( ) ; assertEquals ( 0 , rt . size ( ) ) ; rt . close ( ) ; mgr . close ( ) ; <START_BUG> w . close ( ) ; <END_BUG> dir . close ( ) ; } }

Fix:
w . shutdown ( ) ;