Buggy code:
} final int [ ] fieldsPerDoc = new int [ NUM_DOCS ] ; int baseCount = 0 ; for ( int docCount = 0 ; docCount < NUM_DOCS ; docCount ++ ) { final int fieldCount = TestUtil . nextInt ( random ( ) , 1 , 17 ) ; fieldsPerDoc [ docCount ] = fieldCount - 1 ; final int finalDocCount = docCount ; if ( VERBOSE ) { System . out . println ( ( ( ( "TEST:<seq2seq4repair_space>" + fieldCount ) + "<seq2seq4repair_space>fields<seq2seq4repair_space>in<seq2seq4repair_space>doc<seq2seq4repair_space>" ) + docCount ) ) ; } final int finalBaseCount = baseCount ; baseCount += fieldCount - 1 ; IndexDocument d = new IndexDocument ( ) { @ Override public Iterable < IndexableField > indexableFields ( ) { return new Iterable < IndexableField > ( ) { @ Override public Iterator < IndexableField > iterator ( ) { return new Iterator < IndexableField > ( ) { int fieldUpto = 0 ; private IndexableField next ; @ Override public boolean hasNext ( ) { if ( ( fieldUpto ) >= fieldCount ) return false ; next = null ; if ( ( fieldUpto ) == 0 ) { fieldUpto = 1 ; next = newStringField ( "id" , ( "" + finalDocCount ) , YES ) ; } else { next = new TestIndexableField . MyField ( ( finalBaseCount + ( ( ( fieldUpto ) ++ ) - 1 ) ) ) ; } if ( ( ( next ) != null ) && ( next . fieldType ( ) . indexed ( ) ) ) return true ; else return this . hasNext ( ) ; } @ Override public IndexableField next ( ) { assert ( fieldUpto ) <= fieldCount ; if ( ( ( next ) == null ) && ( ! ( hasNext ( ) ) ) ) { return null ; } else { return next ; } } @ Override public void remove ( ) { throw new UnsupportedOperationException ( ) ; } } ; } } ; } @ Override public Iterable < StorableField > storableFields ( ) { return new Iterable < StorableField > ( ) { @ Override public Iterator < StorableField > iterator ( ) { return new Iterator < StorableField > ( ) { int fieldUpto = 0 ; private StorableField next = null ; @ Override public boolean hasNext ( ) { if ( ( fieldUpto ) == fieldCount ) return false ; next = null ; if ( ( fieldUpto ) == 0 ) { fieldUpto = 1 ; next = newStringField ( "id" , ( "" + finalDocCount ) , YES ) ; } else { next = new TestIndexableField . MyField ( ( finalBaseCount + ( ( ( fieldUpto ) ++ ) - 1 ) ) ) ; } if ( ( ( next ) != null ) && ( next . fieldType ( ) . stored ( ) ) ) return true ; else return this . hasNext ( ) ; } @ Override public StorableField next ( ) { assert ( fieldUpto ) <= fieldCount ; if ( ( ( next ) == null ) && ( ! ( hasNext ( ) ) ) ) { return null ; } else { return next ; } } @ Override public void remove ( ) { throw new UnsupportedOperationException ( ) ; } } ; } } ; } } ; w . addDocument ( d ) ; } final IndexReader r = w . getReader ( ) ; <START_BUG> w . close ( ) ; <END_BUG> final IndexSearcher s = newSearcher ( r ) ; int counter = 0 ; for ( int id = 0 ; id < NUM_DOCS ; id ++ ) { if ( VERBOSE ) { System . out . println ( ( ( ( ( ( "TEST:<seq2seq4repair_space>verify<seq2seq4repair_space>doc<seq2seq4repair_space>id=" + id ) + "<seq2seq4repair_space>(" ) + ( fieldsPerDoc [ id ] ) ) + "<seq2seq4repair_space>fields)<seq2seq4repair_space>counter=" ) + counter ) ) ; } final TopDocs hits = s . search ( new org . apache . lucene . search . TermQuery ( new Term ( "id" , ( "" + id ) ) ) , 1 ) ; assertEquals ( 1 , hits . totalHits ) ; final int docID = hits . scoreDocs [ 0 ] . doc ; final StoredDocument doc = s . doc ( docID ) ; final int endCounter = counter + ( fieldsPerDoc [ id ] ) ; while ( counter < endCounter ) { final String name = "f" + counter ; final int fieldID = counter % 10 ; final boolean stored = ( ( counter & 1 ) == 0 ) || ( fieldID == 3 ) ; final boolean binary = fieldID == 3 ; final boolean indexed = fieldID != 3 ; final String stringValue ; if ( ( fieldID != 3 ) && ( fieldID != 9 ) ) { stringValue = "text<seq2seq4repair_space>" + counter ; } else { stringValue = null ; } if ( stored ) { StorableField f = doc . getField ( name ) ; assertNotNull ( ( ( ( "doc<seq2seq4repair_space>" + id ) + "<seq2seq4repair_space>doesn't<seq2seq4repair_space>have<seq2seq4repair_space>field<seq2seq4repair_space>f" ) + counter ) , f ) ; if ( binary ) { assertNotNull ( ( ( ( "doc<seq2seq4repair_space>" + id ) + "<seq2seq4repair_space>doesn't<seq2seq4repair_space>have<seq2seq4repair_space>field<seq2seq4repair_space>f" ) + counter ) , f ) ; final BytesRef b = f . binaryValue ( ) ; assertNotNull ( b ) ; assertEquals ( 10 , b . length ) ; for ( int idx = 0 ; idx < 10 ; idx ++ ) { assertEquals ( ( ( byte ) ( idx + counter ) ) , b . bytes [ ( ( b . offset ) + idx ) ] ) ; } } else { assert stringValue != null ; assertEquals ( stringValue , f . stringValue ( ) ) ; }

Fix:
w . shutdown ( ) ;