lib/sync: Print all lockers, add holder to RWMutex

This commit is contained in:
Audrius Butkevicius 2016-10-30 00:14:38 +01:00
parent 0296c23685
commit 7fba8cf759
3 changed files with 28 additions and 8 deletions

View File

@ -172,8 +172,9 @@ func NewModel(cfg *config.Wrapper, id protocol.DeviceID, deviceName, clientName,
// period. // period.
func (m *Model) StartDeadlockDetector(timeout time.Duration) { func (m *Model) StartDeadlockDetector(timeout time.Duration) {
l.Infof("Starting deadlock detector with %v timeout", timeout) l.Infof("Starting deadlock detector with %v timeout", timeout)
deadlockDetect(m.fmut, timeout, "fmut") detector := newDeadlockDetector(timeout)
deadlockDetect(m.pmut, timeout, "pmut") detector.Watch("fmut", m.fmut)
detector.Watch("pmut", m.pmut)
} }
// StartFolder constructs the folder service and starts it. // StartFolder constructs the folder service and starts it.

View File

@ -16,10 +16,23 @@ type Holder interface {
Holder() (string, int) Holder() (string, int)
} }
func deadlockDetect(mut sync.Locker, timeout time.Duration, name string) { func newDeadlockDetector(timeout time.Duration) *deadlockDetector {
return &deadlockDetector{
timeout: timeout,
lockers: make(map[string]sync.Locker),
}
}
type deadlockDetector struct {
timeout time.Duration
lockers map[string]sync.Locker
}
func (d *deadlockDetector) Watch(name string, mut sync.Locker) {
d.lockers[name] = mut
go func() { go func() {
for { for {
time.Sleep(timeout / 4) time.Sleep(d.timeout / 4)
ok := make(chan bool, 2) ok := make(chan bool, 2)
go func() { go func() {
@ -29,15 +42,17 @@ func deadlockDetect(mut sync.Locker, timeout time.Duration, name string) {
}() }()
go func() { go func() {
time.Sleep(timeout) time.Sleep(d.timeout)
ok <- false ok <- false
}() }()
if r := <-ok; !r { if r := <-ok; !r {
msg := fmt.Sprintf("deadlock detected at %s", name) msg := fmt.Sprintf("deadlock detected at %s", name)
if hmut, ok := mut.(Holder); ok { for otherName, otherMut := range d.lockers {
holder, goid := hmut.Holder() if otherHolder, ok := otherMut.(Holder); ok {
msg = fmt.Sprintf("deadlock detected at %s, current holder: %s at routine %d", name, holder, goid) holder, goid := otherHolder.Holder()
msg += fmt.Sprintf("\n %s = current holder: %s at routine %d", otherName, holder, goid)
}
} }
panic(msg) panic(msg)
} }

View File

@ -130,6 +130,10 @@ func (m *loggedRWMutex) RUnlock() {
m.RWMutex.RUnlock() m.RWMutex.RUnlock()
} }
func (m *loggedRWMutex) Holder() (string, int) {
return m.lockedAt, m.goid
}
type loggedWaitGroup struct { type loggedWaitGroup struct {
sync.WaitGroup sync.WaitGroup
} }