@@ -320,19 +320,22 @@ func (s *EspressoStreamer[B]) processHotShotRange(ctx context.Context, start, fi
320320// processRemainingBatches is a helper method that checks the remaining batches
321321// and prunes or adds them to the batch buffer as appropriate.
322322func (s * EspressoStreamer [B ]) processRemainingBatches (ctx context.Context ) {
323+ // Collect keys to delete, without modifying the batch list during iteration.
324+ var keysToDelete []common.Hash
325+
323326 // Process the remaining batches
324327 for k , batch := range s .RemainingBatches {
325328 validity , pos := s .CheckBatch (ctx , batch )
326329
327330 switch validity {
328331 case BatchDrop :
329332 s .Log .Warn ("Dropping batch" , "batch" , batch )
330- delete ( s . RemainingBatches , k )
333+ keysToDelete = append ( keysToDelete , k )
331334 continue
332335
333336 case BatchPast :
334337 s .Log .Warn ("Batch already processed. Skipping" , "batch" , batch )
335- delete ( s . RemainingBatches , k )
338+ keysToDelete = append ( keysToDelete , k )
336339 continue
337340
338341 case BatchUndecided :
@@ -349,6 +352,11 @@ func (s *EspressoStreamer[B]) processRemainingBatches(ctx context.Context) {
349352
350353 s .Log .Trace ("Remaining list" , "Inserting batch into buffer" , "batch" , batch )
351354 s .BatchBuffer .Insert (batch , pos )
355+ keysToDelete = append (keysToDelete , k )
356+ }
357+
358+ // Delete keys all at once.
359+ for _ , k := range keysToDelete {
352360 delete (s .RemainingBatches , k )
353361 }
354362}
0 commit comments