You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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()
deferx.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()
deferx.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.
The text was updated successfully, but these errors were encountered:
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
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?
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
it is tempting to write
defer x.Unlock()
instead ofdefer 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()
toWLock()
andUnlock()
toWUnlock()
. That way, it will be really easy to see if there is a potential bug because the code will look like: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.
The text was updated successfully, but these errors were encountered: