Maintenance Ruby 3.3 gets fixes, but no new features. Switch to Ruby 4.0 →
instance method · added in Ruby 3.3
Fiber#kill
fiber.kill → nil
cont.c
View on GitHub ↗
static VALUE
rb_fiber_m_kill(VALUE self)
{
rb_fiber_t *fiber = fiber_ptr(self);
if (fiber->killed) return Qfalse;
fiber->killed = 1;
if (fiber->status == FIBER_CREATED) {
fiber->status = FIBER_TERMINATED;
}
else if (fiber->status != FIBER_TERMINATED) {
if (fiber_current() == fiber) {
fiber_check_killed(fiber);
} else {
fiber_raise(fiber_ptr(self), Qnil);
}
}
return self;
}
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
-
3.3
-
Introduce
Fiber#kill. [Bug #595]Examplefiber = Fiber.new do while true puts "Yielding..." Fiber.yield end ensure puts "Exiting..." end fiber.resume # Yielding... fiber.kill # Exiting...
-