Skip to content

runtime: time.Sleep problem on Windows 10, but not Windows 7 or LinuxΒ #61042

Closed as not planned

Description

@johnrs

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

go version go1.20.5 windows/amd64 - "new"
go version go1.15.13 windows/amd64 - "old"

Does this issue reproduce with the latest release?

Yes

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

windows_amd64 - Windows 10 Pro, 22H2, Build 19045.2965
windows_amd64 - Windows 7 Ultimate, 6.1.7601, Service Pack 1, Build 7601

What did you do?

package main

import (
	"fmt"
	"time"
)

func main() {
	fmt.Println()
	fmt.Println("Time Resolution")
	for i := 0; i < 10; i++ {
		fmt.Printf("    %v\n", timeDiff())
	}

	fmt.Println()
	fmt.Println("Sleep Resolution")
	time.Sleep(1) // sync with the sleep timer
	for i := 0; i < 10; i++ {
		fmt.Printf("    %v\n", sleepDiff())
	}
}

func timeDiff() time.Duration {
	t0 := time.Now()
	for {
		t := time.Now()
		if t != t0 {
			return t.Sub(t0)
		}
	}
}

func sleepDiff() time.Duration {
	t0 := time.Now()
	time.Sleep(1)
	return time.Now().Sub(t0)
}

What did you expect to see?

Results using Go v1.15.13 or v1.20.5 code on Windows 7.
Time Resolution is fine. Sleep Resolution is fine.

Time Resolution
    1.0001ms
    1ms
    1.0001ms
    1ms
    1.0001ms
    1ms
    1.0001ms
    1.0001ms
    1ms
    1.0001ms

Sleep Resolution
    1ms
    1.0001ms
    1.0001ms
    1ms
    1.0001ms
    1ms
    1.0001ms
    1ms
    1.0001ms
    1.0001ms

What did you see instead?

Results using Go v1.20.5 code on Windows 10.
Time Resolution is fine. Sleep Resolution is bad. It should be ~1ms.

Time Resolution
    509.5Β΅s
    555.1Β΅s
    587.7Β΅s
    95.9Β΅s
    585.7Β΅s
    585.4Β΅s
    596.7Β΅s
    514.4Β΅s
    514.4Β΅s
    514.1Β΅s

Sleep Resolution
    15.2307ms
    34.8Β΅s
    13.9486ms
    14.8512ms
    15.1862ms
    15.0722ms
    15.7843ms
    15.6718ms
    898.4Β΅s
    14.525ms

Results using Go v1.15.13 code on Windows 10.
Time Resolution is ok. Sleep Resolution is ok. Neither are as good as when run on Windows 7, but they are certainly usable.

Time Resolution
    1.0394ms
    1.0505ms
    918.6Β΅s
    1.022ms
    997.7Β΅s
    1.0016ms
    1.0788ms
    986.7Β΅s
    1.0057ms
    993.9Β΅s

Sleep Resolution
    1.0853ms
    1.1475ms
    1.1202ms
    995.8Β΅s
    1.211ms
    1.2033ms
    1.2386ms
    1.2209ms
    1.0501ms
    1.1822ms

=== Comments

Comments:

A problem I was having with a program I was working on led me to this issue.
So yes, it makes a difference in the real world.

I don't have a Windows 11 PC.
It would be interesting to see if it has the problem or not.

The "new" code was built on the Windows 10 PC.
The "old" code was build on the Windows 7 PC.

Both Windows PC clocks were running at 1ms due to other software on them.
This was verified using TimerTool.exe.
But this is moot, since the very first Sleep should have caused Go's auto-clock-switch to force it to 1ms and keep it there till 50ms after the last Sleep.

The Windows 10 PC is about 5 times faster than the much older Windows 7 PC.
Yet the results from the older PC are much "cleaner" (less latency error).

The "new" (I didn't try old) code was fine on both a Linux server and Raspberry Pi 4.

Activity

  1. changed the title [-]affected/package: runtime and/or time.Sleep() problem on Windows 10, but not Windows 7 or Linux.[/-] [+]runtime and/or time.Sleep() problem on Windows 10, but not Windows 7 or Linux.[/+] on Jun 27, 2023
  2. changed the title [-]runtime and/or time.Sleep() problem on Windows 10, but not Windows 7 or Linux.[/-] [+]runtime: time.Sleep problem on Windows 10, but not Windows 7 or Linux[/+] on Jun 27, 2023
  3. ianlancetaylor commented on Jun 27, 2023

    @ianlancetaylor
    Contributor

    CC @golang/windows

  4. added
    compiler/runtimeIssues related to the Go compiler and/or runtime.
    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.
    on Jun 27, 2023
  5. seankhliao commented on Jun 27, 2023

    @seankhliao
    Member

    is this #44343 ?

  6. added
    WaitingForInfoIssue is not actionable because of missing required information, which needs to be provided.
    on Jun 27, 2023
  7. johnrs commented on Jun 27, 2023

    @johnrs
    Author

    I think that it's possible that they might be related, but the symptoms seem pretty different.

  8. removed
    WaitingForInfoIssue is not actionable because of missing required information, which needs to be provided.
    on Jun 27, 2023
  9. mknyszek commented on Jun 28, 2023

    @mknyszek
    Contributor

    I suspect they are the same issue (especially given the Go version ranges). Could you elaborate on which of the symptoms are different? Thanks.

    Also, if you're so inclined, it should be straightforward to identify if it's the same issue by trying 8fdc79e and the commit before.

  10. johnrs commented on Jun 28, 2023

    @johnrs
    Author

    I stand corrected. I didn't read it carefully enough. Sorry.

    My gut feeling is that there are 2 issues happening at the same time. One is the Windows auto-switch from 15.6ms to 1ms is broken. And the other is the "noisy" results, some even LESS than the period (that should only happen on a random sleep, not then they are consecutive (except for the first).

    Still, it's VERY strange that I don't see either issue when I run the test on Windows 7.

  11. 12 remaining items

  12. johnrs commented on Jul 4, 2023

    @johnrs
    Author

    @ChrisHines Interesting. When I search #44343 for osRelax I don't see much mention of it. Could you point me to where the counter arguments against restoring it are? Thanks.

  13. ChrisHines commented on Jul 5, 2023

    @ChrisHines
    Contributor

    @johnrs Looking for mentions of timeBeginPeriod might be better.

    Here are a few comments worth reading:

    I admit there is less debate about it in that thread than I remembered. I think I was confusing it with this other thread that I had come across contemporary to my previous work on runtime timers. You will probably remember this thread, as you commented there more than once. The part I think is most relevant starts here:

  14. johnrs commented on Jul 5, 2023

    @johnrs
    Author

    @ChrisHines Thanks!

    The relevant part that you referred to is what the auto switch (os.Relax()) mostly resolved. Also, given the current Windows 10, a Go program's Windows clock setting shouldn't affect any other programs.

    I certainly like the goal of avoid using the Windows clock entirely, but I don't understand why os.Relax() hasn't been put back in use till the eventual goal is achieved.

  15. ChrisHines commented on Jul 5, 2023

    @ChrisHines
    Contributor

    I don't understand why os.Relax() hasn't been put back in use till the eventual goal is achieved.

    I can't answer that, but I think it's a valid question.

  16. johnrs commented on Jul 5, 2023

    @johnrs
    Author

    What's the procedure to make it happen? Open a new issue proposing it?

  17. ChrisHines commented on Jul 5, 2023

    @ChrisHines
    Contributor

    Perhaps. Or add a new comment to #44343 with your suggestion. I suspect a new issue would get marked as a duplicate of that one again but maybe it wouldn't if it was written as a proposal for a specific change rather than a problem report.

  18. johnrs commented on Jul 6, 2023

    @johnrs
    Author

    Thanks. Will post on #44343 and see what happens.

  19. johnrs commented on Jul 13, 2023

    @johnrs
    Author

    @ChrisHines

    No response. What's the procedure for a proposal?

    Thanks.

  20. ChrisHines commented on Jul 14, 2023

    @ChrisHines
    Contributor

    @johnrs The procedure is documented here: https://github.com/golang/proposal

  21. johnrs commented on Jul 14, 2023

    @johnrs
    Author

    Thanks!

  22. jxqu3 commented on Oct 29, 2023

    @jxqu3

    I'm working in an autoclicker in Windows 11 and I'm having a similar problem. If I execute mousedown and mouseup without a time.Sleep, I get thousands of clicks per second, but as soon as I add a time.Sleep, even if it's time.Sleep(1 * time.Nanosecond) it goes down to 66 clicks per second, which is about 15.15ms of sleep.

  23. johnrs commented on Oct 29, 2023

    @johnrs
    Author
    /*
    init() switches the Windows system clock from 15.6ms to 1ms.
    @ In the future (v1.22): golang.org/x/sys/windows.TimeBeginPeriod
    */
    func init() {
    	winmmDLL := syscall.NewLazyDLL("winmm.dll")
    	procTimeBeginPeriod := winmmDLL.NewProc("timeBeginPeriod")
    	procTimeBeginPeriod.Call(uintptr(1))
    }
    
  24. locked and limited conversation to collaborators on Oct 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.OS-Windowscompiler/runtimeIssues related to the Go compiler and/or runtime.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions