Skip to content

JRuby starts executing Enumerator code too soon #4583

Description

@janko

Environment

$ jruby -v
jruby 9.1.8.0 (2.3.1) 2017-03-06 90fc7ab Java HotSpot(TM) 64-Bit Server VM 25.40-b25 on 1.8.0_40-b27 +jit [darwin-x86_64]
$ uname -a
Darwin Jankos-MacBook-Pro.local 16.4.0 Darwin Kernel Version 16.4.0: Thu Dec 22 22:53:21 PST 2016; root:xnu-3789.41.3~3/RELEASE_X86_64 x86_64

Expected Behavior

For the given code

enumerator = Enumerator.new do |yielder|
  yielder << nil
  puts "EXECUTED"
end

enumerator.next

MRI 2.4.1 doesn't output anything. I expected JRuby to behave the same, to pause and not to execute any code after yield, since we requested only the first element of the Enumerator.

Actual Behavior

However, JRuby outputs

EXECUTED

In other words, JRuby started executing the code after yield, even though we only asked for the first element.

Activity

  1. headius commented on May 3, 2017

    @headius
    Member

    The problem is that the thread started to power Enumerator#next does not pause after yielding the nil result to the yielder. It doesn't pause until the next iteration, because there's nobody waiting for that value. As a result, it executes past the yield.

    I believe I already fixed this for Fiber. What I didn't do is reimplement Enumerator#next in terms of Fiber, as MRI does.

  2. headius commented on May 3, 2017

    @headius
    Member

    If you are really just using this yielder pattern, you could swap in Fiber and it would work ok.

    fiber = Fiber.new {
      Fiber.yield nil
      puts "EXECUTED"
    }
    
    fiber.resume

    This will not output anything, because you only resume and get the first value, after which the Fiber properly transfers control back and waits.

  3. headius commented on May 3, 2017

    @headius
    Member

    More on reimplementing Enumerator#next in terms of Fiber...

    I have prototyped this before, and the amount of code required is very small (all implemented in Ruby). The piece that's missing is our short-circuited logic for when the data being enumerated comes from an Array.

    RubyEnumerator, implemented in Java, has at least two kinds of next logic: one that uses a pseudo-fiber to actually drive the nexting like a fiber, and one that just acts as a cursor into the given collection. The latter is significantly lighter-weight, both because it doesn't have to transfer values out of a fiber and because it doesn't have to spin up a native thread.

    If (when?) we move Enumerator#next to use a fiber, we will still want to keep the optimized versions for known collections.

  4. added this to the JRuby 9.2.0.0 milestone on May 3, 2017
  5. janko commented on May 4, 2017

    @janko
    Author

    Thanks you @headius!

  6. added a commit that references this issue on Jul 2, 2017
  7. added a commit that references this issue on Aug 18, 2017
  8. janko commented on Mar 3, 2018

    @janko
    Author

    I'm closing this one as a duplicate of #5007.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions