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

setTimeout delayed when sleeping on Windows #6763

Closed
seishun opened this issue May 14, 2016 · 12 comments
Closed

setTimeout delayed when sleeping on Windows #6763

seishun opened this issue May 14, 2016 · 12 comments
Labels
libuv Issues and PRs related to the libuv dependency or the uv binding. timers Issues and PRs related to the timers subsystem / setImmediate, setInterval, setTimeout. windows Issues and PRs related to the Windows platform.

Comments

@seishun
Copy link
Contributor

seishun commented May 14, 2016

  • Version: v6.1.0 and master
  • Platform: Windows 10 64-bit
  • Subsystem: timers

Testcase:

console.log(Date());
setTimeout(function() {
    console.log(Date());
}, 20000);

When running it normally, the result is as expected:

Sat May 14 2016 23:20:36 GMT+0300 (FLE Daylight Time)
Sat May 14 2016 23:20:56 GMT+0300 (FLE Daylight Time)

However, if you do the following:

  1. Start the script.
  2. Put the computer to sleep ASAP.
  3. Resume after 10 seconds.

The timeout fires not after the remaining 10 seconds, but after ~20 seconds:

Sat May 14 2016 23:22:14 GMT+0300 (FLE Daylight Time)
Sat May 14 2016 23:22:45 GMT+0300 (FLE Daylight Time)

I've asked on IRC and people say that this doesn't happen on Linux. That is, the callback is not delayed unless it would have occurred during sleep, in which case it's fired as soon as the computer wakes up.

@mscdex mscdex added timers Issues and PRs related to the timers subsystem / setImmediate, setInterval, setTimeout. windows Issues and PRs related to the Windows platform. labels May 14, 2016
@bnoordhuis
Copy link
Member

Libuv on Linux uses CLOCK_BOOTTIME when available (kernel >= 2.6.39). Windows doesn't have an equivalent with good enough precision, AFAIK.

@seishun
Copy link
Contributor Author

seishun commented May 15, 2016

Doesn't libuv on Linux simply use the timeout parameter of epoll_wait, just like it does on Windows with IOCP?

@bnoordhuis
Copy link
Member

I take back what I said. I thought CLOCK_BOOTTIME was the clock source for timers but it's the clock source for uptime calculations only.

@seishun
Copy link
Contributor Author

seishun commented May 15, 2016

Anyway, apparently GetQueuedCompletionStatus doesn't advance the timeout while sleeping. Testcase:

#include "Windows.h"
#include <ctime>
#include <iostream>
#include <iomanip>

int main()
{
  auto whatever = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 0);
  OVERLAPPED_ENTRY entries[10];

  auto t = std::time(nullptr);
  auto tm = *std::localtime(&t);
  std::cout << std::put_time(&tm, "%d-%m-%Y %H-%M-%S") << std::endl;

  ULONG bleh;
  GetQueuedCompletionStatusEx(whatever, entries, 10, &bleh, 20000, FALSE);

  // try sleeping here

  t = std::time(nullptr);
  tm = *std::localtime(&t);
  std::cout << std::put_time(&tm, "%d-%m-%Y %H-%M-%S") << std::endl;

}

I'm not sure if there is an alternative that works properly. Pinging @piscisaureus

@seishun
Copy link
Contributor Author

seishun commented May 17, 2016

Well what do you know. I cannot reproduce this on Windows 7. Either I've discovered an obscure bug in Windows 10, or there's something funky going on with my Windows 10 machine.

@seishun
Copy link
Contributor Author

seishun commented May 17, 2016

@nodejs/collaborators @nodejs/platform-windows if anyone could try to reproduce this and post their results that would be great.

@Fishrock123 Fishrock123 added the libuv Issues and PRs related to the libuv dependency or the uv binding. label May 17, 2016
@iakat
Copy link

iakat commented May 18, 2016

I can reproduce this issue on my desktop and laptop; both running Windows 10 (Pro & Enterprise), on v6.1.0.

@seishun
Copy link
Contributor Author

seishun commented May 29, 2016

Pinging @orangemocha

<@saghul> maybe @ orangemocha, maybe he can ask someone on the Windows team at Microsoft

@orangemocha
Copy link
Contributor

I don't know the answer from the top of my head, but I'll follow up on this one. Thank you for bringing it to my attention.

@seishun
Copy link
Contributor Author

seishun commented Jul 5, 2016

@orangemocha have you found out anything?

@orangemocha
Copy link
Contributor

I haven't investigated it yet, sorry. Bumping it.

bzoz added a commit to JaneaSystems/libuv that referenced this issue Aug 1, 2016
When Windows resumes after sleep GetQueuedCompletionStatus timeout is
not updated. This commit adds a method for signaling all loops to
wake up and update their timers.

Fixes: nodejs/node#6763
@seishun
Copy link
Contributor Author

seishun commented Aug 12, 2016

Reopening since this is still an issue in node.js until libuv is updated.

@seishun seishun reopened this Aug 12, 2016
saghul added a commit to saghul/libuv that referenced this issue Aug 18, 2016
When Windows resumes after sleep GetQueuedCompletionStatus timeout is
not updated. This commit adds a method for signaling all loops to
wake up and update their timers.

Fixes: nodejs/node#6763
PR-URL: libuv#962
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Saúl Ibarra Corretgé <saghul@gmail.com>
cjihrig added a commit to cjihrig/node that referenced this issue Oct 26, 2016
Fixes: nodejs#4351
Fixes: nodejs#6763
Refs: nodejs#8280
PR-URL: nodejs#9267
Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: James M Snell <jasnell@gmail.com>
evanlucas pushed a commit that referenced this issue Nov 3, 2016
Fixes: #4351
Fixes: #6763
Refs: #8280
PR-URL: #9267
Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: James M Snell <jasnell@gmail.com>
MylesBorins pushed a commit that referenced this issue May 16, 2017
Fixes: #4351
Fixes: #6763
Refs: #8280
PR-URL: #9267
Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: James M Snell <jasnell@gmail.com>
MylesBorins pushed a commit that referenced this issue May 18, 2017
Fixes: #4351
Fixes: #6763
Refs: #8280
PR-URL: #9267
Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: James M Snell <jasnell@gmail.com>
andrew749 pushed a commit to michielbaird/node that referenced this issue Jul 19, 2017
Fixes: nodejs/node#4351
Fixes: nodejs/node#6763
Refs: nodejs/node#8280
PR-URL: nodejs/node#9267
Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: James M Snell <jasnell@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
libuv Issues and PRs related to the libuv dependency or the uv binding. timers Issues and PRs related to the timers subsystem / setImmediate, setInterval, setTimeout. windows Issues and PRs related to the Windows platform.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants