10

我使用BrB为我 fork 的 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 <main>'
    from bootstrap.rb:39:in `fork'
    from bootstrap.rb:39:in `<main>'</pre>

这个错误并没有告诉我很多关于死锁实际发生在哪里,它只指向joinEventMachine 线程上的。

如何追溯程序锁定的时间点?

4

2 回答 2

5

它锁定join在父线程中,该信息是准确的。要跟踪它在子线程中的锁定位置,请尝试将线程的工作包装在一个timeoutblock中。您需要暂时删除 catch-allrescue以引发超时异常。

目前父线程尝试按顺序加入所有线程,阻塞直到每个线程完成。但是,每个线程只会加入一个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
于 2011-04-30T02:28:05.297 回答
3

我遇到了同样的问题并通过使用此代码段解决了它:

# 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)

于 2012-07-13T13:47:50.600 回答