Skip to content

Commit 347451c

Browse files
committed
Call configured on_thread_error on thread errors when running async
For worker and dispatcher, on errors outside their thread pool/tasks.
1 parent 11b0848 commit 347451c

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

lib/solid_queue/processes/runnable.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def start
1414
end
1515

1616
if running_async?
17-
@thread = Thread.new { run }
17+
@thread = create_thread { run }
1818
else
1919
run
2020
end
@@ -73,5 +73,15 @@ def running_async?
7373
def running_as_fork?
7474
mode.fork?
7575
end
76+
77+
78+
def create_thread(&block)
79+
Thread.new do
80+
block.call
81+
rescue Exception => exception
82+
handle_thread_error(exception)
83+
raise
84+
end
85+
end
7686
end
7787
end

test/unit/worker_test.rb

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,30 @@ class WorkerTest < ActiveSupport::TestCase
2222
assert_equal({ "queues" => "background", "polling_interval" => 0.2, "thread_pool_size" => 3 }, process.metadata)
2323
end
2424

25-
test "errors on claiming executions are reported via Rails error subscriber regardless of on_thread_error setting" do
25+
test "errors on polling are passed to on_thread_error and re-raised" do
26+
errors = Concurrent::Array.new
27+
28+
original_on_thread_error, SolidQueue.on_thread_error = SolidQueue.on_thread_error, ->(error) { errors << error.message }
29+
previous_thread_report_on_exception, Thread.report_on_exception = Thread.report_on_exception, false
30+
31+
SolidQueue::ReadyExecution.expects(:claim).raises(RuntimeError.new("everything is broken")).at_least_once
32+
33+
AddToBufferJob.perform_later "hey!"
34+
35+
worker = SolidQueue::Worker.new(queues: "background", threads: 3, polling_interval: 0.2).tap(&:start)
36+
sleep(1)
37+
38+
assert_raises RuntimeError do
39+
worker.stop
40+
end
41+
42+
assert_equal [ "everything is broken" ], errors
43+
ensure
44+
SolidQueue.on_thread_error = original_on_thread_error
45+
Thread.report_on_exception = previous_thread_report_on_exception
46+
end
47+
48+
test "errors on claimed executions are reported via Rails error subscriber regardless of on_thread_error setting" do
2649
original_on_thread_error, SolidQueue.on_thread_error = SolidQueue.on_thread_error, nil
2750

2851
subscriber = ErrorBuffer.new

0 commit comments

Comments
 (0)