Skip to content

Commit

Permalink
Fix race condition on worker failure
Browse files Browse the repository at this point in the history
If a worker fails, the context is cancelled, this leads to all workers+the scanner stopping immediately.

The race conditions happens because if all the workers close before the scanner, the code will proceed and close the error channel. Then the scanner fails when attempting to publish to the error channel.

This is solved by adding the scanner to the worker group. this will make sure we always wait for all goroutines that publish to errCh to stop
  • Loading branch information
MetalBlueberry committed Dec 19, 2024
1 parent 5885d47 commit d7eb615
Showing 1 changed file with 2 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pkg/csvcopy/csvcopy.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,9 @@ func (c *Copier) Copy(ctx context.Context, reader io.Reader) (Result, error) {
}

start := time.Now()
workerWg.Add(1)
go func() {
defer workerWg.Done()
if err := batch.Scan(ctx, reader, batchChan, opts); err != nil {
errCh <- fmt.Errorf("failed reading input: %w", err)
cancel()
Expand Down

0 comments on commit d7eb615

Please sign in to comment.