From 4365f693306c33283d50dc87f4a884aaaacd5df4 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Wed, 8 Jan 2014 20:36:17 -0800 Subject: [PATCH] Fix data race in reindexing. See https://camlistore.org/issue/331 Change-Id: I210ae4e1779b7fe248196152a6acbc5728051249 --- TESTS | 5 +++++ pkg/index/index.go | 7 ++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/TESTS b/TESTS index 7ac2b830a..b4226c7c8 100644 --- a/TESTS +++ b/TESTS @@ -1,5 +1,10 @@ Tests needed +-- integration test of reindexing + race detector + +-- support for running race detector on all tests. when in race mode, also run + integration test children in race mode. + -- test that server/camlistored still builds & starts even when sqlite isn't available (TODO: hide it from the test by running make.go in a child process with a faked-out PKG_CONFIG environment or something, to make diff --git a/pkg/index/index.go b/pkg/index/index.go index 51505e8fe..0db4e4c96 100644 --- a/pkg/index/index.go +++ b/pkg/index/index.go @@ -142,11 +142,11 @@ func (x *Index) Reindex() error { nerr := 0 blobc := make(chan blob.Ref, 32) - defer close(blobc) enumCtx := ctx.New() enumErr := make(chan error, 1) go func() { + defer close(blobc) donec := enumCtx.Done() var lastTick time.Time enumErr <- blobserver.EnumerateAll(enumCtx, x.BlobSource, func(sb blob.SizedRef) error { @@ -167,8 +167,11 @@ func (x *Index) Reindex() error { }) }() const j = 4 // arbitrary concurrency level + var wg sync.WaitGroup for i := 0; i < j; i++ { + wg.Add(1) go func() { + defer wg.Done() for br := range blobc { if err := x.reindex(br); err != nil { log.Printf("Error reindexing %v: %v", br, err) @@ -184,6 +187,8 @@ func (x *Index) Reindex() error { if err := <-enumErr; err != nil { return err } + wg.Wait() + log.Printf("Index rebuild complete.") nerrmu.Lock() // no need to unlock if nerr != 0 {