Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[internal/common/ttlmap] Fix leak caused by time.Tick #32044

Merged
merged 6 commits into from
Apr 25, 2024

Conversation

crobert-1
Copy link
Member

Description:

Documentation shows the existing usage of time.Tick will leak a goroutine on shutdown. This updates the internal/common/ttlmap tick functionality to use a ticker with a context that has a cancel function. This allows the ticker to be properly shut down.

I don't believe this requires a changelog because no user-facing components were updated to actually call the shutdown function. It makes more sense to me to add those after this gets merged to keep this PR more concise.

This also adds goleak to the package to help ensure goroutines aren't being leaked.

Link to tracking Issue:
Related to #30438

Testing:
Existing tests are still passing, goleak check was failing before change, succeeds after.

Copy link
Contributor

@MovieStoreGuy MovieStoreGuy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor bit of feedback.

internal/common/ttlmap/ttl_map.go Outdated Show resolved Hide resolved
}

// New creates a TTLMap. The sweepIntervalSeconds arg indicates how often
// entries are checked for expiration. The maxAgeSeconds arg indicates how long
// entries can persist before getting evicted. Call Start() on the returned
// TTLMap to begin periodic sweeps which check for expiration and evict entries
// as needed.
func New(sweepIntervalSeconds int64, maxAgeSeconds int64) *TTLMap {
// done is the channel that will be used to signal to the timer to stop its work.
func New(sweepIntervalSeconds int64, maxAgeSeconds int64, done chan struct{}) *TTLMap {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apologies for the size of this PR now. I originally wanted to simply create the channel inside of New, but an existing test exporter/signalfxexporter/TestConfigGetMetricTranslator does an Assert.Equal on objects that contain the TTLMap as a private member. The assertion fails as the pointers are not equal. I've attempted a few workarounds (cmp and cmpopts, yaml or JSON marshalling). Nothing works.

If the preference is to simply not have a done channel and use the timer and the timer.Stop() method instead, I can modify this PR to pass that in. Either way, the signature of New will need to be updated to accomodate.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An alternative that may be less impacting would be to pass the done channel to Start. I don't think passing in the Ticker would work as it gets started at creation. I don't think it would be a great design to pass a running Ticker to these methods.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the TTLMap struct might also create its own done channel. Wouldn't that be best?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would be best, but the usage and testing is where the challenge comes in. The TestConfigGetMetricTranslator is testing to ensure the config is created properly relative to the metric translator itself in the signalfx exporter. When the TTLMap creates its own channel the equality comparison fails.

The other options are remove the test, or add a public equality checking method to the metrics translator.

@crobert-1 crobert-1 marked this pull request as ready for review April 4, 2024 17:42
@crobert-1 crobert-1 added the Skip Changelog PRs that do not require a CHANGELOG.md entry label Apr 4, 2024
@evan-bradley
Copy link
Contributor

@crobert-1 Sorry for the delay, could you remove the Dynatrace exporter changes? It has been removed from the repo.

@crobert-1
Copy link
Member Author

@crobert-1 Sorry for the delay, could you remove the Dynatrace exporter changes? It has been removed from the repo.

Not a problem, thanks for pointing this out! I've removed references 👍

@evan-bradley evan-bradley merged commit 6167c35 into open-telemetry:main Apr 25, 2024
171 checks passed
@github-actions github-actions bot added this to the next release milestone Apr 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
exporter/signalfx Skip Changelog PRs that do not require a CHANGELOG.md entry
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants