Skip to content

Commit 89b75e1

Browse files
authored
[receiver/opencensusreceiver] Fix memory leak on shutdown (#31152)
The opencensus receiver opens a few servers on `Start`, each of which use the context's cancel to signal shutdown. Without cancelling the context, the goroutines were being leaked. This change properly cancels the context and shutdowns the receiver's servers. This also enables `goleak` checks on the opencensus receiver and exporter. I realize the exporter is not technically related here, but its tests were what alerted me to the leak in the receiver. The `TestSendTraces` and `TestSendMetrics` tests in the exporter were starting opencensus receivers and exporters, and failing on leaks. The change here is test-only for the exporter, and a bug fix for the receiver. #30438 All existing tests are passing, as well as added `goleak` checks.
1 parent c233bc7 commit 89b75e1

File tree

5 files changed

+63
-1
lines changed

5 files changed

+63
-1
lines changed

.chloggen/goleak_opencensuscomps.yaml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: bug_fix
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
7+
component: opencensusreceiver
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Fix memory leak on shutdown
11+
12+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
13+
issues: [31152]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# If your change doesn't affect end users or the exported elements of any package,
21+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
22+
# Optional: The change log or logs in which this entry should be included.
23+
# e.g. '[user]' or '[user, api]'
24+
# Include 'user' if the change is relevant to end users.
25+
# Include 'api' if there is a change to a library API.
26+
# Default: '[user]'
27+
change_logs: []

exporter/opencensusexporter/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ require (
2121
go.opentelemetry.io/collector/receiver v0.94.1
2222
go.opentelemetry.io/otel/metric v1.23.0
2323
go.opentelemetry.io/otel/trace v1.23.0
24+
go.uber.org/goleak v1.3.0
2425
google.golang.org/grpc v1.61.0
2526
)
2627

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package opencensusexporter
5+
6+
import (
7+
"testing"
8+
9+
"go.uber.org/goleak"
10+
)
11+
12+
func TestMain(m *testing.M) {
13+
goleak.VerifyTestMain(m)
14+
}

receiver/opencensusreceiver/opencensus.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ type ocReceiver struct {
3737
gatewayMux *gatewayruntime.ServeMux
3838
corsOrigins []string
3939
grpcServerSettings configgrpc.ServerConfig
40+
cancel context.CancelFunc
4041

4142
traceReceiver *octrace.Receiver
4243
metricsReceiver *ocmetrics.Receiver
@@ -192,6 +193,10 @@ func (ocr *ocReceiver) Shutdown(context.Context) error {
192193
// tests and code should be reactive in less than even 1second.
193194
// ocr.serverGRPC.Stop()
194195

196+
if ocr.cancel != nil {
197+
ocr.cancel()
198+
}
199+
195200
return err
196201
}
197202

@@ -213,7 +218,8 @@ func (ocr *ocReceiver) httpServer() *http.Server {
213218

214219
func (ocr *ocReceiver) startServer() error {
215220
// Register the grpc-gateway on the HTTP server mux
216-
c := context.Background()
221+
var c context.Context
222+
c, ocr.cancel = context.WithCancel(context.Background())
217223
opts := []grpc.DialOption{grpc.WithTransportCredentials(insecure.NewCredentials())}
218224
endpoint := ocr.ln.Addr().String()
219225

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package opencensusreceiver
5+
6+
import (
7+
"testing"
8+
9+
"go.uber.org/goleak"
10+
)
11+
12+
func TestMain(m *testing.M) {
13+
goleak.VerifyTestMain(m)
14+
}

0 commit comments

Comments
 (0)