Skip to content

Commit e9702b8

Browse files
authored
Fix race when stopping chunks memcached client (cortexproject#4511)
* Fix race when stopping chunks memcached client We need to check again if we have been asked to quit before writing to the results chan. Also, the reader of that chan should not close it, because this can trigger a panic in a writer. The chan will be cleaned up by garbage- collection once all the readers and writers have exited on quit. Signed-off-by: Bryan Boreham <bjboreham@gmail.com> * simplify select Don't open another race where quit is closed just after we check it. Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
1 parent f9bef66 commit e9702b8

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

pkg/chunk/cache/memcached.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,13 @@ func NewMemcached(cfg MemcachedConfig, client MemcachedClient, name string, reg
8989
batchID: input.batchID,
9090
}
9191
res.found, res.bufs, res.missed = c.fetch(input.ctx, input.keys)
92-
input.resultCh <- res
92+
// No-one will be reading from resultCh if we were asked to quit
93+
// during the fetch, so check again before writing to it.
94+
select {
95+
case <-c.quit:
96+
return
97+
case input.resultCh <- res:
98+
}
9399
}
94100
}
95101
}()
@@ -214,7 +220,6 @@ loopResults:
214220
results[result.batchID] = result
215221
}
216222
}
217-
close(resultsCh)
218223

219224
for _, result := range results {
220225
if result == nil {

0 commit comments

Comments
 (0)