ezmobius/redis-rb — Issue #183: Synchrony / Fiber concurrent issue
Author: fonzo14
Created: 2012-02-25 10:14:51+00:00
State: open

Hi,

It looks like the synchrony driver isn't "fiber safe". In a very concurrent context, fibers are receiving a redis response from a concurrent fiber. I reproduce that behaviour with this little script:

require 'em-synchrony'
require 'redis/connection/synchrony'
require 'redis'

EM.synchrony do

redis = Redis.new

proc = Proc.new do
EM.add_timer(0.001) { proc.call }
#EM.next_tick { proc.call }
Fiber.new do
fiber_id = Fiber.current.object_id
redis.set "fiber.#{fiber_id}", fiber_id
f_id = redis.get("fiber.#{fiber_id}")
p [:fiber_ids, fiber_id, f_id]
end.resume
end

proc.call

#EM.stop
end

Here is the output :

....
[:fiber_ids, 78343020, "78343020"]
[:fiber_ids, 78481940, "OK"]
[:fiber_ids, 78484590, "78484590"]
[:fiber_ids, 78483590, "78483590"]
....
[:fiber_ids, 78478320, "78478320"]
[:fiber_ids, 78477270, "78477270"]
[:fiber_ids, 78342560, "OK"]
[:fiber_ids, 78341500, "78341500"]
[:fiber_ids, 78339550, "OK"]
[:fiber_ids, 78338470, "78338470"]

So, sometimes, the "get" code is receiving the response from the set "code" : "OK"

If I simulate a very very concurrent context using next_tick, the script output looks like this :

[:fiber_ids, 80650660, "OK"]
[:fiber_ids, 80649130, "OK"]
[:fiber_ids, 80468040, "80649130"]
[:fiber_ids, 80465970, "OK"]
[:fiber_ids, 80464380, "OK"]
[:fiber_ids, 80390570, "OK"]
[:fiber_ids, 80388770, "OK"]
[:fiber_ids, 80386860, "OK"]
[:fiber_ids, 80384990, "OK"]

Not sure about what happens here, maybe @igrigorik can give me a hint.

I have this problem on master and 2.2.2