FiberError
RubyERRORNotableConcurrency

Invalid Fiber operation

Quick Answer

Track fiber state before resuming; never resume a fiber that has already returned from its block.

What this means

Raised when an illegal operation is performed on a Fiber, such as resuming a dead fiber, resuming a fiber from a different thread, or calling Fiber.yield outside of a fiber.

Why it happens
  1. 1Calling Fiber#resume on a fiber that has already finished
  2. 2Calling Fiber.yield outside of any running fiber
  3. 3Resuming a fiber from a thread other than the one that created it

Fix

Check fiber state before resuming

Check fiber state before resuming
fiber = Fiber.new { Fiber.yield 1; Fiber.yield 2 }
fiber.resume   # => 1
fiber.resume   # => 2
fiber.resume   # => nil (fiber finished, no more yields)
# A 4th resume would raise FiberError

Why this works

A fiber returns nil on the resume after its block completes — track this to avoid over-resuming.

Code examples
Resuming a dead fiberruby
f = Fiber.new { 1 }
f.resume   # => 1
f.resume   # FiberError: dead fiber called
Fiber.yield outside fiberruby
Fiber.yield
# FiberError: can't yield from main fiber
Rescue FiberErrorruby
begin
  result = fiber.resume
rescue FiberError => e
  puts "Fiber problem: #{e.message}"
end
Sources
Official documentation ↗

Ruby Core Documentation

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All Ruby errors