resque / resque/resque-scheduler
Delayed job processing loop uses wall time to calculate remaining sleep seconds
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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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