Skip to content

time: time.After and time.Sleep return early on windows/amd64 #45582

@sitnikovv

Description

@sitnikovv

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

OS: Microsoft Windows Server 2019 Standard

$ go version
go version go1.16.3 windows/amd64

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
set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\xxx\AppData\Local\go-build
set GOENV=C:\Users\xxx\AppData\Roaming\go\env
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=d:\gopath\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=d:\gopath
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=c:\go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=c:\go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.16.3
set GCCGO=gccgo
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=NUL
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0 -fdebug-prefix-map=C:\Users\xxx\AppData\Local\Temp\2\go-build1340947938=/tmp/go-build -gno-record-gcc-switches

What did you do?

package main

import (
	"fmt"
	"time"
)

func TruncateMinute(inputTime time.Time, minute int) time.Time {
	return inputTime.Truncate(time.Duration(minute) * time.Minute)
}

func main() {
	for {
		tickTime := time.Now()
		fmt.Printf("\ncron_tick: %v\n", tickTime)

		time.Sleep(10 * time.Millisecond)

		now := time.Now()
		nextTick := TruncateMinute(now.Add(time.Minute), 1)
		waitTick := nextTick.Sub(now)
		fmt.Printf("cron_now: %v\n", now)
		fmt.Printf("cron_next: %v\n", nextTick)
		fmt.Printf("cron_wait: %v\n", waitTick)
		//time.Sleep(waitTick)
		select {
		case <-time.After(waitTick):
		}
	}

}

What did you expect to see?

I expected time.After not to fire ahead of time

What did you see instead?

Start, there are 17.3107307s left until the start of the next minute, but ended earlier (next cycle start at 15:29:59.9998877)

cron_tick: 2021-04-15 15:29:42.6426691 +0300 MSK m=+0.003286301
cron_now: 2021-04-15 15:29:42.6892693 +0300 MSK m=+0.049887501
cron_next: 2021-04-15 15:30:00 +0300 MSK
cron_wait: 17.3107307s

Next iteration 59.9894804s left until the start of the next minute, but ended earlier (next cycle start at 15:30:59.9997131)

cron_tick: 2021-04-15 15:29:59.9998877 +0300 MSK m=+17.360879901
cron_now: 2021-04-15 15:30:00.0105196 +0300 MSK m=+17.371512001
cron_next: 2021-04-15 15:31:00 +0300 MSK
cron_wait: 59.9894804s

Next iteration 59.9897221s left until the start of the next minute, but ended earlier (next cycle start at 15:31:59.9993215)

cron_tick: 2021-04-15 15:30:59.9997131 +0300 MSK m=+77.361995801
cron_now: 2021-04-15 15:31:00.0102779 +0300 MSK m=+77.372560801
cron_next: 2021-04-15 15:32:00 +0300 MSK
cron_wait: 59.9897221s

... Skipped

Next iteration 59.9903326s left until the start of the next minute, CORRECT!!! (next cycle start at 15:41:00.000753)

cron_tick: 2021-04-15 15:39:59.999119 +0300 MSK m=+617.373498701
cron_now: 2021-04-15 15:40:00.0096674 +0300 MSK m=+617.384047301
cron_next: 2021-04-15 15:41:00 +0300 MSK
cron_wait: 59.9903326s

Next iteration 59.9886968s left until the start of the next minute, CORRECT!!! (next cycle start at 15:42:00.0000637)

cron_tick: 2021-04-15 15:41:00.0007533 +0300 MSK m=+677.376445001
cron_now: 2021-04-15 15:41:00.0113032 +0300 MSK m=+677.386995101
cron_next: 2021-04-15 15:42:00 +0300 MSK
cron_wait: 59.9886968s

Next iteration 59.9891146s left until the start of the next minute, but ended earlier (next cycle start at 15:42:59.9990432)

cron_tick: 2021-04-15 15:42:00.0000637 +0300 MSK m=+737.377059301
cron_now: 2021-04-15 15:42:00.0108854 +0300 MSK m=+737.387881301
cron_next: 2021-04-15 15:43:00 +0300 MSK
cron_wait: 59.9891146s

cron_tick: 2021-04-15 15:42:59.9990432 +0300 MSK m=+797.377335701
cron_now: 2021-04-15 15:43:00.0104573 +0300 MSK m=+797.388750001
cron_next: 2021-04-15 15:44:00 +0300 MSK
cron_wait: 59.9895427s

On Linux all good (but linux is not so heavily loaded)

Metadata

Metadata

Assignees

No one assigned

    Labels

    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.OS-Windows

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions