Skip to content

setTimeout callback is executed sooner than it should in docker on Mac OS #26155

Closed

Description

@Jokero
  • Node version: 10.15.1
  • Node docker image: node:10.15.1
  • OS: Mac OS 10.14.3 (18D109)
  • Platform: Darwin dmitry 18.2.0 Darwin Kernel Version 18.2.0: Thu Dec 20 20:46:53 PST 2018; root:xnu-4903.241.1~1/RELEASE_X86_64 x86_64
  • Docker: engine - 18.09.1, machine - 0.16.1
  • Subsystem: timers

Let's assume I have the following script which I want to run inside a docker container:

const date = new Date();
setTimeout(() => console.log(new Date() - date, new Date(), date), 3000);
setTimeout(() => console.log(new Date() - date, new Date(), date), 3000);
setTimeout(() => console.log(new Date() - date, new Date(), date), 3000);
setTimeout(() => console.log(new Date() - date, new Date(), date), 3000);
setTimeout(() => console.log(new Date() - date, new Date(), date), 3000);

What the official documentation says about setTimeout (https://nodejs.org/en/docs/guides/timers-in-node):

The timeout interval that is set cannot be relied upon to execute after that exact number of milliseconds. /* ... */ The only guarantee is that the timeout will not execute sooner than the declared timeout interval

Ok, timeout should not be executed sooner than the declared timeout interval.

However, after several script executions (it was 8th attempt) I got the following confusing result:

2975 2019-02-16T20:06:41.410Z 2019-02-16T20:06:38.435Z
2985 2019-02-16T20:06:41.420Z 2019-02-16T20:06:38.435Z
2987 2019-02-16T20:06:41.422Z 2019-02-16T20:06:38.435Z
2987 2019-02-16T20:06:41.422Z 2019-02-16T20:06:38.435Z
2987 2019-02-16T20:06:41.422Z 2019-02-16T20:06:38.435Z

setTimeout handlers were executed sooner than 3000 milliseconds.

I was not able to reproduce it on host (Mac OS, Ubuntu) and in docker on Ubuntu, it happens only in docker on Mac OS (haven't checked on Windows).

Activity

  1. added
    macos
    timers
    on Feb 16, 2019
  2. bnoordhuis commented on Feb 17, 2019

    @bnoordhuis
    Member

    There could be one of two things going on here:

    1. Docker somehow slews (compresses) the monotonic system time used for timers.
    2. The real-time clock (what new Date() uses) drifts inside the docker instance.

    Either way, I suspect this isn't actually a Node.js bug but behavior that can also be observed with e.g. a python script or a C program.

    It's possible that Node.js is more likely to trigger it as it prefers CLOCK_MONOTONIC_COARSE (when it's available and granular enough) to the more common CLOCK_MONOTONIC.

  3. Jokero commented on Feb 18, 2019

    @Jokero
    Author

    We can update the documentation by adding the link to this issue. I really didn't expect that this might happen.

    Now:

    The only guarantee is that the timeout will not execute sooner than the declared timeout interval.

    Suggestion:

    The only guarantee is that the timeout will not execute sooner than the declared timeout interval (there is an <link>issue</link> inside docker container on Mac OS).

  4. bnoordhuis commented on Feb 18, 2019

    @bnoordhuis
    Member

    We don't usually list third-party software bugs in our documentation. Have you checked if there's an open docker bug about this? If not, it might be worthwhile to report it over there.

  5. gireeshpunathil commented on Jan 6, 2020

    @gireeshpunathil
    Member

    excerpts from the connected issue:

    Unfortunately it's a known issue on macOS with Hypervisor.framework that the wall-clock in the VM drifts relative to the host at about 2ms for every 3s. We work around this by moving the wall-clock forwards every 30s ...
    https://engineering.docker.com/2019/02/addressing-time-drift-in-docker-desktop-for-mac/

    closing this based on the above, not an issue with node.js

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

    macostimers

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions