lib/sync, lib/model: Capture locker routine ID, print locker details on deadlock

This commit is contained in:
Audrius Butkevicius
2016-10-18 21:00:01 +01:00
parent 6152eb6d6d
commit 815588daba
3 changed files with 34 additions and 4 deletions

View File

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

View File

@@ -7,11 +7,16 @@
package model
import (
"fmt"
"sync"
"time"
)
func deadlockDetect(mut sync.Locker, timeout time.Duration) {
type Holder interface {
Holder() (string, int)
}
func deadlockDetect(mut sync.Locker, timeout time.Duration, name string) {
go func() {
for {
time.Sleep(timeout / 4)
@@ -29,7 +34,12 @@ func deadlockDetect(mut sync.Locker, timeout time.Duration) {
}()
if r := <-ok; !r {
panic("deadlock detected")
msg := fmt.Sprintf("deadlock detected at %s", name)
if hmut, ok := mut.(Holder); ok {
holder, goid := hmut.Holder()
msg = fmt.Sprintf("deadlock detected at %s, current holder: %s at routine %d", name, holder, goid)
}
panic(msg)
}
}
}()