跟踪Ruby中的死锁

时间:2010-06-30 20:40:56

标签: ruby multithreading locking deadlock backtrace

我使用BrB来共享Ruby 1.9中各种工作进程的数据源,我使用Process#fork进行分叉,如下所示:

Thread.abort_on_exception = true

fork do
  puts "Initializing data source process... (PID: #{Process.pid})"
  data = DataSource.new(files)

  BrB::Service.start_service(:object => data, :verbose => false, :host => host, :port => port)
  EM.reactor_thread.join
end

工人分叉如下:

8.times do |t|  
  fork do
    data = BrB::Tunnel.create(nil, "brb://#{host}:#{port}", :verbose => false)

    puts "Launching #{threads_num} worker threads... (PID: #{Process.pid})"    

    threads = []
    threads_num.times { |i|
      threads << Thread.new {
        while true
          begin
            worker = Worker.new(data, config)

          rescue OutOfTargetsError
            break

          rescue Exception => e
            puts "An unexpected exception was caught: #{e.class} => #{e}"
            sleep 5

          end
        end
      }
    }
    threads.each { |t| t.join }

    data.stop_service
    EM.stop
  end
end

这非常完美,但运行大约10分钟后,我收到以下错误:

bootstrap.rb:47:in `join': deadlock detected (fatal)
        from bootstrap.rb:47:in `block in '
        from bootstrap.rb:39:in `fork'
        from bootstrap.rb:39:in `'

现在这个错误并没有告诉我很多关于死锁实际发生的地方,它只指向我在EventMachine线程上的连接。

如何追溯程序锁定的位置?

2 个答案:

答案 0 :(得分:5)

它锁定了父线程中的连接,该信息是准确的。 要跟踪它在子线程中锁定的位置,请尝试将线程的工作包装在timeout block 中。您需要暂时删除全部抢救以获取超时异常以进行提升。

目前,父线程尝试按顺序连接所有线程,阻塞直到每个线程完成。但是每个线程只会加入OutOfTargetsError。可以通过使用短期线程并将while循环移动到父级来避免死锁。没有保证,但也许是这样的?

8.times do |t|  
  fork do
    running = true
    Signal.trap("INT") do
      puts "Interrupt signal received, waiting for threads to finish..."
      running = false
    end

    data = BrB::Tunnel.create(nil, "brb://#{host}:#{port}", :verbose => false)

    puts "Launching max #{threads_num} worker threads... (PID: #{Process.pid})"    

    threads = []
    while running
      # Start new threads until we have threads_num running
      until threads.length >= threads_num do
        threads << Thread.new {
          begin
            worker = Worker.new(data, config)
          rescue OutOfTargetsError
          rescue Exception => e
            puts "An unexpected exception was caught: #{e.class} => #{e}"
            sleep 5
          end
        }
      end

      # Make sure the parent process doesn't spin too much
      sleep 1

      # Join finished threads
      finished_threads = threads.reject &:status
      threads -= finished_threads
      finished_threads.each &:join
    end

    data.stop_service
    EM.stop
  end
end

答案 1 :(得分:2)

我遇到了同样的问题,并使用此代码段解决了这个问题。

# Wait for all threads (other than the current thread and
# main thread) to stop running.
# Assumes that no new threads are started while waiting
def join_all
  main     = Thread.main       # The main thread
  current  = Thread.current    # The current thread
  all      = Thread.list       # All threads still running
  # Now call join on each thread
  all.each{|t| t.join unless t == current or t == main }
end

来源:Ruby编程语言,O'Reilly(2008)