X Tutup
Skip to content

asyncio.BaseEventLoop clock resolution becomes too low over time #145703

@CaptainFlint

Description

@CaptainFlint

Bug report

Bug description:

There have been a few discussions over this bit of code from Lib/asyncio/base_events.py:

        end_time = self.time()+ self._clock_resolution
        while self._scheduled:
            handle = self._scheduled[0]
            if handle._when >= end_time:
                break
            handle = heapq.heappop(self._scheduled)
            handle._scheduled = False
            self._ready.append(handle)

It has implicit dependency on the current system uptime. The clock resolution in my test environment was equal to 1e-09, and as soon as time.monotonic() (which is uptime on Linux systems) reaches 16M seconds (just over 194 days), the floating point precision shifts, and the addition of 1e-09 does not change the time value anymore. Which causes the comparison to become invalid, and the scheduled events not trigger at their exact designated time.

What if instead of adding the same fixed resolution value, we use an adaptive value that increases to match the current floating point precision?
I've looked through the discussions that seemed relevant, but didn't find any similar suggestions (although I could have missed them).

I'll create a pull request shortly to illustrate my idea.

Some of the relevant discussions:
#64704 (looks like the original addition of the current code)
#116342 (a very close hit; it looks to me, like the situation was misjudged a little in that dialog)
#110695 (not directly related to this issue, but also deals with clock resolution under specific scenario)

CPython versions tested on:

3.12

Operating systems tested on:

Linux

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytopic-asynciotype-bugAn unexpected behavior, bug, or error

    Projects

    Status

    Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      X Tutup