Why does Ruby's EventMachine.defers_finished?() sometimes return true when not all defers are complete?

StackOverflow https://stackoverflow.com/questions/18302423

Question

Sample code:

require 'eventmachine'

t = Thread.new { EM.run }

op = proc {
  op2 = proc {
    sleep 1
    puts 'op2 finishing'
  }
  cb2 = proc {
    sleep 1
    puts 'cb2 finishing'
  }
  EM.defer(op2, cb2)

  puts 'op finishing'
}
cb = proc {
  puts 'cb finishing'
}

EM.defer(op, cb)

while (!EM.defers_finished?) do
  puts 'defers not finished, sleeping...'
  sleep 1
end

EM.stop

About 10% of the time the above program will fail with:

eventmachine-1.0.3/lib/eventmachine.rb:1039:in `signal_loopbreak': eventmachine not initialized: evma_signal_loopbreak (RuntimeError)
eventmachine-1.0.3/lib/eventmachine.rb:1039:in `block in spawn_threadpool'

It appears that EM.defers_finished?() is sometimes returning true even though there are still outstanding defers. I am new to EventMachine so perhaps am doing this wrong, but as it stands it seems like something is not behaving correctly with EventMachine.

Some background as to why I'm not doing a typical "EM.run do", this is running under a Passenger process and as such you can't start EventMachine in the usual fashion. See http://www.railstips.org/blog/archives/2011/05/04/eventmachine-and-passenger/ and http://www.hiringthing.com/2011/11/04/eventmachine-with-rails.html

Was it helpful?

Solution

Here link, it says you need this line:

# Wait for the reactor to start
while not EM.reactor_running?; end

..and your error seems to indicate you are missing something like that:

eventmachine not initialized
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top