resque / resque/resque-scheduler

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

Open
#776 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Ruby
Stars
1.7k
Forks
477
PR merge metrics
No merged PRs in 30d

Description

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 (...)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in lib/resque/scheduler.rb at the poll_sleep_loop entry point around line 416, and inspect how elapsed and remaining sleep time are calculated. The change is done when polling duration is measured independently of wall-clock adjustments while preserving the requested sleep interval.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
backend
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.