Skip to content

Maintenance Ruby 3.4 gets fixes, but no new features. Switch to Ruby 4.0 →

instance method · added in Ruby 3.3

Fiber#kill

() -> nil
fiber.kill → nil

Terminates the fiber by raising an uncatchable exception. It only terminates the given fiber and no other fiber, returning nil to another fiber if that fiber was calling resume or transfer.

Fiber#kill only interrupts another fiber when it is in Fiber.yield. If called on the current fiber then it raises that exception at the Fiber#kill call site.

If the fiber has not been started, transition directly to the terminated state.

If the fiber is already terminated, does nothing.

Raises FiberError if called on a fiber belonging to another thread.

Ruby version changes

  1. 3.3
    • Introduce Fiber#kill. [Bug #595]

      Example
      fiber = Fiber.new do
        while true
          puts "Yielding..."
          Fiber.yield
        end
      ensure
        puts "Exiting..."
      end
      
      fiber.resume
      # Yielding...
      fiber.kill
      # Exiting...
      
See also #inspect #raise #blocking? #resume also in 4.1 · 4.0 · 3.3