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

Delayed job processing loop uses wall time to calculate remaining sleep seconds #776

Open
Verseth opened this issue Aug 4, 2023 · 0 comments

Comments

@Verseth
Copy link

Verseth commented Aug 4, 2023

Looking through the codebase, I noticed that Time.now is used to calculate the remaining time to sleep between the iterations of the delayed job processing loop.

This could result in undefined behaviour, as Time.now reports wall time, which could change and get updated at any point.

This is the code I'm talking about: https://github.com/resque/resque-scheduler/blob/master/lib/resque/scheduler.rb#L416

def poll_sleep_loop
  @sleeping = true
  if poll_sleep_amount > 0
    start = Time.now
    loop do
      elapsed_sleep = (Time.now - start)
      remaining_sleep = poll_sleep_amount - elapsed_sleep
      # etc (...)

If the system's time was updated and moved backward one hour between the two calls to Time.now, the scheduler would sleep for one hour.

One way to mitigate this issue would be to use monotonic time to measure the remaining seconds to sleep like so:

def poll_sleep_loop
  @sleeping = true
  if poll_sleep_amount > 0
    start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
    loop do
      elapsed_sleep = (Process.clock_gettime(Process::CLOCK_MONOTONIC) - start)
      remaining_sleep = poll_sleep_amount - elapsed_sleep
      # etc (...)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant