Skip to content

Commit e9526d4

Browse files
committed
Address FileThreadPool race condition
I saw a couple ThreadErrors around popping from an empty queue in bugsnag: this got introduced when I moved away from catching all ThreadErrors. There's a race condition between checking that the queue is empty & attempting to pop from it. This change locks the pop operation with a mutex to prevent the race.
1 parent 67b9919 commit e9526d4

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

lib/cc/engine/analyzers/file_thread_pool.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def run(&block)
1717

1818
@workers = thread_count.times.map do
1919
Thread.new do
20-
while !queue.empty? && (item = queue.pop(true))
20+
while item = next_item(queue)
2121
yield item
2222
end
2323
end
@@ -32,6 +32,11 @@ def join
3232

3333
attr_reader :files, :concurrency, :workers
3434

35+
def next_item(queue)
36+
@lock ||= Mutex.new
37+
@lock.synchronize { queue.pop(true) unless queue.empty? }
38+
end
39+
3540
def build_queue
3641
Queue.new.tap do |queue|
3742
files.each do |file|

0 commit comments

Comments
 (0)