Skip to content

Commit

Permalink
Merge pull request sidekiq#2285 from eugenk/2284-handle_circular_erro…
Browse files Browse the repository at this point in the history
…r_causes

Handle circular error causes
  • Loading branch information
seuros committed Apr 8, 2015
2 parents 91919a0 + 4fa9fa8 commit 7c9b159
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/sidekiq/middleware/server/retry_jobs.rb
Expand Up @@ -188,12 +188,16 @@ def retry_in(worker, count)
end
end

def exception_caused_by_shutdown?(e)
def exception_caused_by_shutdown?(e, checked_causes = [])
# In Ruby 2.1.0 only, check if exception is a result of shutdown.
return false unless defined?(e.cause)

# Handle circular causes
checked_causes << e.object_id
return false if checked_causes.include?(e.cause.object_id)

e.cause.instance_of?(Sidekiq::Shutdown) ||
exception_caused_by_shutdown?(e.cause)
exception_caused_by_shutdown?(e.cause, checked_causes)
end

end
Expand Down
40 changes: 40 additions & 0 deletions test/test_retry.rb
Expand Up @@ -328,6 +328,46 @@ def @redis.multi; yield self; end
File.read(@tmp_log_path), 'Log entry missing for sidekiq_retry_in')
end
end

describe 'handles errors withouth cause' do
before do
@error = nil
begin
raise ::StandardError, 'Error'
rescue ::StandardError => e
@error = e
end
end

it "does not recurse infinitely checking if it's a shutdown" do
assert(!Sidekiq::Middleware::Server::RetryJobs.new.send(
:exception_caused_by_shutdown?, @error))
end
end

describe 'handles errors with circular causes' do
before do
@error = nil
begin
begin
raise ::StandardError, 'Error 1'
rescue ::StandardError => e1
begin
raise ::StandardError, 'Error 2'
rescue ::StandardError => e2
raise e1
end
end
rescue ::StandardError => e
@error = e
end
end

it "does not recurse infinitely checking if it's a shutdown" do
assert(!Sidekiq::Middleware::Server::RetryJobs.new.send(
:exception_caused_by_shutdown?, @error))
end
end
end

end

0 comments on commit 7c9b159

Please sign in to comment.