EM::Synchrony.defer with fiber aware database call causes FiberError exception

508 views Asked by At

I'm trying to use EM-Synchrony for concurrency in an application and have come across an issue with my use of deferred code and Fibers.

Any calls to the database within either EM.defer or EM::Synchrony.defer results in the application crashing with the error can't yield from root fiber

Below is a very trimmed down runnable example of what I'm trying to accomplish. The first print works and displays [:first, 1] but the second is where I crash with the error mentioned above.

require 'mysql2'
require 'em-synchrony/activerecord'
ActiveRecord::Base.establish_connection(
  :adapter => 'em_mysql2',
  :username => 'user',
  :password => 'pass',
  :host => 'localhost',
  :database => 'app_dev',
  :pool => 60
)
class User < ActiveRecord::Base; end
EM.synchrony do
  p [:first, User.all.count]
  EM::Synchrony.defer do
    p [:second, User.all.count]
  end
end

My first thought was perhaps the Fiber.current and Fiber.yield within EM::Synchrony.defer meant I could fix the problem with an extra Fiber.new call

EM::Synchrony.defer do
  Fiber.new do
    p [:second, User.all.count]
  end.resume
end

This fails to run as well but this time I get the error fiber called across threads.

0

There are 0 answers