Skip to content

Commit 41fbf77

Browse files
authored
Fix goroutines leak at reportDataToBackend (#3068)
Below is the monitor panel of the number of parca-agent goroutines before and after applying this patch.
2 parents eb446e4 + 86e9bef commit 41fbf77

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

reporter/grpc_upload_client.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ func (c *GrpcUploadClient) grpcUpload(ctx context.Context, uploadInstructions *d
6060
return 0, fmt.Errorf("initiate upload: %w", err)
6161
}
6262

63+
defer func() {
64+
if stream != nil {
65+
stream.CloseAndRecv()
66+
}
67+
}()
68+
6369
err = stream.Send(&debuginfopb.UploadRequest{
6470
Data: &debuginfopb.UploadRequest_Info{
6571
Info: &debuginfopb.UploadInfo{

reporter/parca_reporter.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1001,6 +1001,7 @@ func (r *ParcaReporter) reportDataToBackend(ctx context.Context, buf *bytes.Buff
10011001
if err != nil {
10021002
return err
10031003
}
1004+
defer client.CloseSend()
10041005

10051006
if err := client.Send(&profilestorepb.WriteRequest{
10061007
Record: buf.Bytes(),
@@ -1085,7 +1086,20 @@ func (r *ParcaReporter) reportDataToBackend(ctx context.Context, buf *bytes.Buff
10851086
}
10861087
r.stacktraceWriteRequestBytes.Add(float64(buf.Len()))
10871088

1088-
return client.CloseSend()
1089+
// CloseSend() is deferred at the top of this function.
1090+
// Drain any remaining responses so the gRPC helper goroutine
1091+
// (newClientStreamWithParams.func4) can exit.
1092+
for {
1093+
_, err := client.Recv()
1094+
if err == io.EOF {
1095+
break
1096+
}
1097+
if err != nil {
1098+
return err
1099+
}
1100+
}
1101+
1102+
return nil
10891103
}
10901104

10911105
func (r *ParcaReporter) writeCommonLabels(w *SampleWriter, rows uint64) {

0 commit comments

Comments
 (0)