Issue #6822 has been updated by ko1 (Koichi Sasada).

Category changed from YARV to core
Status changed from Assigned to Closed

In general, you can sync with variables because Fibers are not changed automatically.  In other words, you can completely control Fiber transition.

----------------------------------------
Bug #6822: Race Condition with Fiber and Process
https://bugs.ruby-lang.org/issues/6822#change-29651

Author: MartinBosslet (Martin Bosslet)
Status: Closed
Priority: Normal
Assignee: ko1 (Koichi Sasada)
Category: core
Target version: 2.0.0
ruby -v: ruby 2.0.0dev (2012-05-07 trunk 35550) [x86_64-linux]


If I run the following code

    $stdout.sync = true
    objects = [1, 2, 3]

    fiber = Fiber.new do
      loop do
        objects.each { |obj| Fiber.yield(obj) }
      end
    end

    def run(obj)
      fork do
        puts obj
      end
    end

    def on_child_exit(obj)
      begin
        while Process.wait(-1, Process::WNOHANG)
          run(obj)
        end
      rescue Errno::ECHILD
      end
    end

    trap(:CHLD) { on_child_exit(fiber.resume) }
    4.times { run(fiber.resume) }
    sleep

I get
 
    fiber_process.rb:26:in `resume': double resume (FiberError)

or

    fiber_process.rb:26:in `resume': fiber called across stack rewinding barrier (FiberError)

There is a race condition when two or more children exit. Now I know I can implement
this differently, but this still made me curious. Is this a bug? Let's say I would 
need to use a Fiber, then there is no way how I can do the synchronization manually,
or is there? Using a Mutex to synchronize the Fiber#resume will fail due to the 
non-reentrant behaviour of Mutex#lock (I'll get "in `lock': deadlock; recursive 
locking (ThreadError)"). Is there a way to do this or should Fibers not be used in
this context? 





-- 
http://bugs.ruby-lang.org/