var mu sync.RWMutex
go func() {
mu.RLock()
defer mu.RUnlock()
mu.RLock() // In my real scenario this second lock happened in a nested function.
defer mu.RUnlock()
// More code.
}()
mu.Lock()
mu.Unlock() // The goroutine above still hangs.
If a function read-locks a read/write mutex twice, while another function write-locks and then write-unlocks that same mutex, the original function still hangs.
Why is that? Is it because there's a serial order in which mutexes allow code to execute?
I've just solved a scenario like this (which took me hours to pinpoint) by removing the second mu.RLock()
line.