From 86f4034ceaa724c38c5126cd8a25a6ed5308f59c Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Tue, 8 Sep 2020 12:43:28 +0800 Subject: [PATCH] Fix data race when batch.Reset. Fix #1460 --- index/scorch/scorch.go | 8 +++++--- index/upsidedown/upsidedown.go | 10 ++++++---- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/index/scorch/scorch.go b/index/scorch/scorch.go index ba98a460d..f23648c7b 100644 --- a/index/scorch/scorch.go +++ b/index/scorch/scorch.go @@ -334,15 +334,17 @@ func (s *Scorch) Batch(batch *index.Batch) (err error) { // FIXME could sort ids list concurrent with analysis? if numUpdates > 0 { - go func() { - for _, doc := range batch.IndexOps { + // batch.IndexOps maybe reseted when the goroutine is running, so + // we should send a copy of it. + go func(ops map[string]*document.Document) { + for _, doc := range ops { if doc != nil { aw := index.NewAnalysisWork(s, doc, resultChan) // put the work on the queue s.analysisQueue.Queue(aw) } } - }() + }(batch.IndexOps) } // wait for analysis result diff --git a/index/upsidedown/upsidedown.go b/index/upsidedown/upsidedown.go index 8e915c6ad..d84a03302 100644 --- a/index/upsidedown/upsidedown.go +++ b/index/upsidedown/upsidedown.go @@ -819,16 +819,18 @@ func (udc *UpsideDownCouch) Batch(batch *index.Batch) (err error) { } if numUpdates > 0 { - go func() { - for k := range batch.IndexOps { - doc := batch.IndexOps[k] + // batch.IndexOps maybe reseted when the goroutine is running, so + // we should send a copy of it. + go func(ops map[string]*document.Document) { + for k := range ops { + doc := ops[k] if doc != nil { aw := index.NewAnalysisWork(udc, doc, resultChan) // put the work on the queue udc.analysisQueue.Queue(aw) } } - }() + }(batch.IndexOps) } // retrieve back index rows concurrent with analysis