Skip to content

Commit a3b6b10

Browse files
authored
Fix: Failure to close BlockSeriesClient cause store-gateway deadlock (#6086)
* Fix: Failure to close BlockSeriesClient cause store-gateway deadlock Signed-off-by: Alan Protasio <[email protected]> * Adding tests Signed-off-by: Alan Protasio <[email protected]> * reverting the change on get series Signed-off-by: Alan Protasio <[email protected]> * fix lint Signed-off-by: Alan Protasio <[email protected]> --------- Signed-off-by: Alan Protasio <[email protected]>
1 parent 4a3166f commit a3b6b10

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

pkg/store/bucket.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1226,6 +1226,7 @@ func (s *BucketStore) Series(req *storepb.SeriesRequest, srv storepb.Store_Serie
12261226
}
12271227

12281228
shardMatcher := req.ShardInfo.Matcher(&s.buffers)
1229+
12291230
blockClient := newBlockSeriesClient(
12301231
srv.Context(),
12311232
s.logger,
@@ -1238,9 +1239,11 @@ func (s *BucketStore) Series(req *storepb.SeriesRequest, srv storepb.Store_Serie
12381239
s.seriesBatchSize,
12391240
s.metrics.chunkFetchDuration,
12401241
)
1242+
12411243
defer blockClient.Close()
12421244

12431245
g.Go(func() error {
1246+
12441247
span, _ := tracing.StartSpan(gctx, "bucket_store_block_series", tracing.Tags{
12451248
"block.id": blk.meta.ULID,
12461249
"block.mint": blk.meta.MinTime,
@@ -1482,6 +1485,7 @@ func (s *BucketStore) LabelNames(ctx context.Context, req *storepb.LabelNamesReq
14821485
SkipChunks: true,
14831486
}
14841487
blockClient := newBlockSeriesClient(newCtx, s.logger, b, seriesReq, nil, bytesLimiter, nil, true, SeriesBatchSize, s.metrics.chunkFetchDuration)
1488+
defer blockClient.Close()
14851489

14861490
if err := blockClient.ExpandPostings(
14871491
reqSeriesMatchersNoExtLabels,
@@ -1656,6 +1660,7 @@ func (s *BucketStore) LabelValues(ctx context.Context, req *storepb.LabelValuesR
16561660
SkipChunks: true,
16571661
}
16581662
blockClient := newBlockSeriesClient(newCtx, s.logger, b, seriesReq, nil, bytesLimiter, nil, true, SeriesBatchSize, s.metrics.chunkFetchDuration)
1663+
defer blockClient.Close()
16591664

16601665
if err := blockClient.ExpandPostings(
16611666
reqSeriesMatchersNoExtLabels,

pkg/store/bucket_e2e_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"os"
1010
"path/filepath"
1111
"strings"
12+
"sync"
1213
"testing"
1314
"time"
1415

@@ -765,6 +766,10 @@ func TestBucketStore_LabelNames_e2e(t *testing.T) {
765766
} {
766767
t.Run(name, func(t *testing.T) {
767768
vals, err := s.store.LabelNames(ctx, tc.req)
769+
for _, b := range s.store.blocks {
770+
waitTimeout(t, &b.pendingReaders, 5*time.Second)
771+
}
772+
768773
testutil.Ok(t, err)
769774

770775
testutil.Equals(t, tc.expected, vals.Names)
@@ -868,6 +873,10 @@ func TestBucketStore_LabelValues_e2e(t *testing.T) {
868873
} {
869874
t.Run(name, func(t *testing.T) {
870875
vals, err := s.store.LabelValues(ctx, tc.req)
876+
for _, b := range s.store.blocks {
877+
waitTimeout(t, &b.pendingReaders, 5*time.Second)
878+
}
879+
871880
testutil.Ok(t, err)
872881

873882
testutil.Equals(t, tc.expected, emptyToNil(vals.Values))
@@ -882,3 +891,17 @@ func emptyToNil(values []string) []string {
882891
}
883892
return values
884893
}
894+
895+
func waitTimeout(t *testing.T, wg *sync.WaitGroup, timeout time.Duration) {
896+
c := make(chan struct{})
897+
go func() {
898+
defer close(c)
899+
wg.Wait()
900+
}()
901+
select {
902+
case <-c:
903+
return
904+
case <-time.After(timeout):
905+
t.Fatalf("timeout waiting wg for %v", timeout)
906+
}
907+
}

0 commit comments

Comments
 (0)