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: goroutine leak #5364

Merged
merged 1 commit into from Nov 10, 2023
Merged

fix: goroutine leak #5364

merged 1 commit into from Nov 10, 2023

Conversation

0xff-dev
Copy link
Contributor

@0xff-dev 0xff-dev commented Oct 8, 2023

fix: goroutine leak

@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Oct 8, 2023
@k8s-ci-robot
Copy link
Contributor

Welcome @0xff-dev!

It looks like this is your first PR to kubernetes-sigs/kustomize 🎉. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation.

You can also check if kubernetes-sigs/kustomize has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs!

Thank you, and welcome to Kubernetes. 😃

@k8s-ci-robot
Copy link
Contributor

Hi @0xff-dev. Thanks for your PR.

I'm waiting for a kubernetes-sigs member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

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.

@k8s-ci-robot k8s-ci-robot added needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels Oct 8, 2023
@charles-chenzz
Copy link
Member

/ok-to-test

@k8s-ci-robot k8s-ci-robot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Oct 9, 2023
@koba1t
Copy link
Member

koba1t commented Oct 14, 2023

@0xff-dev

Thanks for your contribution!

Please add any tests that check this bug fixed.
And it looks like any tests failed on CI, Could you check and fix it?

@k8s-ci-robot k8s-ci-robot added size/S Denotes a PR that changes 10-29 lines, ignoring generated files. and removed size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels Oct 15, 2023
@0xff-dev
Copy link
Contributor Author

@0xff-dev

Thanks for your contribution!

Please add any tests that check this bug fixed. And it looks like any tests failed on CI, Could you check and fix it?

Hi @koba1t , The code has been updated. During my local testing, I encountered a failure in the last test when utilizing an unbuffered channel.

@0xff-dev
Copy link
Contributor Author

@koba1t Based on the execution results of the two change actions, it appears that the Test Linux test may be a flaky test.
https://github.com/kubernetes-sigs/kustomize/actions/runs/6522412115
https://github.com/kubernetes-sigs/kustomize/actions/runs/6446341552

_ = TimedCall("expect no goroutine leaks", time.Second, func() error {
time.Sleep(2 * time.Second)
return fmt.Errorf("not done")
})
Copy link
Member

Choose a reason for hiding this comment

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

could you be more specify on what has not done? it would help a lot. Thanks

Copy link
Contributor Author

@0xff-dev 0xff-dev Oct 16, 2023

Choose a reason for hiding this comment

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

Sorry for the misrepresentation here.
not done I would say would cause the goroutine to fail to exit and not finish.
Would it be better to change it to return fmt.Error("function done")? Or just return nil

Copy link
Member

Choose a reason for hiding this comment

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

I‘m not buying just return nil, I think something like goroutine to fail to exit and not finish or function done is fine

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the suggestion, the code has been changed to return fmt.Errorf("function done")

Copy link
Member

@stormqueen1990 stormqueen1990 left a comment

Choose a reason for hiding this comment

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

Just a few small suggestions

Comment on lines 74 to 77
if beforeGroutine != afterGoroutine {
t.Fatal("goroutine leak")
}
Copy link
Member

Choose a reason for hiding this comment

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

Small suggestion:

Suggested change
if beforeGroutine != afterGoroutine {
t.Fatal("goroutine leak")
}
require.NotEqual(t, beforeGoroutine, afterGoroutine, "goroutine leak")

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi @stormqueen1990 , I think it should be require.Equal here. Because we modified the unbuffered channel to be buffered, it should be equal when the test is done here.

Copy link
Member

Choose a reason for hiding this comment

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

Yes! I forgot to invert the test when I typed, sorry about that 😅

@@ -62,3 +63,15 @@ func TestTimedCallSlowWithError(t *testing.T) {
t.Fail()
}
}

func TestTimedCallGoroutineLeak(t *testing.T) {
beforeGroutine := runtime.NumGoroutine()
Copy link
Member

Choose a reason for hiding this comment

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

Small typo in variable name:

Suggested change
beforeGroutine := runtime.NumGoroutine()
beforeGoroutine := runtime.NumGoroutine()

Copy link
Member

@charles-chenzz charles-chenzz left a comment

Choose a reason for hiding this comment

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

just one minor comment


func TestTimedCallGoroutineLeak(t *testing.T) {
beforeGroutine := runtime.NumGoroutine()
_ = TimedCall("expect no goroutine leaks", time.Second, func() error {
Copy link
Member

Choose a reason for hiding this comment

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

why we don't catch the error here? are you ignore for some reasons?
image

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think the function has no special complex logic. So ignore the error.
I just looked at the other test examples and none of them ignored the error, so i need to change it, thanks for the review.

@0xff-dev
Copy link
Contributor Author

0xff-dev commented Nov 6, 2023

@charles-chenzz @stormqueen1990 @koba1t I want to use go.uber.org/goleak to check for goroutine leaks, there may be some problems with the method I wrote before, do you think it's ok?

@charles-chenzz
Copy link
Member

@charles-chenzz @stormqueen1990 @koba1t I want to use go.uber.org/goleak to check for goroutine leaks, there may be some problems with the method I wrote before, do you think it's ok?

you can push up an quick example to demonstrate it

@@ -7,6 +7,7 @@ require (
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
github.com/imdario/mergo v0.3.13
github.com/stretchr/testify v1.8.1
go.uber.org/goleak v1.3.0
Copy link
Contributor

Choose a reason for hiding this comment

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

Adding a note here for other reviewers: I double checked if this dependency is OK by checking the allowlist here . MIT licenses are allowed, which looks like this repo is covered under, so this should be OK.

Copy link
Member

Choose a reason for hiding this comment

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

I also check k/k, most of the deps on it are MIT and apache2. so I think goleak from uber which MIT license as well will be OK.

Copy link
Member

@charles-chenzz charles-chenzz left a comment

Choose a reason for hiding this comment

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

/lgtm
/assign @natasha41575 @koba1t

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Nov 9, 2023
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: 0xff-dev, natasha41575

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

The pull request process is described here

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

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Nov 10, 2023
@k8s-ci-robot k8s-ci-robot merged commit 863ca93 into kubernetes-sigs:master Nov 10, 2023
9 checks passed
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. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. size/S Denotes a PR that changes 10-29 lines, ignoring generated files.
Development

Successfully merging this pull request may close these issues.

None yet

6 participants