From 36084a97e3426848106df854110698c5207faba3 Mon Sep 17 00:00:00 2001 From: Curtis Robert Date: Wed, 10 Apr 2024 01:22:04 -0700 Subject: [PATCH] [receiver/jmx] Fix memory leak on shutdown (#32289) **Description:** The receiver was starting a goroutine that would run without being stopped during shutdown. This changes the goroutine to be stopped during shutdown. `goleak` is also added as a part of this change. **Link to tracking Issue:** #30438 **Testing:** All existing tests are passing, as well as added `goleak` check. --- .chloggen/goleak_jmxrec.yaml | 27 +++++++++++++++++++++++++++ receiver/jmxreceiver/go.mod | 1 + receiver/jmxreceiver/package_test.go | 14 ++++++++++++++ receiver/jmxreceiver/receiver.go | 22 ++++++++++++++++++---- 4 files changed, 60 insertions(+), 4 deletions(-) create mode 100644 .chloggen/goleak_jmxrec.yaml create mode 100644 receiver/jmxreceiver/package_test.go diff --git a/.chloggen/goleak_jmxrec.yaml b/.chloggen/goleak_jmxrec.yaml new file mode 100644 index 000000000000..8a66407fdbbe --- /dev/null +++ b/.chloggen/goleak_jmxrec.yaml @@ -0,0 +1,27 @@ +# Use this changelog template to create an entry for release notes. + +# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' +change_type: bug_fix + +# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) +component: jmxreceiver + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: Fix memory leak during component shutdown + +# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. +issues: [32289] + +# (Optional) One or more lines of additional information to render under the primary note. +# These lines will be padded with 2 spaces and then inserted directly into the document. +# Use pipe (|) for multiline entries. +subtext: + +# If your change doesn't affect end users or the exported elements of any package, +# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. +# Optional: The change log or logs in which this entry should be included. +# e.g. '[user]' or '[user, api]' +# Include 'user' if the change is relevant to end users. +# Include 'api' if there is a change to a library API. +# Default: '[user]' +change_logs: [] diff --git a/receiver/jmxreceiver/go.mod b/receiver/jmxreceiver/go.mod index 4e1b32cec68b..d4e2e4177231 100644 --- a/receiver/jmxreceiver/go.mod +++ b/receiver/jmxreceiver/go.mod @@ -19,6 +19,7 @@ require ( go.opentelemetry.io/collector/receiver/otlpreceiver v0.97.1-0.20240409140257-792fac1b62d4 go.opentelemetry.io/otel/metric v1.24.0 go.opentelemetry.io/otel/trace v1.24.0 + go.uber.org/goleak v1.3.0 go.uber.org/zap v1.27.0 ) diff --git a/receiver/jmxreceiver/package_test.go b/receiver/jmxreceiver/package_test.go new file mode 100644 index 000000000000..27ad8f75003f --- /dev/null +++ b/receiver/jmxreceiver/package_test.go @@ -0,0 +1,14 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package jmxreceiver + +import ( + "testing" + + "go.uber.org/goleak" +) + +func TestMain(m *testing.M) { + goleak.VerifyTestMain(m) +} diff --git a/receiver/jmxreceiver/receiver.go b/receiver/jmxreceiver/receiver.go index 8b405f7c741e..554701374218 100644 --- a/receiver/jmxreceiver/receiver.go +++ b/receiver/jmxreceiver/receiver.go @@ -36,6 +36,7 @@ type jmxMetricReceiver struct { otlpReceiver receiver.Metrics nextConsumer consumer.Metrics configFile string + cancel context.CancelFunc } func newJMXMetricReceiver( @@ -54,6 +55,8 @@ func newJMXMetricReceiver( func (jmx *jmxMetricReceiver) Start(ctx context.Context, host component.Host) error { jmx.logger.Debug("starting JMX Receiver") + ctx, jmx.cancel = context.WithCancel(ctx) + var err error jmx.otlpReceiver, err = jmx.buildOTLPReceiver() if err != nil { @@ -98,13 +101,19 @@ func (jmx *jmxMetricReceiver) Start(ctx context.Context, host component.Host) er return err } go func() { - for range jmx.subprocess.Stdout { // nolint - // ensure stdout/stderr buffer is read from. - // these messages are already debug logged when captured. + for { + select { + case <-ctx.Done(): + return + case <-jmx.subprocess.Stdout: + // ensure stdout/stderr buffer is read from. + // these messages are already debug logged when captured. + continue + } } }() - return jmx.subprocess.Start(context.Background()) + return jmx.subprocess.Start(ctx) } func (jmx *jmxMetricReceiver) Shutdown(ctx context.Context) error { @@ -114,6 +123,11 @@ func (jmx *jmxMetricReceiver) Shutdown(ctx context.Context) error { jmx.logger.Debug("Shutting down JMX Receiver") subprocessErr := jmx.subprocess.Shutdown(ctx) otlpErr := jmx.otlpReceiver.Shutdown(ctx) + + if jmx.cancel != nil { + jmx.cancel() + } + removeErr := os.Remove(jmx.configFile) if subprocessErr != nil { return subprocessErr