Skip to content

Memory leak: fix the scheduler's goroutine leakage - #4233

Merged
jetstack-bot merged 3 commits into
cert-manager:masterfrom
maelvls:goroutine-leak
Jul 23, 2021
Merged

jetstack-bot merged 3 commits into
cert-manager:masterfrom
maelvls:goroutine-leak

Conversation

@maelvls

@maelvls maelvls commented Jul 20, 2021 •

Copy link
Copy Markdown
Member

A memory leak was identified in #4224. I found out that it was caused by our scheduler that was not stopping its timers. We probably thought that timer.Stop would close the timer.C channel, but it does not as per the documentation:

Stop turns off a ticker. After Stop, no more ticks will be sent. Stop does not close the channel, to prevent a concurrent goroutine reading from the channel from seeing an erroneous "tick".

This go playground example shows that timer.C is never closed.

I wrote a unit test to show the leakage:

% go test ./pkg/scheduler
--- FAIL: Test_afterFunc (0.22s)
    --- FAIL: Test_afterFunc/stop_works (0.12s)
        scheduler_test.go:56: 3 goroutines before, 10003 goroutines after
        scheduler_test.go:58: 
                Error Trace:    scheduler_test.go:58
                Error:          Max difference between 3 and 10003 allowed is 100, but difference was -10000
                Test:           Test_afterFunc/stop_works
FAIL
Fixed a goroutine leak that was causing the controller's memory usage to grow with time

/kind bug

Fixes #4224

@jetstack-bot

Copy link
Copy Markdown
Contributor

Skipping CI for Draft Pull Request.
If you want CI signal for your change, please convert it to an actual PR.
You can still manually trigger a test run with /test all

@jetstack-bot jetstack-bot added release-note Denotes a PR that will be considered when it comes time to generate release notes. kind/bug Categorizes issue or PR as related to a bug. do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. dco-signoff: yes Indicates that all commits in the pull request have the valid DCO sign-off message. approved Indicates a PR has been approved by an approver from all required OWNERS files. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Jul 20, 2021
@jetstack-bot jetstack-bot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Jul 20, 2021
@maelvls
maelvls marked this pull request as ready for review July 20, 2021 13:57
@jetstack-bot jetstack-bot added area/acme Indicates a PR directly modifies the ACME Issuer code and removed do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. labels Jul 20, 2021
@maelvls
maelvls force-pushed the goroutine-leak branch 2 times, most recently from 7ef79af to 0039f51 Compare July 20, 2021 17:29
@maelvls maelvls changed the title scheduler: add a unit test to show the goroutine leakage Fix the scheduler's goroutine leakage Jul 20, 2021
@maelvls maelvls changed the title Fix the scheduler's goroutine leakage Memory leak: fix the scheduler's goroutine leakage Jul 20, 2021
@irbekrm

irbekrm commented Jul 22, 2021

Copy link
Copy Markdown
Contributor

/assign

@irbekrm irbekrm left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Fantastic work on this, I learned more about go timers whilst reviewing :)

So if I understand correctly, the issue was that when the timer was stopped externally we did not actually exit the goroutine whereas now we will do it here.

I've read through the code and verified that the new unit test fails with the old version of the code- so the fix looks right.
I've added some nits, but generally happy to approve.

Comment thread pkg/scheduler/scheduler.go Resolved
Comment thread pkg/scheduler/scheduler.go Resolved
Comment thread pkg/controller/acmeorders/controller.go Resolved
Comment thread pkg/scheduler/scheduler.go Resolved
@jetstack-bot

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: irbekrm, maelvls

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@irbekrm

irbekrm commented Jul 23, 2021

Copy link
Copy Markdown
Contributor

Adding a hold in case you want to address the few comments, but also happy for you to unhold

/hold
/lgtm

@jetstack-bot jetstack-bot added do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. lgtm Indicates that a PR is ready to be merged. labels Jul 23, 2021
@jetstack-bot jetstack-bot removed the lgtm Indicates that a PR is ready to be merged. label Jul 23, 2021
@maelvls maelvls mentioned this pull request Jul 23, 2021
13 tasks done
@maelvls

maelvls commented Jul 23, 2021

Copy link
Copy Markdown
Member Author

/retest

@maelvls

maelvls commented Jul 23, 2021

Copy link
Copy Markdown
Member Author

So if I understand correctly, the issue was that when the timer was stopped externally we did not actually exit the goroutine whereas now we will do it here.

You are correct, Forget was not doing anything previously.

@irbekrm

irbekrm commented Jul 23, 2021

Copy link
Copy Markdown
Contributor

This looks great to me.

/lgtm
/hold cancel

@jetstack-bot jetstack-bot added lgtm Indicates that a PR is ready to be merged. and removed do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. labels Jul 23, 2021
@jetstack-bot
jetstack-bot merged commit 1021b58 into cert-manager:master Jul 23, 2021
@jetstack-bot jetstack-bot added this to the v1.5 milestone Jul 23, 2021
@jetstack-bot

Copy link
Copy Markdown
Contributor

@jakexks: new pull request created: #4270

Details

In response to this:

/cherry-pick release-1.4

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@maelvls
maelvls deleted the goroutine-leak branch July 28, 2021 14:19
maelvls added a commit to maelvls/cert-manager that referenced this pull request Jul 28, 2021
…tion"

This reverts commit 641960b. The reason
we decided to revert this is that we are unsure about the implications
of adding the scheduledWorkQueue.Forget call. The new Forget call is
left untested, and it makes us nervous not to know exactly if it works
as intended.

The "Forget" memory leak that we are reverting now is the cause of a
tiny faction of the overall memory leakage that was fixed in the PR
cert-manager#4233. The vast majority of
the memory leak came from the bug that was in the sheduler itself.
Reverting this means that some goroutines will be leaked, but only when
a Certificate gets removed and never recreated with the same name.

Signed-off-by: Maël Valais <[email protected]>
maelvls added a commit to maelvls/cert-manager that referenced this pull request Jul 28, 2021
This reverts commit 641960b. The reason
we decided to revert this is that we are unsure about the implications
of adding the scheduledWorkQueue.Forget call. The new Forget call is
left untested, and it makes us nervous not to know exactly if it works
as intended.

The "Forget" memory leak that we are reverting now is the cause of a
tiny faction of the overall memory leakage that was fixed in the PR
cert-manager#4233. The vast majority of
the memory leak came from the bug that was in the sheduler itself.
Reverting this means that some goroutines will be leaked, but only when
a Certificate gets removed and never recreated with the same name.

Signed-off-by: Maël Valais <[email protected]>
shashankvarma499 added a commit to shashankvarma499/cert-manager that referenced this pull request Sep 3, 2026
…rt-manager#9248)

The scheduledWorkQueue.afterFunc field was added for testing purposes in
cert-manager#4233, but Add never used it, so tests ran against real timers and
TestAdd raced on the mock clock state under -race.

Rather than wiring the dead hook up, delete it: with Go 1.26 the tests
can use testing/synctest, which drives the real afterFunc through
k8s.io/utils clock.RealClock deterministically. This removes the
unsynchronised mockAfter bookkeeping that cert-manager#9248 flagged, makes the
"fired early" check actually fail when Add mis-schedules, and catches
goroutine leaks without runtime.NumGoroutine counting.

Fixes cert-manager#9248

Signed-off-by: Shashank Varma <[email protected]>
cert-manager-prow Bot pushed a commit that referenced this pull request Sep 8, 2026
) (#9311)

The scheduledWorkQueue.afterFunc field was added for testing purposes in
#4233, but Add never used it, so tests ran against real timers and
TestAdd raced on the mock clock state under -race.

Rather than wiring the dead hook up, delete it: with Go 1.26 the tests
can use testing/synctest, which drives the real afterFunc through
k8s.io/utils clock.RealClock deterministically. This removes the
unsynchronised mockAfter bookkeeping that #9248 flagged, makes the
"fired early" check actually fail when Add mis-schedules, and catches
goroutine leaks without runtime.NumGoroutine counting.

Fixes #9248

Signed-off-by: Shashank Varma <[email protected]>
Co-authored-by: Shashank Varma <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved Indicates a PR has been approved by an approver from all required OWNERS files. area/acme Indicates a PR directly modifies the ACME Issuer code dco-signoff: yes Indicates that all commits in the pull request have the valid DCO sign-off message. kind/bug Categorizes issue or PR as related to a bug. lgtm Indicates that a PR is ready to be merged. release-note Denotes a PR that will be considered when it comes time to generate release notes. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

cert-manager controller increasing memory usage

3 participants