sync.RWMutex behaviour I don't understand.

194 views
Skip to first unread message

Ignacio Grande

unread,
Apr 8, 2015, 5:08:28 AM4/8/15
to golan...@googlegroups.com
Hello.

I have this small program that shows a weird behaviour while using RWMutex: http://play.golang.org/p/bhrY_W5F1z

From the documentation, I expected that the Unlock operation in the goroutine would cause a runtime error (because it is RLocked, not Locked), but somehow it is Unlocked. In addition, I can RLock it even if there is a pending Lock operation that is not completed (it should be blocked until the Lock is acquired and released).

Is this a bug or am I missing something? What is the status of the lock while the Tick is working?

Regards.

Jan Mercl

unread,
Apr 8, 2015, 5:20:40 AM4/8/15
to golan...@googlegroups.com
On Wed, Apr 8, 2015 at 11:08 AM Ignacio Grande <ncc17...@gmail.com> wrote:

> I have this small program that shows a weird behaviour while using 

Ignacio Grande

unread,
Apr 8, 2015, 5:30:51 AM4/8/15
to golan...@googlegroups.com
I don't understand what it is WAI... why Unlock works if the lock is RLocked and never RUnlocked? It is never Locked, either ("Locking..." is printed but not "Locked", that is expected because the lock is still RLocked).

Ignacio Grande

unread,
Apr 8, 2015, 5:31:20 AM4/8/15
to golan...@googlegroups.com
*why* it is WAI, sorry.

Dmitry Vyukov

unread,
Apr 8, 2015, 5:31:43 AM4/8/15
to Ignacio Grande, golang-nuts
On Wed, Apr 8, 2015 at 12:08 PM, Ignacio Grande <ncc17...@gmail.com> wrote:
> Hello.
>
> I have this small program that shows a weird behaviour while using RWMutex:
> http://play.golang.org/p/bhrY_W5F1z
>
> From the documentation, I expected that the Unlock operation in the
> goroutine would cause a runtime error (because it is RLocked, not Locked),
> but somehow it is Unlocked.

This misuse is detected only probabilistically. If you runt he
following program with GOMAXPROCS>1 it crashes eventually with "panic:
sync: Unlock of unlocked RWMutex":
http://play.golang.org/p/kgW1mmY-la
Non-probabilistic detection will slowdown correct programs.


> In addition, I can RLock it even if there is a
> pending Lock operation that is not completed (it should be blocked until the
> Lock is acquired and released).

I don't understand this part. The program misuses mutex, so it's
behavior is all broken.
Can you provide a correct program that demonstrates the problem?

> Is this a bug or am I missing something? What is the status of the lock
> while the Tick is working?
>
> Regards.
>
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Jan Mercl

unread,
Apr 8, 2015, 5:41:32 AM4/8/15
to golan...@googlegroups.com
On Wed, Apr 8, 2015 at 11:31 AM Ignacio Grande <ncc17...@gmail.com> wrote:

*why* it is WAI, sorry.

I am sorry. I somehow managed to miss completely the first line of output of the program. Except for that line, it would be WAI. Mea culpa.

-j

Ignacio Grande

unread,
Apr 8, 2015, 5:44:07 AM4/8/15
to golan...@googlegroups.com, ncc17...@gmail.com
Hi.


On Wednesday, April 8, 2015 at 11:31:43 AM UTC+2, Dmitry Vyukov wrote:

This misuse is detected only probabilistically. If you runt he
following program with GOMAXPROCS>1 it crashes eventually with "panic:
sync: Unlock of unlocked RWMutex":
http://play.golang.org/p/kgW1mmY-la
Non-probabilistic
detection will slowdown correct programs.

The program misuses mutex, so it's
behavior is all broken.
Can you provide a correct program that demonstrates the problem?

Sorry I wasn't clear enough. What puzzled me is that the misuse went undetected. Of course, I didn't expect a incorrect program to work fine, but I didn't know why the misuse was undetected (I was expecting a runtime-error as the documentation says). You clarified this and I understand the trade-off. I wonder if this could happen in a (inadvertently incorrect) real-world program.

Regards.



Dmitry Vyukov

unread,
Apr 8, 2015, 5:50:15 AM4/8/15
to Ignacio Grande, golang-nuts
The misuse will detected in real program sooner or later. And the
program probably will deadlock every time, so the bug won't go
unnoticed.
Reply all
Reply to author
Forward
0 new messages