Skip to content

clock_gettime(CLOCK_MONOTONIC, timespec &tp) sometimes sets invalid values for tp #3840

Closed

Description

  • Your Windows build number: Microsoft Windows [Version 10.0.17134.523]

  • WSL Linux Distribution: Ubuntu 16.04.5 LTS (official package installed via Windows Store)

  • What you're doing and what's happening:
    Create a file clock.c:

#include <stdio.h>
#include <time.h>

int main(int argc, char** argv) {
  struct timespec now;
  int ret;
  ret = clock_gettime(CLOCK_MONOTONIC, &now);
  printf("ret: %i\n", ret);
  printf("after tv_sec: %ld\n", now.tv_sec);
  printf("after tv_nsec: %ld\n", now.tv_nsec);
}

In a WSL bash.exe terminal run:
gcc clock.c
./a.out

Normally you will see something like this:

ret: 0
tv_sec: 385
tv_nsec: 449802000

However sometimes you will see something like this instead:

ret: 0
tv_sec: -2975346
tv_nsec: -412345678

(the specific values above are examples, the key point is they are negative numbers significantly less than 0)

Note that if you run the same command some seconds later, the values will have changed moving closer to 0:

ret: 0
tv_sec: -2975341
tv_nsec: -12345678

I have not had the patience to wait for the number to return to 0 to see what happens next.

This problem does not just affect this toy example.

Example 1:
In WSL bash.exe:

  1. sudo apt-get install ruby.
  2. irb
  3. sleep 1
    Normal behaviour:
2.5.1 :001 > sleep 1
 => 1

Behaviour when issue occurs:
Ruby interpreter crashes referencing pthread_cond_timedwait EINVAL.

Example 2:
In WSL bash.exe:

  1. ssh -v -v -v -v -v <any valid hostname that allows ssh connections>.
  2. A large stream of debug messages will be shown, but at the end you will see a message like "%s: chan %d: skip for %d more seconds" where %d is a large positive integer.
  3. ssh session never finishes opening, so you never get a terminal prompt etc.

I have checked the source code of both ruby and openssh-client, and I can trace the errors back to usages of clock_gettime(CLOCK_MONOTONIC) and then the values being uses assuming that tv_sec and tv_nsec are non-negative.

  • What's wrong / what should be happening instead:
    Sometimes, the clock_gettime syscall with clockid_t CLOCK_MONTONIC, will fill out the timespec fields tv_sec and tv_nsec with negative (signed) integers, instead of (signed) integers >= 0.

I am not 100% if the relevant spec specifies this requirement exactly, but in practice, many applications assume that the tv_sec and tv_nsec fields filled in by clock_gettime(CLOCK_MONTONIC, &now) will be non-negative integers.

Normally the value of these fields for this clock seems to be the length of time since the LxssManager Windows service has been started, this seems like OK behaviour to keep in all circumstances.

When this problem occurs, there is a workaround:

  1. Close all open instances of bash.exe (i.e. any applications using WSL)
  2. On the Windows host, type Win+R, type services.msc, press Enter.
  3. Locate LxssManager and restart the service.
  4. Open bash.exe and rerun any command that relies on the value of clock_gettime(CLOCK_MONTONIC, &now), and see that the values are now sane.

FYI my Windows host system had been up for 34 days and 7 hours when this issue occured, so perhaps there is some connection to a long system uptime and this issue occuring.

This is NOT the same issue as #506 OR #1023 as this error occurs with CLOCK_MONTONIC, not CLOCK_MONTONIC_RAW.
This is NOT the same issue as #2503 as that is referencing CLOCK_REALTIME, not CLOCK_MONTONIC.

Activity

  1. benhillis commented on Feb 12, 2019

    @benhillis
    Member

    Thanks for filing, I suspect you are hitting #3434 and variants. To confirm, the issue should go away when you reboot your machine (or restart the lxssmanager service).

  2. yecril71pl commented on May 3, 2019

    @yecril71pl

    This issue makes SeaMonkey (or any other gthread-based application) crash because the makers of glib assumed that monotonic time cannot be negative (which is sort of documented, albeit no too explicitly).
    Note that a clock that wraps is not a monotonic clock.

  3. therealkenc commented on May 3, 2019

    @therealkenc

    dupe #3434

  4. yecril71pl commented on May 3, 2019

    @yecril71pl

    dupe #3434

    #3434 is about CLOCK_BOOTTIME.

  5. therealkenc commented on May 3, 2019

    @therealkenc

    What is your Win10 ver.

  6. yecril71pl commented on May 4, 2019

    @yecril71pl

    Win10 ver. 1803

  7. therealkenc commented on May 4, 2019

    @therealkenc

    The fix-in tag was 1809

  8. yecril71pl commented on May 4, 2019

    @yecril71pl

    That fact, while very entertaining, does not make this one a duplicate.

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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions