Skip to content

Conversation

denpeshkov
Copy link
Contributor

If Close() is called concurrently, both goroutines could pass the select’s default case before either of them executes close(ml.stopCh), resulting in the channel being closed twice

/kind bug

@k8s-ci-robot k8s-ci-robot added the kind/bug Categorizes issue or PR as related to a bug. label Aug 11, 2025
Copy link

linux-foundation-easycla bot commented Aug 11, 2025

CLA Signed

The committers listed above are authorized under a signed CLA.

  • ✅ login: denpeshkov / name: Denis Peshkov (14892a7)

@k8s-ci-robot k8s-ci-robot requested review from dims and thockin August 11, 2025 16:44
@k8s-ci-robot k8s-ci-robot added the cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. label Aug 11, 2025
@k8s-ci-robot
Copy link
Contributor

Welcome @denpeshkov!

It looks like this is your first PR to kubernetes/utils 🎉. 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/utils 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 k8s-ci-robot added size/S Denotes a PR that changes 10-29 lines, ignoring generated files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. and removed cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. labels Aug 11, 2025
Copy link
Member

@liggitt liggitt left a comment

Choose a reason for hiding this comment

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

go ahead and squash commits once the comment is addressed, then this lgtm

If Close() is called concurrently, both goroutines could pass the select’s default case before either of them executes close(), resulting in the channel being closed twice
@k8s-ci-robot k8s-ci-robot added size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. and removed size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Aug 14, 2025
@liggitt
Copy link
Member

liggitt commented Aug 14, 2025

lgtm, ack from network reviewer would be great as well (@aojea?)

@aojea
Copy link
Member

aojea commented Aug 20, 2025

/lgtm
/approve

Thanks , good one

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

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: aojea, denpeshkov

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 Aug 20, 2025
@k8s-ci-robot k8s-ci-robot merged commit 0af2bda into kubernetes:master Aug 20, 2025
2 checks passed
@ash2k
Copy link
Member

ash2k commented Aug 20, 2025

Wouldn't it be better to use a sync.Once to ensure all callers see the effects of a concurrent Close if Close returns for them?

i.e. a caller may rely on a listener actually being closed vs being in the process of being closed when Close returns. The current behavior does not match the doc since it might not wait for the goroutines to exit:

utils/net/multi_listen.go

Lines 151 to 152 in 0af2bda

// Close implements net.Listener. It will close all sub-listeners and wait for
// the go-routines to exit.

I can open a PR if the above makes sense.

@aojea aojea mentioned this pull request Aug 21, 2025
@aojea
Copy link
Member

aojea commented Aug 23, 2025

I do not completely follow, but the behavior that we have now IIUIC is:

  • You can only can call Close() once, the second time it returns an error
  • The first Close() call wait for the goroutines to exit

Is your point about the second call to Close() , if the fist Close() didn't finish yet, it should also wait?

@ash2k
Copy link
Member

ash2k commented Aug 23, 2025

Yes. Today, unless you know you are the first/only caller, you cannot rely on Close() to block until all goroutines finish. It might give you an error and then you have no way to wait.

@ash2k
Copy link
Member

ash2k commented Aug 23, 2025

Also, I'm not sure I understand why returning an error on double close is important but at the same time we discard all the actual errors from listeners/connections that we close.

@aojea
Copy link
Member

aojea commented Aug 26, 2025

Also, I'm not sure I understand why returning an error on double close is important but at the same time we discard all the actual errors from listeners/connections that we close.

I honestly can;t remember, and do not have much time these days to dig deeper, I assume that if a listener is close we do not care about bubbling up the error since it is already close and that is the intention.

The best next step should be to ensure we match the behavior of the existing standard listener , since the goal was to make a compatible one for dual stack use cases, see the golang original issue golang/go#9334 that is still open

@ash2k
Copy link
Member

ash2k commented Aug 27, 2025

Opened #331.

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. kind/bug Categorizes issue or PR as related to a bug. lgtm "Looks good to me", indicates that a PR is ready to be merged. size/XS Denotes a PR that changes 0-9 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants