Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

affected/package: time time.AfterFunc not working when the code is in a struct. #59842

Closed
jubaobleach opened this issue Apr 26, 2023 · 2 comments

Comments

@jubaobleach
Copy link

jubaobleach commented Apr 26, 2023

What version of Go are you using (go version)?

I try both go version go1.17.1 darwin/amd64 and go version go1.20.3 darwin/amd64

$ go version
go version go1.17.1 darwin/amd64
go version go1.20.3 

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE="off"
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/kuma/Library/Caches/go-build"
GOENV="/Users/kuma/Library/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/kuma/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/kuma/go"
GOPRIVATE=""
GOPROXY="https://goproxy.cn,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GOVCS=""
GOVERSION="go1.20.3"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD=""
GOWORK=""
CGO_CFLAGS="-O2 -g"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-O2 -g"
CGO_FFLAGS="-O2 -g"
CGO_LDFLAGS="-O2 -g"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/q6/zk_3f2k92tv8lyptpmz927y00000gn/T/go-build2068950621=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

Here is the code
package main

import (
"fmt"
"time"
)

type Packet struct {
seqNum uint
data []byte
}

type Window struct {
windowSize uint
base uint
nextSeqNum uint
packets map[uint]*Packet
timer *time.Timer
timeout time.Duration
}

func NewWindow(windowSize uint, timeout time.Duration) *Window {
return &Window{
windowSize: windowSize,
base: 0,
nextSeqNum: 0,
packets: make(map[uint]*Packet),
timer: nil,
timeout: timeout,
}
}

func (w *Window) SendPacket(seqNum uint, data []byte) {
if int(seqNum-w.base) < 0 || int(w.base+w.windowSize-1-seqNum) < 0 {
return
}

if _, ok := w.packets[seqNum]; ok {
	return
}
packet := &Packet{
	seqNum: seqNum,
	data:   data,
}
w.packets[seqNum] = packet

f := func() {
	fmt.Println("hit timer run")
	w.Timeout()
	w.timer = nil
}

if w.timer == nil {
	fmt.Println("Timer Start", w.timeout)
	w.timer = time.AfterFunc(w.timeout, f)
}

}

func (w *Window) Timeout() {
fmt.Println("timout hit")
for i := uint(0); i < w.windowSize; i++ {
seqNum := w.base + i
if seqNum > w.nextSeqNum {
break
}
if packet, ok := w.packets[seqNum]; ok && packet != nil {
fmt.Printf("Packet %d timed out", seqNum)
delete(w.packets, seqNum)
}
}
w.base = (w.base + 1) % 0x10000
w.nextSeqNum = (w.base + 1) % 0x10000
}

func (w *Window) ReceivePacket(seqNum uint) (int, []byte) {
if int(seqNum-w.base) < 0 || int(w.base+w.windowSize-1-seqNum) < 0 {
return 1, nil
}

packet, ok := w.packets[seqNum]
if !ok || packet == nil {
	return 2, nil
}
delete(w.packets, seqNum)
if w.timer != nil {
	w.timer.Stop()
	w.timer = nil
}
w.base = (seqNum + 1)
w.nextSeqNum = (w.base + 1)
return 0, packet.data

}

func main() {

window := NewWindow(256, time.Millisecond)
window.SendPacket(1, []byte("Packet 1"))
window.SendPacket(2, []byte("Packet 2"))
window.SendPacket(3, []byte("Packet 3"))
window.SendPacket(4, []byte("Packet 4"))
window.SendPacket(5, []byte("Packet 5"))
window.SendPacket(6, []byte("Packet 6"))
window.SendPacket(7, []byte("Packet 7"))
window.SendPacket(8, []byte("Packet 8"))
fmt.Println(window.ReceivePacket(3))
fmt.Println(window.ReceivePacket(1))
fmt.Println(window.ReceivePacket(2))
time.Sleep(time.Second * 2)
fmt.Println(window.ReceivePacket(4))
fmt.Println(window.ReceivePacket(5))
fmt.Println(window.ReceivePacket(7))
fmt.Println(window.ReceivePacket(8))
time.Sleep(time.Second * 2)

}

What did you expect to see?

after 1ms this func will be hit.
f := func() {
fmt.Println("hit timer run")
w.Timeout()
w.timer = nil
}

and if I modify the code,
if w.timer == nil {
fmt.Println("Timer Start", w.timeout)
//w.timer = time.AfterFunc(w.timeout, f)
time.AfterFunc(w.timeout, f) // it works why ?
}

What did you see instead?

Nothing happened after 4 seconds

@jubaobleach jubaobleach changed the title affected/package: time affected/package: time time.AfterFunc not working when the code is in a struct. Apr 26, 2023
@jdemeyer
Copy link

You're calling Timer1.Stop(), which stops the timer. So it's expected that the AfterFunc is not called.

@jubaobleach
Copy link
Author

jubaobleach commented Apr 26, 2023

yes, I put the origin code, and rewrite the issue.

@jubaobleach jubaobleach reopened this Apr 26, 2023
@golang golang locked and limited conversation to collaborators Apr 25, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants