sasha-s / go-deadlock

Online deadlock detection in go (golang)
Apache License 2.0
1.03k stars 76 forks source link

Single thread lock in different order reported as deadlock #24

Closed yiminc closed 2 years ago

yiminc commented 2 years ago

There is only one thread, the order should not matter.

package main

import (
    sync "github.com/sasha-s/go-deadlock"
)

func main() {
    m := make(map[int]*sync.Mutex)
    for i := 0; i < 8; i++ {
        m[i] = &sync.Mutex{}
    }
    useLock(m)
    useLock(m)
}

func useLock(locks map[int]*sync.Mutex) {
    for _, lock := range locks {
        lock.Lock()
        defer lock.Unlock()
    }
    // do something that require all locks
}
sasha-s commented 2 years ago

I think this is working as expected.

Iteration order for maps is random. So, even though the example does not deadlock, it is dangerous: If you run useLock(m) in different goroutines, it can deadlock.

sasha-s commented 2 years ago

Closing (working as intended). Please re-open if needed.