Why will it deadlock if a goroutine acquire a mutex while pinned to its P?

153 views
Skip to first unread message

Cholerae Hu

unread,
Apr 8, 2019, 2:31:07 AM4/8/19
to golang-nuts
I'm reading this commit https://github.com/golang/go/commit/d5fd2dd6a17a816b7dfd99d4df70a85f1bf0de31 .  Inside runtime_procPin we only increases the m.lock count, why will it cause deadlock when acquiring a mutex after pin?

Ian Lance Taylor

unread,
Apr 8, 2019, 2:25:06 PM4/8/19
to Cholerae Hu, Austin Clements, golang-nuts
[ + austin ]

On Sun, Apr 7, 2019 at 11:31 PM Cholerae Hu <chole...@gmail.com> wrote:
>
> I'm reading this commit https://github.com/golang/go/commit/d5fd2dd6a17a816b7dfd99d4df70a85f1bf0de31 . Inside runtime_procPin we only increases the m.lock count, why will it cause deadlock when acquiring a mutex after pin?
>
> --
> 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.

Robert Engels

unread,
Apr 8, 2019, 5:34:22 PM4/8/19
to Ian Lance Taylor, Cholerae Hu, Austin Clements, golang-nuts
Apologies not warranted. I was only pointing out that it didn't seem correct to me.

Austin Clements

unread,
Apr 9, 2019, 10:59:37 AM4/9/19
to Cholerae Hu, golang-nuts
Acquiring a mutex while pinned can cause deadlock because pinning prevents a stop-the-world. For example, the following sequence could result in a deadlock:

M1: Acquires mutex l.
M2: Pins the M.
M2: Attempts to acquire mutex l.
M3: Initiates stop-the-world
M3: Stops M1
M3: Attempts to stop M2, but can't because M2 is pinned.

At this point, M1 can't make progress to release mutex l because M3 stopped it, which means M2 won't be able to finish acquiring the mutex (so it will never release the pin), which means M3 won't be able to finish stopping the world (so it will never start M1 back up).

On Mon, Apr 8, 2019 at 2:31 AM Cholerae Hu <chole...@gmail.com> wrote:
I'm reading this commit https://github.com/golang/go/commit/d5fd2dd6a17a816b7dfd99d4df70a85f1bf0de31 .  Inside runtime_procPin we only increases the m.lock count, why will it cause deadlock when acquiring a mutex after pin?

--

Cholerae Hu

unread,
Apr 10, 2019, 3:42:10 AM4/10/19
to golang-nuts
Thanks austin! I totally understand.

在 2019年4月9日星期二 UTC+8下午10:59:37,Austin Clements写道:
To unsubscribe from this group and stop receiving emails from it, send an email to golan...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages