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

Fix memory leaks caused by time.After #914

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

biningo
Copy link

@biningo biningo commented Sep 3, 2024

Fixes #913

Copy link
Contributor

Choose a reason for hiding this comment

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

Only starting with Go 1.23 unreferenced and unstopped time.Ticker will be garbage collected.

Since the min. Go version of this project is Go 1.20, you should call defer ticker.Stop(). Otherwise, this will leak the ticker.

In both cases, the changed code does not behave the same as the original one: time.Ticker publishes in a defined Interval $I$. You have to reset the ticker every time you start the next "cycle. "Otherwise, the timeout you're waiting for may be shorter than expected.

Copy link
Author

Choose a reason for hiding this comment

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

Thank you for your suggestions! : )
I'll fix it!

About Go version
As you said, using time.After in Go 1.23 and later versions does not cause memory leaks, but this project is using Go 1.20.

About ticker or timer
The ticker sends values to the channel at a fixed rate. If the task execution time is longer than the ticker interval, the next task will start immediately after the previous task finished.
But the timer can be reset after the task has finished. No matter how long the previous task is running, the next task will start after the interval.

@huangc28
Copy link

huangc28 commented Oct 2, 2024

Why haven't we merge this PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG] Memory leaks caused by time.After
3 participants