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

proposal: sync: Go 2: rename sync.RWMutex's Unlock() to WUnlock() #27936

Closed
brendan-rius opened this issue Sep 29, 2018 · 4 comments
Closed

proposal: sync: Go 2: rename sync.RWMutex's Unlock() to WUnlock() #27936

brendan-rius opened this issue Sep 29, 2018 · 4 comments
Labels
FrozenDueToAge Proposal v2 A language change or incompatible library change
Milestone

Comments

@brendan-rius
Copy link

What version of Go are you using (go version)?

1.10.2

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

Not relevant

What did you do?

When writing a code such as

x.RLock()
defer x.RUnlock()

it is tempting to write defer x.Unlock() instead of defer x.RUnlock(), which will cause an error if the mutex was not write-locked.

I suggest to not only be explicit when read-(un)locking but also when write-(un)locking by renaming Lock() to WLock() and Unlock() to WUnlock(). That way, it will be really easy to see if there is a potential bug because the code will look like:

x.RLock()
defer x.WUnlock()

which is way more explicit. If this code is involuntary, then ther'es a high chance the dev will spot the bug easily, and if it was meant to be like that (which I suppose is not common at all), then it's way better for it to be explicit (because not common)

I am writing this issue because I saw this bug happens 2 times in production over the course of the week and it feels like a really simple, common-sense fix to do.

@adamdecaf
Copy link
Contributor

What about static analysis checks? i.e. megacheck or vet? There's still the potential for programmer error no matter what the names are.

This is probably a Go 2 proposal since we can't remove the existing methods.

@bcmills bcmills added the v2 A language change or incompatible library change label Sep 30, 2018
@ALTree ALTree changed the title Rename sync.RWMutex's Unlock() to WUnlock() sync: rename sync.RWMutex's Unlock() to WUnlock() Oct 1, 2018
@ianlancetaylor ianlancetaylor changed the title sync: rename sync.RWMutex's Unlock() to WUnlock() sync: Go 2: rename sync.RWMutex's Unlock() to WUnlock() Oct 1, 2018
@ianlancetaylor ianlancetaylor added this to the Proposal milestone Oct 1, 2018
@ianlancetaylor ianlancetaylor changed the title sync: Go 2: rename sync.RWMutex's Unlock() to WUnlock() proposal: sync: Go 2: rename sync.RWMutex's Unlock() to WUnlock() Oct 1, 2018
@deanveloper
Copy link

I think instead this should be handled by tools such as go vet. Currently, go vet does not handle this:

https://play.golang.org/p/4dIi5FjIbeB

Notice that there is no go vet output in the above example.

Playing around with the above example, there is also no go vet output for a mutex that's never unlocked, or a mutex that is unlocked, but never locked. Perhaps these cases should be added?

@bcmills
Copy link
Contributor

bcmills commented Oct 2, 2018

Note that *sync.RWMutex currently implements the sync.Locker interface. How does that change under your proposal?

@ianlancetaylor
Copy link
Contributor

This isn't a change we're going to make at this point.

@golang golang locked and limited conversation to collaborators Oct 23, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge Proposal v2 A language change or incompatible library change
Projects
None yet
Development

No branches or pull requests

6 participants