mirror of
https://github.com/octoleo/syncthing.git
synced 2025-02-02 11:58:28 +00:00
lib/sync, lib/model: Capture locker routine ID, print locker details on deadlock
This commit is contained in:
parent
6152eb6d6d
commit
815588daba
@ -173,8 +173,8 @@ 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)
|
deadlockDetect(m.fmut, timeout, "fmut")
|
||||||
deadlockDetect(m.pmut, timeout)
|
deadlockDetect(m.pmut, timeout, "pmut")
|
||||||
}
|
}
|
||||||
|
|
||||||
// StartFolder constructs the folder service and starts it.
|
// StartFolder constructs the folder service and starts it.
|
||||||
|
@ -7,11 +7,16 @@
|
|||||||
package model
|
package model
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"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() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
time.Sleep(timeout / 4)
|
time.Sleep(timeout / 4)
|
||||||
@ -29,7 +34,12 @@ func deadlockDetect(mut sync.Locker, timeout time.Duration) {
|
|||||||
}()
|
}()
|
||||||
|
|
||||||
if r := <-ok; !r {
|
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
@ -10,6 +10,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
@ -60,12 +61,14 @@ type loggedMutex struct {
|
|||||||
sync.Mutex
|
sync.Mutex
|
||||||
start time.Time
|
start time.Time
|
||||||
lockedAt string
|
lockedAt string
|
||||||
|
goid int
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *loggedMutex) Lock() {
|
func (m *loggedMutex) Lock() {
|
||||||
m.Mutex.Lock()
|
m.Mutex.Lock()
|
||||||
m.start = time.Now()
|
m.start = time.Now()
|
||||||
m.lockedAt = getCaller()
|
m.lockedAt = getCaller()
|
||||||
|
m.goid = goid()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *loggedMutex) Unlock() {
|
func (m *loggedMutex) Unlock() {
|
||||||
@ -76,10 +79,15 @@ func (m *loggedMutex) Unlock() {
|
|||||||
m.Mutex.Unlock()
|
m.Mutex.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m *loggedMutex) Holder() (string, int) {
|
||||||
|
return m.lockedAt, m.goid
|
||||||
|
}
|
||||||
|
|
||||||
type loggedRWMutex struct {
|
type loggedRWMutex struct {
|
||||||
sync.RWMutex
|
sync.RWMutex
|
||||||
start time.Time
|
start time.Time
|
||||||
lockedAt string
|
lockedAt string
|
||||||
|
goid int
|
||||||
|
|
||||||
logUnlockers uint32
|
logUnlockers uint32
|
||||||
|
|
||||||
@ -98,6 +106,7 @@ func (m *loggedRWMutex) Lock() {
|
|||||||
duration := m.start.Sub(start)
|
duration := m.start.Sub(start)
|
||||||
|
|
||||||
m.lockedAt = getCaller()
|
m.lockedAt = getCaller()
|
||||||
|
m.goid = goid()
|
||||||
if duration > threshold {
|
if duration > threshold {
|
||||||
l.Debugf("RWMutex took %v to lock. Locked at %s. RUnlockers while locking: %s", duration, m.lockedAt, strings.Join(m.unlockers, ", "))
|
l.Debugf("RWMutex took %v to lock. Locked at %s. RUnlockers while locking: %s", duration, m.lockedAt, strings.Join(m.unlockers, ", "))
|
||||||
}
|
}
|
||||||
@ -139,3 +148,14 @@ func getCaller() string {
|
|||||||
file = filepath.Join(filepath.Base(filepath.Dir(file)), filepath.Base(file))
|
file = filepath.Join(filepath.Base(filepath.Dir(file)), filepath.Base(file))
|
||||||
return fmt.Sprintf("%s:%d", file, line)
|
return fmt.Sprintf("%s:%d", file, line)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func goid() int {
|
||||||
|
var buf [64]byte
|
||||||
|
n := runtime.Stack(buf[:], false)
|
||||||
|
idField := strings.Fields(strings.TrimPrefix(string(buf[:n]), "goroutine "))[0]
|
||||||
|
id, err := strconv.Atoi(idField)
|
||||||
|
if err != nil {
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
return id
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user