kubernetes / kubectl

Issue tracker and mirror of kubectl code
Apache License 2.0
2.75k stars 894 forks source link

goleak in RunNoErrOutput #1566

Closed Zeel-Patel closed 3 months ago

Zeel-Patel commented 4 months ago

What is the issue?

I am trying to write UTs for a function which internally calls cli.RunNoErrOutput(command)

cli -> "k8s.io/component-base/cli"

But there seems to be goleak

goleak: Errors on successful test run: found unexpected goroutines:
[Goroutine 25 in state select, with k8s.io/klog/v2.(*flushDaemon).run.func1 on top of the stack:
k8s.io/klog/v2.(*flushDaemon).run.func1()
    external/io_k8s_klog_v2/klog.go:1157 +0xec
created by k8s.io/klog/v2.(*flushDaemon).run in goroutine 43
    external/io_k8s_klog_v2/klog.go:1153 +0x19c
]

What can be possible reason?

How to reproduce?

Define run method

func run(args []string) error {
    command := cmd.NewDefaultKubectlCommandWithArgs(cmd.KubectlOptions{
        PluginHandler: cmd.NewDefaultPluginHandler(plugin.ValidPluginFilenamePrefixes),
        Arguments:     args,
        IOStreams:     genericclioptions.IOStreams{In: os.Stdin, Out: os.Stdout, ErrOut: os.Stderr},
        ConfigFlags: genericclioptions.NewConfigFlags(true).
            WithWrapConfigFn(func(c *rest.Config) *rest.Config {

                return c
            }),
    })
    return cli.RunNoErrOutput(command)
}

Define test for run() in test file

func Test_run(t *testing.T) {

    tests := []struct {
        name    string
        args    []string
        wantErr bool
    }{
        {
            name:    "success",
            args:    []string{"kubectl", "get", "--help"},
            wantErr: false,
        },
    }
    for _, test := range tests {
        t.Run(test.name, func(t *testing.T) {
            err := run(test.args)
            if test.wantErr {
                require.NotNil(t, err)
            } else {
                require.Nil(t, err)
            }
        })
    }
}

Run test, test run will fail with error above

k8s-ci-robot commented 4 months ago

This issue is currently awaiting triage.

SIG CLI takes a lead on issue triage for this repo, but any Kubernetes member can accept issues by applying the triage/accepted label.

The triage/accepted label can be added by org members by writing /triage accepted in a comment.

Instructions for interacting with me using PR comments are available [here](https://git.k8s.io/community/contributors/guide/pull-requests.md). If you have questions or suggestions related to my behavior, please file an issue against the [kubernetes/test-infra](https://github.com/kubernetes/test-infra/issues/new?title=Prow%20issue:) repository.
Zeel-Patel commented 4 months ago

/sig cli

mpuckett159 commented 4 months ago

/needs-information Please provide a method of reproduction so we can verify this and investigate further.

mpuckett159 commented 4 months ago

/triage needs-information

Zeel-Patel commented 4 months ago

@mpuckett159 , edited the question with info.

mpuckett159 commented 3 months ago

/close Closing the issue, this seems to be related to klog according to the output in the error trace (if I'm wrong please correct me) which is not something that SIG-CLI owns, this would be better suited in the kubernetes/klog repo, but as I also understand this is a fork of golang/glog, so this may be something to fix even farther upstream in that repo, I'm unsure. If the fix truly needs to happen in our code, we would accept a PR to fix it.

k8s-ci-robot commented 3 months ago

@mpuckett159: Closing this issue.

In response to [this](https://github.com/kubernetes/kubectl/issues/1566#issuecomment-2024103773): >/close >Closing the issue, this seems to be related to klog according to the output in the error trace (if I'm wrong please correct me) which is not something that SIG-CLI owns, this would be better suited in the kubernetes/klog repo, but as I also understand this is a fork of golang/glog, so this may be something to fix even farther upstream in that repo, I'm unsure. If the fix truly needs to happen in our code, we would accept a PR to fix it. Instructions for interacting with me using PR comments are available [here](https://git.k8s.io/community/contributors/guide/pull-requests.md). If you have questions or suggestions related to my behavior, please file an issue against the [kubernetes/test-infra](https://github.com/kubernetes/test-infra/issues/new?title=Prow%20issue:) repository.