go: sync: inconsistent mutex state

My code face “sync: inconsistent mutex state” panic, and I’m trying to fix it.

https://github.com/golang/go/blob/d54a9a9c42e751a020308cae296add426b56d0f0/src/sync/mutex.go#L147

I wonder should this line be old = atomic.LoadInt32(&m.state)?

https://github.com/golang/go/blob/d54a9a9c42e751a020308cae296add426b56d0f0/src/sync/mutex.go#L140

It seems like there will be concurrent write in line: 212, and this line cannot guarantee old value is correct.

I found https://groups.google.com/g/golang-nuts/c/Eq4CWTB6LhM/m/F0Lbgnfq4hQJ this post said it’s a bug, but 7 years past, it’s still there, any purpose? Or is it safe to read int value from a pointer’s field?

sorry for my poor english

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Comments: 21 (8 by maintainers)

Most upvoted comments

This looks like memory corruption. Have you tried running your program under the race detector? See https://blog.golang.org/race-detector .