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

Not thread-safe #19

Open
jamespeerless opened this issue Mar 24, 2018 · 0 comments
Open

Not thread-safe #19

jamespeerless opened this issue Mar 24, 2018 · 0 comments

Comments

@jamespeerless
Copy link

Multiple jobs can still be scheduled within the perform_in interval if being scheduled in a multithreaded environment.

Here is some sample code to reproduce issue:

# create a simple debounced worker
class DebouncedWorkerJob
  include Sidekiq::Worker
  sidekiq_options debounce: true

  def perform(id)
    puts "performing work with #{id}"
  end
end
  
# var on main thread so threads block until flipped
wait_for_it  = true
# significant enough concurrency to force issue
concurrency_level = 10
# store all jids from perform_in calls
jids = []

# initialize threads
threads = concurrency_level.times.map do |i|
  Thread.new do
    # block until flag is flipped
    true while wait_for_it
    # schedule debounced job, expectation is all but 1 return nil
    jids[i] = DebouncedWorkerJob.perform_in(10.seconds, 1)
  end
end

# flip flag so all threads unblock
wait_for_it = false
# wait for threads to finish
threads.each(&:join)

# count how many scheduled job ids we got back
scheduled_jobs = jids.reduce(0) { |acc, jid| jid.present? ? acc + 1 : acc }

# print out how many were scheduled, routinely returns 2 or 3 jobs scheduled even with debounce enabled
puts "Scheduled #{scheduled_jobs} jobs, expected 1"
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