RussellLuo / timingwheel

Golang implementation of Hierarchical Timing Wheels.
MIT License
656 stars 121 forks source link

32位机器运行会 panic: runtime error #13

Closed Allenxuxu closed 4 years ago

Allenxuxu commented 4 years ago
package main

import (
    "fmt"
    "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, 20)
    tw.Start()
    defer tw.Stop()

    _ = tw.ScheduleFunc(&EveryScheduler{time.Second}, func() {
        fmt.Println("The timer fires")
    })

    select {}
}

在 centos6.5 32位的机器上运行如上代码会造成 panic ,centos7 则正常:

[root@develop test]# go run main.go
go: finding github.com/RussellLuo/timingwheel latest
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x8049fac]

goroutine 1 [running]:
runtime/internal/atomic.Xchg64(0x9c66bcc, 0xcdfcee50, 0x16d, 0x0, 0x1)
        /usr/lib/golang/src/runtime/internal/atomic/asm_386.s:151 +0xc
github.com/RussellLuo/timingwheel.(*bucket).SetExpiration(...)
        /home/gohome/pkg/mod/github.com/!russell!luo/timingwheel@v0.0.0-20190930141959-b40c04085073/bucket.go:74
github.com/RussellLuo/timingwheel.(*TimingWheel).add(0x9c761e0, 0x9c66620, 0x9c761e0)
        /home/gohome/pkg/mod/github.com/!russell!luo/timingwheel@v0.0.0-20190930141959-b40c04085073/timingwheel.go:79 +0x167
github.com/RussellLuo/timingwheel.(*TimingWheel).add(0x9c76140, 0x9c66620, 0x9c76140)
        /home/gohome/pkg/mod/github.com/!russell!luo/timingwheel@v0.0.0-20190930141959-b40c04085073/timingwheel.go:106 +0x1f2
github.com/RussellLuo/timingwheel.(*TimingWheel).add(0x9c760a0, 0x9c66620, 0x9c66640)
        /home/gohome/pkg/mod/github.com/!russell!luo/timingwheel@v0.0.0-20190930141959-b40c04085073/timingwheel.go:106 +0x1f2
github.com/RussellLuo/timingwheel.(*TimingWheel).addOrRun(0x9c760a0, 0x9c66620)
        /home/gohome/pkg/mod/github.com/!russell!luo/timingwheel@v0.0.0-20190930141959-b40c04085073/timingwheel.go:113 +0x29
github.com/RussellLuo/timingwheel.(*TimingWheel).ScheduleFunc(0x9c760a0, 0x81173c0, 0x9c120b8, 0x8105430, 0x9c760a0)
        /home/gohome/pkg/mod/github.com/!russell!luo/timingwheel@v0.0.0-20190930141959-b40c04085073/timingwheel.go:222 +0x2b5
main.main()
        /home/repo/test/main.go:23 +0xba
exit status 2

atomic 库中有如下说明:

Bugs ☞ On x86-32, the 64-bit functions use instructions unavailable before the Pentium MMX. On non-Linux ARM, the 64-bit functions use instructions unavailable before the ARMv6k core. On ARM, x86-32, and 32-bit MIPS, it is the caller's responsibility to arrange for 64-bit alignment of 64-bit words accessed atomically. The first word in a variable or in an allocated struct, array, or slice can be relied upon to be 64-bit aligned.

https://godoc.org/sync/atomic#pkg-note-bug

RussellLuo commented 4 years ago

@Allenxuxu 感谢,修复速度真快 👍

For more explanations, see Memory Layouts.