Project

General

Profile

Actions

Bug #20089

open

Fiber#kill transfers to root fiber

Added by rmosolgo (Robert Mosolgo) 11 months ago. Updated 2 months ago.

Status:
Assigned
Target version:
-
ruby -v:
ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [x86_64-darwin22]
[ruby-core:115911]

Description

I was hoping to use Fiber#kill to clean up formerly .transfer-d Fibers and work around https://bugs.ruby-lang.org/issues/20081, but I found that Fiber#kill has a similar control flow jump behavior. Is this on purpose, or a bug?

Here's a script to test the behavior:

manager = Fiber.new do
  worker = Fiber.new do
    puts "2. Begin Worker"
    manager.transfer
    puts "This should never print -- killed"
  end

  puts "1. Transfer to Worker"
  worker.transfer
  puts "3. Killing Worker"
  worker.kill
  puts "4. Finished manager"
end

manager.transfer
puts "5. Finished script"

I expected items 1 through 5 to be printed in order, but in fact, 4 is never printed:

$ ruby fiber_transfer_test.rb
1. Transfer to Worker
2. Begin Worker
3. Killing Worker
5. Finished script

It seems like worker.kill is transferring control to the top-level fiber instead of giving it back to manager.

I also tried having the thread kill itself, hoping it would return to the fiber that originally .transfered to it, but it also seems to jump out:

manager = Fiber.new do
  worker = Fiber.new do
    puts "2. Begin Worker"
    manager.transfer
    Fiber.current.kill
    puts "This should never print -- killed"
  end

  puts "1. Transfer to Worker"
  worker.transfer
  puts "3. Killing Worker"
  worker.transfer
  puts "4. Finished manager"
end

manager.transfer
puts "5. Finished script"

Prints:

1. Transfer to Worker
2. Begin Worker
3. Killing Worker
5. Finished script

Related issues 1 (0 open1 closed)

Related to Ruby master - Bug #20414: `Fiber#raise` should recurse to `resumed_fiber` rather than failing.Closedioquatix (Samuel Williams)Actions
Actions

Also available in: Atom PDF

Like1
Like0Like0Like0Like0Like0Like0Like0