RussellLuo / timingwheel

Golang implementation of Hierarchical Timing Wheels.
MIT License
660 stars 125 forks source link

解决 invalid memory address or nil pointer dereference 问题 #6

Closed l5071134 closed 5 years ago

RussellLuo commented 5 years ago

@l5071134 谢谢关注!请问这个 PR 是要解决哪个问题,有复现问题的步骤吗?

l5071134 commented 5 years ago

@l5071134 谢谢关注!请问这个 PR 是要解决哪个问题,有复现问题的步骤吗?

执行go test ./timingwheel_test.go 会提示指针错误,调整结构体list的顺序就可以了,对了好半天找到是这的问题。

panic: runtime error: invalid memory address or nil pointer dereference [recovered] panic: runtime error: invalid memory address or nil pointer dereference [signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x8049e2c]

goroutine 8 [running]: testing.tRunner.func1(0x9886140) /media/yxl/c3eba355-98a8-4630-8002-730ff890d2d1/data/soft/go/src/testing/testing.go:792 +0x2fc panic(0x813ad60, 0x82225f8) /media/yxl/c3eba355-98a8-4630-8002-730ff890d2d1/data/soft/go/src/runtime/panic.go:513 +0x16d runtime/internal/atomic.Xchg64(0x985498c, 0xc39586d7, 0x16a, 0x9854b00, 0x9854b00) /media/yxl/c3eba355-98a8-4630-8002-730ff890d2d1/data/soft/go/src/runtime/internal/atomic/asm_386.s:148 +0xc github.com/RussellLuo/timingwheel.(bucket).SetExpiration(0x9854980, 0xc39586d7, 0x16a, 0x0) /opt/gopath/src/github.com/RussellLuo/timingwheel/bucket.go:76 +0x36 github.com/RussellLuo/timingwheel.(TimingWheel).add(0x9888140, 0x9854ae0, 0x9854ae0) /opt/gopath/src/github.com/RussellLuo/timingwheel/timingwheel.go:77 +0x157 github.com/RussellLuo/timingwheel.(TimingWheel).addOrRun(0x9888140, 0x9854ae0) /opt/gopath/src/github.com/RussellLuo/timingwheel/timingwheel.go:111 +0x29 github.com/RussellLuo/timingwheel.(TimingWheel).AfterFunc(0x9888140, 0xf4240, 0x0, 0x980e268, 0x8226340) /opt/gopath/src/github.com/RussellLuo/timingwheel/timingwheel.go:168 +0x16d command-line-arguments_test.TestTimingWheel_AfterFunc.func1(0x9886140) /media/yxl/c3eba355-98a8-4630-8002-730ff890d2d1/data/go_code/code/src/timingwheel/timingwheel_test.go:29 +0xaf testing.tRunner(0x9886140, 0x980c070) /media/yxl/c3eba355-98a8-4630-8002-730ff890d2d1/data/soft/go/src/testing/testing.go:827 +0xa3 created by testing.(*T).Run /media/yxl/c3eba355-98a8-4630-8002-730ff890d2d1/data/soft/go/src/testing/testing.go:878 +0x2a1

RussellLuo commented 5 years ago

我这里执行一切正常:

$ go version
go version go1.9.3 darwin/amd64

$ go test -v -race timingwheel_test.go 
=== RUN   TestTimingWheel_AfterFunc
=== RUN   TestTimingWheel_AfterFunc/#00
=== RUN   TestTimingWheel_AfterFunc/#01
=== RUN   TestTimingWheel_AfterFunc/#02
=== RUN   TestTimingWheel_AfterFunc/#03
=== RUN   TestTimingWheel_AfterFunc/#04
=== RUN   TestTimingWheel_AfterFunc/#05
=== RUN   TestTimingWheel_AfterFunc/#06
--- PASS: TestTimingWheel_AfterFunc (1.68s)
    --- PASS: TestTimingWheel_AfterFunc/#00 (0.00s)
    --- PASS: TestTimingWheel_AfterFunc/#01 (0.01s)
    --- PASS: TestTimingWheel_AfterFunc/#02 (0.01s)
    --- PASS: TestTimingWheel_AfterFunc/#03 (0.05s)
    --- PASS: TestTimingWheel_AfterFunc/#04 (0.10s)
    --- PASS: TestTimingWheel_AfterFunc/#05 (0.50s)
    --- PASS: TestTimingWheel_AfterFunc/#06 (1.00s)
PASS
ok      command-line-arguments  2.691s
l5071134 commented 5 years ago

这是我的版本 go version go1.11 linux/amd64

RussellLuo commented 5 years ago

我用 Go 1.11 测试了下,看起来也是正常的:

$ go version
go version go1.11.10 linux/amd64

$ go test -v -race timingwheel_test.go
=== RUN   TestTimingWheel_AfterFunc
=== RUN   TestTimingWheel_AfterFunc/#00
=== RUN   TestTimingWheel_AfterFunc/#01
=== RUN   TestTimingWheel_AfterFunc/#02
=== RUN   TestTimingWheel_AfterFunc/#03
=== RUN   TestTimingWheel_AfterFunc/#04
=== RUN   TestTimingWheel_AfterFunc/#05
=== RUN   TestTimingWheel_AfterFunc/#06
--- PASS: TestTimingWheel_AfterFunc (1.68s)
    --- PASS: TestTimingWheel_AfterFunc/#00 (0.00s)
    --- PASS: TestTimingWheel_AfterFunc/#01 (0.01s)
    --- PASS: TestTimingWheel_AfterFunc/#02 (0.01s)
    --- PASS: TestTimingWheel_AfterFunc/#03 (0.05s)
    --- PASS: TestTimingWheel_AfterFunc/#04 (0.10s)
    --- PASS: TestTimingWheel_AfterFunc/#05 (0.50s)
    --- PASS: TestTimingWheel_AfterFunc/#06 (1.00s)
PASS
ok      command-line-arguments  2.690s

@l5071134 你那里是每次必定出错吗?如果 “struct 内部不同的字段顺序,会导致 Go 程序有不同的表现”,我会觉得这有点不可思议 🤔

RussellLuo commented 5 years ago

该 PR 解决的问题无法复现,先关闭。如果有人遇到类似问题,或者有复现方法,欢迎提 issue。