Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix coroutine leak #7821

Merged
merged 7 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions pkg/store/proxy_merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -446,8 +446,10 @@ func newAsyncRespSet(
emptyStreamResponses prometheus.Counter,
) (respSet, error) {

var span opentracing.Span
var closeSeries context.CancelFunc
var (
span opentracing.Span
cancel context.CancelFunc
)

storeID, storeAddr, isLocalStore := storeInfo(st)
seriesCtx := grpc_opentracing.ClientAddContextTags(ctx, opentracing.Tags{
Expand All @@ -459,7 +461,7 @@ func newAsyncRespSet(
"store.addr": storeAddr,
})

seriesCtx, closeSeries = context.WithCancel(seriesCtx)
seriesCtx, cancel = context.WithCancel(seriesCtx)

shardMatcher := shardInfo.Matcher(buffers)

Expand All @@ -474,7 +476,7 @@ func newAsyncRespSet(

span.SetTag("err", err.Error())
span.Finish()
closeSeries()
cancel()
return nil, err
}

Expand All @@ -490,6 +492,13 @@ func newAsyncRespSet(
}
}

closeSeries := func() {
cancel()
err := cl.CloseSend()
if err != nil {
level.Warn(logger).Log("msg", "detected close error", "err", err.Error())
}
}
switch retrievalStrategy {
case LazyRetrieval:
return newLazyRespSet(
Expand Down
40 changes: 39 additions & 1 deletion pkg/store/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2251,14 +2251,26 @@ func TestProxyStore_NotLeakingOnPrematureFinish(t *testing.T) {
MinTime: math.MinInt64,
MaxTime: math.MaxInt64,
},
&storetestutil.TestClient{
StoreClient: storepb.ServerAsClient(&storeServerStub{
delay: 50 * time.Millisecond,
responses: []*storepb.SeriesResponse{
storeSeriesResponse(t, labels.FromStrings("b", "a"), []sample{{0, 0}, {2, 1}, {3, 2}}),
storeSeriesResponse(t, labels.FromStrings("b", "b"), []sample{{0, 0}, {2, 1}, {3, 2}}),
storeSeriesResponse(t, labels.FromStrings("b", "c"), []sample{{0, 0}, {2, 1}, {3, 2}}),
},
}),
MinTime: math.MinInt64,
MaxTime: math.MaxInt64,
},
}

logger := log.NewNopLogger()
p := &ProxyStore{
logger: logger,
stores: func() []Client { return clients },
metrics: newProxyStoreMetrics(nil),
responseTimeout: 0,
responseTimeout: 50 * time.Millisecond,
retrievalStrategy: EagerRetrieval,
tsdbSelector: DefaultSelector,
}
Expand All @@ -2275,6 +2287,32 @@ func TestProxyStore_NotLeakingOnPrematureFinish(t *testing.T) {
}))
testutil.NotOk(t, ctx.Err())
})
t.Run("client timeout", func(t *testing.T) {
ctx := context.Background()
testutil.NotOk(t, p.Series(&storepb.SeriesRequest{Matchers: []storepb.LabelMatcher{{}}, PartialResponseStrategy: storepb.PartialResponseStrategy_ABORT}, &mockedSeriesServer{
ctx: ctx,
send: func(*storepb.SeriesResponse) error {
return nil
},
}))
})
}

type storeServerStub struct {
storepb.StoreServer

delay time.Duration
responses []*storepb.SeriesResponse
}

func (m *storeServerStub) Series(_ *storepb.SeriesRequest, server storepb.Store_SeriesServer) error {
for _, r := range m.responses {
<-time.After(m.delay)
if err := server.Send(r); err != nil {
return err
}
}
return nil
}

func TestProxyStore_storeMatchMetadata(t *testing.T) {
Expand Down
3 changes: 3 additions & 0 deletions pkg/store/storepb/inprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ func (c *inProcessClient) Recv() (*SeriesResponse, error) {
return nil, err
}
if !ok {
if c.ctx.Err() != nil {
return nil, c.ctx.Err()
}
return nil, io.EOF
}
return resp, err
Expand Down
1 change: 1 addition & 0 deletions pkg/store/storepb/testutil/store_series_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,4 @@ func (c *StoreSeriesClient) Recv() (*storepb.SeriesResponse, error) {
func (c *StoreSeriesClient) Context() context.Context {
return c.Ctx
}
func (c *StoreSeriesClient) CloseSend() error { return nil }
Loading