Skip to content

Fiber#alive? returns true even when already finished #4838

@satosho

Description

@satosho

It looks to be related to #1789 and #1949.

Environment

jruby 9.1.13.0 (2.3.3) 2017-09-06 8e1c115 Java HotSpot(TM) 64-Bit Server VM 25.151-b12 on 1.8.0_151-b12 +jit [mswin32-x86_64]
Windows 10 Home

Expected Behavior

Fiber#alive? returns false for all the calls in the following code.

require "fiber"

def check(&block)
  1000.times.map do
    fiber = Fiber.new(&block)
    fiber.resume
    fiber.alive?
  end
end

results = [
  check {},
  check { "Text" },
  check { Fiber.new {} },
  check { sleep(0.001) },
]

puts ["", "FALSE", "TRUE"].join("\t")
results.each_with_index do |r, i|
  puts ["test#{i}", r.count(false), r.count(true)].join("\t")
end

On CRuby:

$ ruby check_fiber_alive.rb
        FALSE   TRUE
test0   1000    0
test1   1000    0
test2   1000    0
test3   1000    0

Actual Behavior

On JRuby, the results can be true.

$ jruby check_fiber_alive.rb
        FALSE   TRUE
test0   998     2
test1   999     1
test2   288     712
test3   987     13

The code sometimes runs into NullPointerException.

$ jruby check_fiber_alive.rb
Unhandled Java exception: java.lang.NullPointerException
java.lang.NullPointerException: null
                 alive at org/jruby/ext/fiber/ThreadFiber.java:243
             __alive__ at org/jruby/ext/fiber/ThreadFiber.java:229
                  call at org/jruby/ext/fiber/ThreadFiber$INVOKER$i$0$0$__alive__.gen:-1
                  call at org/jruby/internal/runtime/methods/AliasMethod.java:56
                  call at org/jruby/runtime/callsite/CachingCallSite.java:129
  invokeOther16:alive? at check_fiber_alive.rb:7
        block in check at check_fiber_alive.rb:7
           yieldDirect at org/jruby/runtime/CompiledIRBlockBody.java:156
                 yield at org/jruby/runtime/BlockBody.java:114
                 yield at org/jruby/runtime/Block.java:165
                  call at org/jruby/RubyEnumerable.java:846
         yieldSpecific at org/jruby/runtime/CallBlock19.java:77
         yieldSpecific at org/jruby/runtime/Block.java:134
                 times at org/jruby/RubyFixnum.java:299
                  call at org/jruby/RubyFixnum$INVOKER$i$0$0$times.gen:-1
                  call at org/jruby/internal/runtime/methods/JavaMethod.java:498
               finvoke at org/jruby/RubyClass.java:522
                invoke at org/jruby/runtime/Helpers.java:395
            callMethod at org/jruby/RubyBasicObject.java:393
                  each at org/jruby/RubyEnumerator.java:323
                  call at org/jruby/RubyEnumerator$INVOKER$i$each.gen:-1
               finvoke at org/jruby/RubyClass.java:512
                invoke at org/jruby/runtime/Helpers.java:383
            callEach19 at org/jruby/RubyEnumerable.java:116
         collectCommon at org/jruby/RubyEnumerable.java:838
                   map at org/jruby/RubyEnumerable.java:830
                  call at org/jruby/RubyEnumerable$INVOKER$s$0$0$map.gen:-1
             callBlock at org/jruby/runtime/callsite/CachingCallSite.java:139
                  call at org/jruby/runtime/callsite/CachingCallSite.java:145
     invokeOther20:map at check_fiber_alive.rb:4
                 check at check_fiber_alive.rb:4
                  call at org/jruby/internal/runtime/methods/CompiledIRMethod.java:90
          cacheAndCall at org/jruby/runtime/callsite/CachingCallSite.java:328
             callBlock at org/jruby/runtime/callsite/CachingCallSite.java:141
                  call at org/jruby/runtime/callsite/CachingCallSite.java:145
   invokeOther28:check at check_fiber_alive.rb:15
                <main> at check_fiber_alive.rb:15
   invokeWithArguments at java/lang/invoke/MethodHandle:-1
                  load at org/jruby/ir/Compiler.java:95
             runScript at org/jruby/Ruby.java:828
           runNormally at org/jruby/Ruby.java:747
           runNormally at org/jruby/Ruby.java:765
           runFromMain at org/jruby/Ruby.java:578
         doRunFromMain at org/jruby/Main.java:417
           internalRun at org/jruby/Main.java:305
                   run at org/jruby/Main.java:232
                  main at org/jruby/Main.java:204

Workaround:
Fiber#alive? looks to return false as expected by adding a short sleep between resume and alive? calls.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions