Popping errors out from ruby threads using a Queue

Written by Ben Wendt

I had an issue at work where some database writes were throwing errors within a thread, and the error was being silently eaten when the thread died.

The solution is to use a queue for inter-thread communication, like this:

q = Queue.new

Thread.new do
    i = 1
    loop do
    begin
        sleep i
        i += 1
        raise 'whatever'

    rescue StandardError => e
        q << e
    end
    end
end

loop do
    puts q.size
end