debugmutex

package module
v0.0.0-...-c187cad Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 18, 2019 License: MIT Imports: 5 Imported by: 0

README

debugmutex

Build Status GoDoc

Mutex for debugging deadlocks. It can find non-obvious deadlocks in systems with heavy sync.Mutex usage. I found many deadlocks in Docker with it.

Usage

type Struct struct {
    sync.Locker
}

func New() *Struct {
    locker := &sync.Mutex{}
    if os.Getenv("DEBUG") != "" {
        // will crash program with traceback and file:line where deadlock is
        // occured after five tries to acquire mutex with 1 second gap between.
        locker = debugmutex.New(5, true)
    }
    return &Struct{Locker: locker}
}

For logging github.com/sirupsen/logrus is used. You can set debug logging with

logrus.SetLevel(logrus.DebugLevel)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(retries int, fatal bool) sync.Locker

New returns new debug mutex. retries specify the number of retries acquiring Mutex before error message, if <=0, then retry forever. If fatal is true, then program will exit by os.Exit(1).

Types

type Mutex

type Mutex struct {
	// contains filtered or unexported fields
}

Mutex is debugging mutex, which prints logs and traces for locks. Mutex implements sync.Locker interface.

func (*Mutex) Lock

func (m *Mutex) Lock()

Lock tries to lock mutex. Interval between attempts is 1 second. On each attempt stack trace and file:line of a previous Lock printed. Lock does os.Exit(1) after last attempt if fatal parameter was true.

func (*Mutex) Unlock

func (m *Mutex) Unlock()

Unlock unlocks the mutex. It prints place in the code where it was called and where mutex was locked.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL