RussellLuo / timingwheel

Golang implementation of Hierarchical Timing Wheels.
MIT License
657 stars 123 forks source link

deadlock #25

Closed FJSDS closed 4 years ago

FJSDS commented 4 years ago

this is code:

package main

import (
    "fmt"
    "net/http"
    _ "net/http/pprof"
    "sync/atomic"
    "time"

    "github.com/RussellLuo/timingwheel"
)

type EveryScheduler struct {
    Interval time.Duration
}

func (s *EveryScheduler) Next(prev time.Time) time.Time {
    return prev.Add(s.Interval)
}

func main() {
    tw := timingwheel.NewTimingWheel(time.Millisecond, 60)
    tw.Start()
    defer tw.Stop()
    now := time.Now()
    var addCount int64
    for i := 0; i < 100000; i++ {
        _ = tw.ScheduleFunc(&EveryScheduler{time.Millisecond}, func() {
            atomic.AddInt64(&addCount, 1)
        })
    }

    go func() {
        http.ListenAndServe("0.0.0.0:8000", nil)
    }()
    for {
        n := time.Now()
        fmt.Println(atomic.LoadInt64(&addCount), n.Sub(now))
        now = n
        time.Sleep(time.Second)
    }
}

then, stdout print:

62576 83.7743ms
2516677 1.0003254s
3937967 1.0003248s
5276756 1.0003255s
6510020 1.0000941s
7849283 1.0003247s
9158321 1.0003253s
10420686 1.000325s
11601313 1.0003252s
12916420 1.0003255s
14280131 1.0003252s
15661477 1.0003246s
16964818 1.0003252s
17022683 1.0003653s
17022683 1.000204s
17022683 1.0003877s
17022683 1.0007381s
17022683 1.0004781s
17022683 1.0007608s
17022683 1.0000029s

load pprof:

http://127.0.0.1:8000/debug/pprof/goroutine?debug=2

all goroutine stop for wait mutex

goroutine 7 [semacquire]:
sync.runtime_SemacquireMutex(0xc00008a34c, 0xc000167c00, 0x1)
    C:/Go/src/runtime/sema.go:71 +0x4e
sync.(*Mutex).lockSlow(0xc00008a348)
    C:/Go/src/sync/mutex.go:138 +0x2a8
sync.(*Mutex).Lock(0xc00008a348)
    C:/Go/src/sync/mutex.go:81 +0x65
github.com/RussellLuo/timingwheel.(*bucket).Add(0xc00008a340, 0xc000493780)
    D:/GOPATH/src/github.com/RussellLuo/timingwheel/bucket.go:84 +0x45
github.com/RussellLuo/timingwheel.(*TimingWheel).add(0xc00009e0e0, 0xc000493780, 0xe5d5e00)
    D:/GOPATH/src/github.com/RussellLuo/timingwheel/timingwheel.go:81 +0x157
github.com/RussellLuo/timingwheel.(*TimingWheel).addOrRun(0xc00009e0e0, 0xc000493780)
    D:/GOPATH/src/github.com/RussellLuo/timingwheel/timingwheel.go:119 +0x3c
github.com/RussellLuo/timingwheel.(*TimingWheel).ScheduleFunc.func1()
    D:/GOPATH/src/github.com/RussellLuo/timingwheel/timingwheel.go:225 +0x1a9
github.com/panjf2000/ants.(*goWorker).run.func1(0xc000075a40)
    D:/GOPATH/src/github.com/panjf2000/ants/worker.go:68 +0xf2
created by github.com/panjf2000/ants.(*goWorker).run
    D:/GOPATH/src/github.com/panjf2000/ants/worker.go:48 +0x5e

goroutine 40 [semacquire]:
sync.runtime_SemacquireMutex(0xc00008a34c, 0xc003da2600, 0x1)
    C:/Go/src/runtime/sema.go:71 +0x4e
sync.(*Mutex).lockSlow(0xc00008a348)
    C:/Go/src/sync/mutex.go:138 +0x2a8
sync.(*Mutex).Lock(0xc00008a348)
    C:/Go/src/sync/mutex.go:81 +0x65
github.com/RussellLuo/timingwheel.(*bucket).Add(0xc00008a340, 0xc003c22ee0)
    D:/GOPATH/src/github.com/RussellLuo/timingwheel/bucket.go:84 +0x45
github.com/RussellLuo/timingwheel.(*TimingWheel).add(0xc00009e0e0, 0xc003c22ee0, 0xe5d5e00)
    D:/GOPATH/src/github.com/RussellLuo/timingwheel/timingwheel.go:81 +0x157
github.com/RussellLuo/timingwheel.(*TimingWheel).addOrRun(0xc00009e0e0, 0xc003c22ee0)
    D:/GOPATH/src/github.com/RussellLuo/timingwheel/timingwheel.go:119 +0x3c
github.com/RussellLuo/timingwheel.(*TimingWheel).ScheduleFunc.func1()
    D:/GOPATH/src/github.com/RussellLuo/timingwheel/timingwheel.go:225 +0x1a9
github.com/panjf2000/ants.(*goWorker).run.func1(0xc00030c270)
    D:/GOPATH/src/github.com/panjf2000/ants/worker.go:68 +0xf2
created by github.com/panjf2000/ants.(*goWorker).run
    D:/GOPATH/src/github.com/panjf2000/ants/worker.go:48 +0x5e

goroutine 41 [semacquire]:
sync.runtime_SemacquireMutex(0xc00008a34c, 0xc000165c00, 0x1)
    C:/Go/src/runtime/sema.go:71 +0x4e
sync.(*Mutex).lockSlow(0xc00008a348)
    C:/Go/src/sync/mutex.go:138 +0x2a8
sync.(*Mutex).Lock(0xc00008a348)
    C:/Go/src/sync/mutex.go:81 +0x65
github.com/RussellLuo/timingwheel.(*bucket).Add(0xc00008a340, 0xc00064b620)
    D:/GOPATH/src/github.com/RussellLuo/timingwheel/bucket.go:84 +0x45
github.com/RussellLuo/timingwheel.(*TimingWheel).add(0xc00009e0e0, 0xc00064b620, 0xe5d5e00)
    D:/GOPATH/src/github.com/RussellLuo/timingwheel/timingwheel.go:81 +0x157
github.com/RussellLuo/timingwheel.(*TimingWheel).addOrRun(0xc00009e0e0, 0xc00064b620)
    D:/GOPATH/src/github.com/RussellLuo/timingwheel/timingwheel.go:119 +0x3c
github.com/RussellLuo/timingwheel.(*TimingWheel).ScheduleFunc.func1()
    D:/GOPATH/src/github.com/RussellLuo/timingwheel/timingwheel.go:225 +0x1a9
github.com/panjf2000/ants.(*goWorker).run.func1(0xc00030c2a0)
    D:/GOPATH/src/github.com/panjf2000/ants/worker.go:68 +0xf2
created by github.com/panjf2000/ants.(*goWorker).run
    D:/GOPATH/src/github.com/panjf2000/ants/worker.go:48 +0x5e

goroutine 42 [semacquire]:
sync.runtime_SemacquireMutex(0xc00008a34c, 0xc000323c00, 0x1)
    C:/Go/src/runtime/sema.go:71 +0x4e
sync.(*Mutex).lockSlow(0xc00008a348)
    C:/Go/src/sync/mutex.go:138 +0x2a8
sync.(*Mutex).Lock(0xc00008a348)
    C:/Go/src/sync/mutex.go:81 +0x65
github.com/RussellLuo/timingwheel.(*bucket).Add(0xc00008a340, 0xc000510f20)
    D:/GOPATH/src/github.com/RussellLuo/timingwheel/bucket.go:84 +0x45
github.com/RussellLuo/timingwheel.(*TimingWheel).add(0xc00009e0e0, 0xc000510f20, 0xe5d5e00)
    D:/GOPATH/src/github.com/RussellLuo/timingwheel/timingwheel.go:81 +0x157
github.com/RussellLuo/timingwheel.(*TimingWheel).addOrRun(0xc00009e0e0, 0xc000510f20)
    D:/GOPATH/src/github.com/RussellLuo/timingwheel/timingwheel.go:119 +0x3c
github.com/RussellLuo/timingwheel.(*TimingWheel).ScheduleFunc.func1()
    D:/GOPATH/src/github.com/RussellLuo/timingwheel/timingwheel.go:225 +0x1a9
github.com/panjf2000/ants.(*goWorker).run.func1(0xc00030c2d0)
    D:/GOPATH/src/github.com/panjf2000/ants/worker.go:68 +0xf2
created by github.com/panjf2000/ants.(*goWorker).run
    D:/GOPATH/src/github.com/panjf2000/ants/worker.go:48 +0x5e

goroutine 43 [semacquire]:
sync.runtime_SemacquireMutex(0xc00008a34c, 0xc000325c00, 0x1)
    C:/Go/src/runtime/sema.go:71 +0x4e
sync.(*Mutex).lockSlow(0xc00008a348)
    C:/Go/src/sync/mutex.go:138 +0x2a8
sync.(*Mutex).Lock(0xc00008a348)
    C:/Go/src/sync/mutex.go:81 +0x65
github.com/RussellLuo/timingwheel.(*bucket).Add(0xc00008a340, 0xc003b756e0)
    D:/GOPATH/src/github.com/RussellLuo/timingwheel/bucket.go:84 +0x45
github.com/RussellLuo/timingwheel.(*TimingWheel).add(0xc00009e0e0, 0xc003b756e0, 0xe5d5e00)
    D:/GOPATH/src/github.com/RussellLuo/timingwheel/timingwheel.go:81 +0x157
github.com/RussellLuo/timingwheel.(*TimingWheel).addOrRun(0xc00009e0e0, 0xc003b756e0)
    D:/GOPATH/src/github.com/RussellLuo/timingwheel/timingwheel.go:119 +0x3c
github.com/RussellLuo/timingwheel.(*TimingWheel).ScheduleFunc.func1()
    D:/GOPATH/src/github.com/RussellLuo/timingwheel/timingwheel.go:225 +0x1a9
github.com/panjf2000/ants.(*goWorker).run.func1(0xc00030c300)
    D:/GOPATH/src/github.com/panjf2000/ants/worker.go:68 +0xf2
created by github.com/panjf2000/ants.(*goWorker).run
    D:/GOPATH/src/github.com/panjf2000/ants/worker.go:48 +0x5e

goroutine 44 [semacquire]:
sync.runtime_SemacquireMutex(0xc00008a34c, 0xc00031fc00, 0x1)
    C:/Go/src/runtime/sema.go:71 +0x4e
sync.(*Mutex).lockSlow(0xc00008a348)
    C:/Go/src/sync/mutex.go:138 +0x2a8
sync.(*Mutex).Lock(0xc00008a348)
    C:/Go/src/sync/mutex.go:81 +0x65
github.com/RussellLuo/timingwheel.(*bucket).Add(0xc00008a340, 0xc003a7aac0)
    D:/GOPATH/src/github.com/RussellLuo/timingwheel/bucket.go:84 +0x45
github.com/RussellLuo/timingwheel.(*TimingWheel).add(0xc00009e0e0, 0xc003a7aac0, 0xe5d5e00)
    D:/GOPATH/src/github.com/RussellLuo/timingwheel/timingwheel.go:81 +0x157
github.com/RussellLuo/timingwheel.(*TimingWheel).addOrRun(0xc00009e0e0, 0xc003a7aac0)
    D:/GOPATH/src/github.com/RussellLuo/timingwheel/timingwheel.go:119 +0x3c
github.com/RussellLuo/timingwheel.(*TimingWheel).ScheduleFunc.func1()
    D:/GOPATH/src/github.com/RussellLuo/timingwheel/timingwheel.go:225 +0x1a9
github.com/panjf2000/ants.(*goWorker).run.func1(0xc00030c330)
    D:/GOPATH/src/github.com/panjf2000/ants/worker.go:68 +0xf2
created by github.com/panjf2000/ants.(*goWorker).run
    D:/GOPATH/src/github.com/panjf2000/ants/worker.go:48 +0x5e

goroutine 45 [semacquire]:
sync.runtime_SemacquireMutex(0xc00008a34c, 0xc0039b0b00, 0x1)
    C:/Go/src/runtime/sema.go:71 +0x4e
sync.(*Mutex).lockSlow(0xc00008a348)
    C:/Go/src/sync/mutex.go:138 +0x2a8
sync.(*Mutex).Lock(0xc00008a348)
    C:/Go/src/sync/mutex.go:81 +0x65
github.com/RussellLuo/timingwheel.(*bucket).Add(0xc00008a340, 0xc00210a1a0)
    D:/GOPATH/src/github.com/RussellLuo/timingwheel/bucket.go:84 +0x45
github.com/RussellLuo/timingwheel.(*TimingWheel).add(0xc00009e0e0, 0xc00210a1a0, 0xe5d5e00)
    D:/GOPATH/src/github.com/RussellLuo/timingwheel/timingwheel.go:81 +0x157
github.com/RussellLuo/timingwheel.(*TimingWheel).addOrRun(0xc00009e0e0, 0xc00210a1a0)
    D:/GOPATH/src/github.com/RussellLuo/timingwheel/timingwheel.go:119 +0x3c
github.com/RussellLuo/timingwheel.(*TimingWheel).ScheduleFunc.func1()
    D:/GOPATH/src/github.com/RussellLuo/timingwheel/timingwheel.go:225 +0x1a9
github.com/panjf2000/ants.(*goWorker).run.func1(0xc00030c360)
    D:/GOPATH/src/github.com/panjf2000/ants/worker.go:68 +0xf2
created by github.com/panjf2000/ants.(*goWorker).run
    D:/GOPATH/src/github.com/panjf2000/ants/worker.go:48 +0x5e

goroutine 46 [semacquire]:
sync.runtime_SemacquireMutex(0xc00008a34c, 0xc00032bc00, 0x1)
    C:/Go/src/runtime/sema.go:71 +0x4e
sync.(*Mutex).lockSlow(0xc00008a348)
    C:/Go/src/sync/mutex.go:138 +0x2a8
sync.(*Mutex).Lock(0xc00008a348)
    C:/Go/src/sync/mutex.go:81 +0x65
github.com/RussellLuo/timingwheel.(*bucket).Add(0xc00008a340, 0xc003e77ce0)
    D:/GOPATH/src/github.com/RussellLuo/timingwheel/bucket.go:84 +0x45
github.com/RussellLuo/timingwheel.(*TimingWheel).add(0xc00009e0e0, 0xc003e77ce0, 0xe5d5e00)
    D:/GOPATH/src/github.com/RussellLuo/timingwheel/timingwheel.go:81 +0x157
github.com/RussellLuo/timingwheel.(*TimingWheel).addOrRun(0xc00009e0e0, 0xc003e77ce0)
    D:/GOPATH/src/github.com/RussellLuo/timingwheel/timingwheel.go:119 +0x3c
github.com/RussellLuo/timingwheel.(*TimingWheel).ScheduleFunc.func1()
    D:/GOPATH/src/github.com/RussellLuo/timingwheel/timingwheel.go:225 +0x1a9
github.com/panjf2000/ants.(*goWorker).run.func1(0xc00030c390)
    D:/GOPATH/src/github.com/panjf2000/ants/worker.go:68 +0xf2
created by github.com/panjf2000/ants.(*goWorker).run
    D:/GOPATH/src/github.com/panjf2000/ants/worker.go:48 +0x5e
FJSDS commented 4 years ago

https://github.com/RussellLuo/timingwheel/pull/27 run same code is good of this pr . can merge?