diff --git a/.chloggen/goleak_azureeventhubrec_fix.yaml b/.chloggen/goleak_azureeventhubrec_fix.yaml new file mode 100644 index 000000000000..85b46c7fb90a --- /dev/null +++ b/.chloggen/goleak_azureeventhubrec_fix.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: azureeventhubreceiver + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: Fix memory leak on shutdown + +# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. +issues: [32401] + +# (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/azureeventhubreceiver/eventhubhandler.go b/receiver/azureeventhubreceiver/eventhubhandler.go index bdfc3a521826..350829f33ad8 100644 --- a/receiver/azureeventhubreceiver/eventhubhandler.go +++ b/receiver/azureeventhubreceiver/eventhubhandler.go @@ -53,12 +53,14 @@ type eventhubHandler struct { dataConsumer dataConsumer config *Config settings receiver.CreateSettings + cancel context.CancelFunc } // Implement eventHandler Interface var _ eventHandler = (*eventhubHandler)(nil) func (h *eventhubHandler) run(ctx context.Context, host component.Host) error { + ctx, h.cancel = context.WithCancel(ctx) storageClient, err := adapter.GetStorageClient(ctx, host, h.config.StorageID, h.settings.ID) if err != nil { @@ -177,6 +179,10 @@ func (h *eventhubHandler) close(ctx context.Context) error { } h.hub = nil } + if h.cancel != nil { + h.cancel() + } + return nil } diff --git a/receiver/azureeventhubreceiver/eventhubhandler_test.go b/receiver/azureeventhubreceiver/eventhubhandler_test.go index 27bbcbe4ed43..5508c1f9bb2c 100644 --- a/receiver/azureeventhubreceiver/eventhubhandler_test.go +++ b/receiver/azureeventhubreceiver/eventhubhandler_test.go @@ -84,9 +84,6 @@ func (m *mockDataConsumer) consume(ctx context.Context, event *eventhub.Event) e } func TestEventhubHandler_Start(t *testing.T) { - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() - config := createDefaultConfig() config.(*Config).Connection = "Endpoint=sb://namespace.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=superSecret1234=;EntityPath=hubName" @@ -97,17 +94,11 @@ func TestEventhubHandler_Start(t *testing.T) { } ehHandler.hub = &mockHubWrapper{} - err := ehHandler.run(ctx, componenttest.NewNopHost()) - assert.NoError(t, err) - - err = ehHandler.close(ctx) - assert.NoError(t, err) + assert.NoError(t, ehHandler.run(context.Background(), componenttest.NewNopHost())) + assert.NoError(t, ehHandler.close(context.Background())) } func TestEventhubHandler_newMessageHandler(t *testing.T) { - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() - config := createDefaultConfig() config.(*Config).Connection = "Endpoint=sb://namespace.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=superSecret1234=;EntityPath=hubName" @@ -131,11 +122,10 @@ func TestEventhubHandler_newMessageHandler(t *testing.T) { } ehHandler.hub = &mockHubWrapper{} - err = ehHandler.run(ctx, componenttest.NewNopHost()) - assert.NoError(t, err) + assert.NoError(t, ehHandler.run(context.Background(), componenttest.NewNopHost())) now := time.Now() - err = ehHandler.newMessageHandler(ctx, &eventhub.Event{ + err = ehHandler.newMessageHandler(context.Background(), &eventhub.Event{ Data: []byte("hello"), PartitionKey: nil, Properties: map[string]any{"foo": "bar"}, @@ -158,4 +148,5 @@ func TestEventhubHandler_newMessageHandler(t *testing.T) { read, ok := sink.AllLogs()[0].ResourceLogs().At(0).ScopeLogs().At(0).LogRecords().At(0).Attributes().Get("foo") assert.True(t, ok) assert.Equal(t, "bar", read.AsString()) + assert.NoError(t, ehHandler.close(context.Background())) }