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

cmd/vet: Add check for channels that must be buffered #9399

Open
titanous opened this issue Dec 19, 2014 · 3 comments
Open

cmd/vet: Add check for channels that must be buffered #9399

titanous opened this issue Dec 19, 2014 · 3 comments
Assignees
Milestone

Comments

@titanous
Copy link
Member

@titanous titanous commented Dec 19, 2014

There are at least two places in the standard library where channels must have a non-zero buffer in order for the program to be correct:

  1. os.Signal channels passed to signal.Notify.
  2. *rpc.Call channels passed to Client.Go in net/rpc.

I propose a vet check that ensures that these channels are buffered.

I took a quick look at @rsc's pubgo dataset (circa early 2013), and in the code that compiles, I found 161 calls to make(chan os.Signal) of which 61 were unbuffered. I do not have a newer dataset handy (is there one?) but I think it's reasonable to guess that the ratio in newer code would not have changed significantly.

I currently have a vet check written that ensures that all calls to make(os.Signal) have a non-zero buffer, but this check seems a bit too simplistic, as there may be uses for these channels that do not require a buffer. I think it's safer to require buffered channels to be passed into the mentioned calls. However, I'm not familiar enough with the relevant packages to understand what the best approach for this is.

This was originally discussed on the mailing list.

@rsc rsc added this to the Unplanned milestone Apr 10, 2015
@rsc rsc changed the title cmd/vet: Add check for channels that must be buffered x/tools/cmd/vet: Add check for channels that must be buffered Apr 14, 2015
@rsc rsc modified the milestones: Unreleased, Unplanned Apr 14, 2015
@rsc rsc removed the repo-tools label Apr 14, 2015
@mvdan mvdan changed the title x/tools/cmd/vet: Add check for channels that must be buffered cmd/vet: Add check for channels that must be buffered May 31, 2018
@odeke-em odeke-em self-assigned this Jun 7, 2020
@gopherbot
Copy link

@gopherbot gopherbot commented Dec 1, 2020

Change https://golang.org/cl/274332 mentions this issue: cmd/go: fix unbuffered channel passed to signal.Notify

@gopherbot
Copy link

@gopherbot gopherbot commented Dec 1, 2020

Change https://golang.org/cl/274352 mentions this issue: go/analysis/passes: add sigchanyzer Analyzer

@gopherbot
Copy link

@gopherbot gopherbot commented Dec 1, 2020

Change https://golang.org/cl/274393 mentions this issue: doc: update signal.Notify example to use buffered channel

gopherbot pushed a commit that referenced this issue Dec 2, 2020
Unbuffered channels passed into signal.Notify can be lost
as the docs for signal.Notify caution with:

    Package signal will not block sending to c: the caller must ensure
    that c has sufficient buffer space to keep up with the expected signal
    rate. For a channel used for notification of just one signal value,
    a buffer of size 1 is sufficient.

Found by a static analyzer from Orijtech, Inc. called "sigchanyzer", but
it'll be donated to the Go project soon.

Updates #9399.

Change-Id: Ia0690e447582da028694ed65ace7b97961997b84
Reviewed-on: https://go-review.googlesource.com/c/go/+/274332
Trust: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Trust: Emmanuel Odeke <emmanuel@orijtech.com>
Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Reviewed-by: Bryan C. Mills <bcmills@google.com>
Reviewed-by: Emmanuel Odeke <emmanuel@orijtech.com>
TryBot-Result: Go Bot <gobot@golang.org>
gopherbot pushed a commit that referenced this issue Dec 2, 2020
This if follow up of CL 274332.

Updates #9399.

Change-Id: Ic6dd534dc18227a799cbb9577979f2285596b825
Reviewed-on: https://go-review.googlesource.com/c/go/+/274393
Trust: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
5 participants