Skip to content

os.uptime returns float instead of integer #41636

Description

@davedodo

Version

v14.18.3

Platform

Linux 5.4.0 #1 SMP Thu Apr 22 16:18:59 MSK 2021 x86_64 x86_64 x86_64 GNU/Linux

Subsystem

os

What steps will reproduce the bug?

I just upgraded from Node.js 12 to 14 (on Ubuntu via nodesource repository) and found that require("os").uptime() now returns a float and no longer an integer as the documentation states. Tested on Ubuntu 18.04 on VirtualBox and Ubuntu 20.04 on a Virtuozzo container. So this is either a bug or the documentation needs an update.

How often does it reproduce? Is there a required condition?

No response

What is the expected behavior?

os.uptime should return an integer.

What do you see instead?

os.uptime returns a number that is not an integer.

Additional information

No response

Activity

  1. richardlau commented on Jan 21, 2022

    @richardlau
    Member

    Some history #12291.

  2. added
    osIssues and PRs related to the os subsystem.
    on Jan 21, 2022
  3. lal12 commented on Jan 28, 2022

    @lal12
    Contributor

    Yes I've noticed this too. There was a change in libuv (libuv/libuv@77a2394) which now reads /proc/uptime instead of using clock_gettime(CLOCK_MONOTONIC and /proc/uptime contains a fractional output. So either this behavior should be changed or the docs should be adjusted to say that it is a fractional number on some systems. I would prefer fractional seconds, since a math.floor is easily doable and a higher precision might be useful sometimes.

  4. lal12 commented on Jan 28, 2022

    @lal12
    Contributor

    I created an additional issue for this in libuv libuv/libuv#3447

  5. davedodo commented on Jan 30, 2022

    @davedodo
    Author

    @lal12 You are correct. Funny, this change is related to bug report #36244 I filed in 2020 because clock_gettime doesn't work properly inside OpenVZ/Virtuozzo containers. Fractional seconds are cool, but it would be a breaking change. 🤔

  6. lal12 commented on Jan 31, 2022

    @lal12
    Contributor

    It depends a bit on how you define a breaking change, there is no datatype change so nothing what totally breaks. But yeah it can lead to strange behavior if someone relies on an integer. I found this issue since some number formatting code of ours relied on integers.

    However for quite a while windows has returned fractionals in this API, which also was changed.

  7. lal12 commented on Feb 2, 2022

    @lal12
    Contributor

    libuv already returned fractional uptime for some architecture, just did a PR there to return fractions on win. So changing this to round down would be a "breaking" change too, especially since some versions are already released with this behavior. I really think updating the docs accordingly is best.

    I can do a PR if wished for.

  8. added
    libuvIssues and PRs related to the libuv dependency or the uv binding.
    on Feb 2, 2022
  9. santigimeno commented on Nov 5, 2022

    @santigimeno
    Member

    This should be fixed as #42340 has landed

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

    libuvIssues and PRs related to the libuv dependency or the uv binding.osIssues and PRs related to the os subsystem.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions