How to gracefully shutdown a thread in Ruby

1k views Asked by At

I have been experimenting multi-threading concept in Ruby for the past a week.

For practising, I am designing a file downloader that makes parallel requests for a collection of URLs. Currently I need to safely shutdown threads when interrupt signal is triggered. I have read the theory of multi-threading and catching a signal at runtime. Yet despite the whole those theoretical knowledge, I still don't have any idea about how to use them in practice.

I am leaving my proof of concept work below, anyhow.

class MultiThread
  attr_reader :limit, :threads, :queue

  def initialize(limit)
    @limit   = limit
    @threads = []
    @queue   = Queue.new
  end

  def add(*args, &block)
    queue << [block, args]
  end

  def invoke
    1.upto(limit).each { threads << spawn_thread }
    threads.each(&:join)
  end

  private

  def spawn_thread
    Thread.new do
      Thread.handle_interrupt(RuntimeError => :on_blocking) do
        # Nothing to do
      end

      until queue.empty?
        block, args = queue.pop
        block&.call(*args)
      end
    end
  end
end

urls = %w[https://example.com]
thread = MultiThread.new(2)

urls.each do |url|
  thread.add do
    puts "Downloading #{url}..."
    sleep 1
  end
end

thread.invoke
1

There are 1 answers

1
Jared Beck On

Yeah, the docs for handle_interrupt are confusing. Try this, which I based on the connection_pool gem used by e.g. puma.

$stdout.sync = true

threads = 3.times.map { |i|
  Thread.new {
    Thread.handle_interrupt(Exception => :never) do
      begin
        Thread.handle_interrupt(Exception => :immediate) do
          puts "Thread #{i} doing work"
          sleep 1000
        end
      ensure
        puts "Thread #{i} cleaning up"
      end
    end
  }
}

Signal.trap("INT")  {
  puts 'Exiting gracefully'
  threads.each { |t|
    puts 'killing thread'
    t.kill
  }
  exit
}

threads.each { |t| t.join }

Output:

Thread 1 doing work
Thread 2 doing work
Thread 0 doing work
^CExiting gracefully
killing thread
killing thread
killing thread
Thread 0 cleaning up
Thread 1 cleaning up
Thread 2 cleaning up