Skip to content

Commit

Permalink
[receiver/googlecloudpubsubreceiver] Fix memory leak during shutdown (#…
Browse files Browse the repository at this point in the history
…32361)

**Description:** <Describe what has changed.>
<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue.
Ex. Adding a feature - Explain what this achieves.-->
This PR contains the following changes:
1. Add `Close` call to the receiver's GRPC client. Without this,
goroutines were being leaked on shutdown.
2. Change `grpc.Dial` -> `grpc.NewClient`. They offer the same
functionality, but `Dial` is being deprecated in favor of `NewClient`.
3. Enable `goleak` checks on this receiver to help ensure no goroutines
are being leaked.
4. Change a couple `Assert.Nil` calls to `Assert.NoError`. The output of
`NoError` includes the error message if hit, `Nil` simply includes the
object's address, i.e. `&status.Error{s:(*status.Status)(0xc00007e158)}`

**Link to tracking Issue:** <Issue number if applicable>
#30438

**Testing:** <Describe what testing was performed and which tests were
added.>
All existing tests are passing, as well as added goleak check.
  • Loading branch information
crobert-1 committed May 8, 2024
1 parent bbcbe3c commit 097c745
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 7 deletions.
27 changes: 27 additions & 0 deletions .chloggen/goleak_googlepubsubrec.yaml
Original file line number Diff line number Diff line change
@@ -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: googlecloudpubsubreceiver

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Fix memory leak during shutdown

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [32361]

# (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: []
4 changes: 3 additions & 1 deletion receiver/googlecloudpubsubreceiver/generated_package_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion receiver/googlecloudpubsubreceiver/metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,8 @@ tests:
skip_lifecycle: true
skip_shutdown: true
goleak:
skip: true
skip: false
ignore:
# See https://github.com/census-instrumentation/opencensus-go/issues/1191 for more information.
top: go.opencensus.io/stats/view.(*worker).start

16 changes: 13 additions & 3 deletions receiver/googlecloudpubsubreceiver/receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ import (
"go.uber.org/zap"
"google.golang.org/api/option"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/status"

"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/googlecloudpubsubreceiver/internal"
)
Expand Down Expand Up @@ -76,7 +78,7 @@ func (receiver *pubsubReceiver) generateClientOptions() (copts []option.ClientOp
if receiver.userAgent != "" {
dialOpts = append(dialOpts, grpc.WithUserAgent(receiver.userAgent))
}
conn, _ := grpc.Dial(receiver.config.Endpoint, append(dialOpts, grpc.WithTransportCredentials(insecure.NewCredentials()))...)
conn, _ := grpc.NewClient(receiver.config.Endpoint, append(dialOpts, grpc.WithTransportCredentials(insecure.NewCredentials()))...)
copts = append(copts, option.WithGRPCConn(conn))
} else {
copts = append(copts, option.WithEndpoint(receiver.config.Endpoint))
Expand Down Expand Up @@ -113,13 +115,21 @@ func (receiver *pubsubReceiver) Start(ctx context.Context, _ component.Host) err
}

func (receiver *pubsubReceiver) Shutdown(_ context.Context) error {
var err error
if receiver.client != nil {
// A canceled code means the client connection is already closed,
// Shutdown shouldn't return an error in that case.
if closeErr := receiver.client.Close(); status.Code(closeErr) != codes.Canceled {
err = closeErr
}
}
if receiver.handler == nil {
return nil
return err
}
receiver.logger.Info("Stopping Google Pubsub receiver")
receiver.handler.CancelNow()
receiver.logger.Info("Stopped Google Pubsub receiver")
return nil
return err
}

func (receiver *pubsubReceiver) handleLogStrings(ctx context.Context, message *pubsubpb.ReceivedMessage) error {
Expand Down
4 changes: 2 additions & 2 deletions receiver/googlecloudpubsubreceiver/receiver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,6 @@ func TestReceiver(t *testing.T) {
return len(logSink.AllLogs()) == 1
}, time.Second, 10*time.Millisecond)

assert.Nil(t, receiver.Shutdown(ctx))
assert.Nil(t, receiver.Shutdown(ctx))
assert.NoError(t, receiver.Shutdown(ctx))
assert.NoError(t, receiver.Shutdown(ctx))
}

0 comments on commit 097c745

Please sign in to comment.