Project

General

Profile

Actions

Bug #20413

closed

Enumerator can block fiber scheduler.

Added by ioquatix (Samuel Williams) about 1 month ago. Updated about 1 month ago.


Description

Using Enumerator in the event loop can cause problems as the fiber created by rb_fiber_new is blocking by default. It should be non-blocking.

#!/usr/bin/env ruby

require 'async'

Async do
  Async do
    while true
      puts "Hello"
      sleep 1
    end
  end
  
  enumerator = Enumerator.new do |yielder|
    while true
      yielder << "World"
      sleep 1
    end
  end
  
  while true
    puts enumerator.next
  end
end

Before this PR, the output is:

> ./test.rb
Hello
World
World
World
World
World
World
...

After this PR, the output is:

> ./test.rb
Hello
World
Hello
World
Hello
World
Hello
World
Hello
World
...

The reason why this happens, is because the enumerator sleep never yields to the event loop.

See https://github.com/ruby/ruby/pull/10481 for the fix.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0