Fiber Stack Errors

Tony Arcieri edited this page Aug 8, 2013 · 13 revisions

You're probably here because you've seen an error like this:

E, [2012-11-16T00:02:21.657925 #44055] ERROR -- : MyActor crashed!
Celluloid::FiberStackError: stack level too deep (please see https://github.com/celluloid/celluloid/wiki/Fiber-stack-errors)
/Users/tony/src/celluloid/lib/celluloid/tasks/task_fiber.rb:49:in `rescue in resume'
/Users/tony/src/celluloid/lib/celluloid/tasks/task_fiber.rb:46:in `resume'

There are a few things that could be causing this error. There are several possible explanations depending on what Ruby VM you are using:

MRI: You are exceeding the stack depth available inside a Fiber

tl;dr: use MRI 2.0, JRuby, or Rubinius.

Celluloid uses Fibers to implement its [pipelined execution model](Pipelining and execution modes). Unfortunately, in Ruby 1.9.x, each Fiber is only afforded 4kB of stack space, which is extremely small.

This has been remedied in Ruby 2.0 where the Fiber stack size is now 128kB on 64-bit platforms (or 64kB on 32-bit platforms). The stack size is also configurable at VM boot time, using the RUBY_FIBER_VM_STACK_SIZE and RUBY_FIBER_MACHINE_STACK_SIZE environment variables.

On Ruby 1.9, there are a few workarounds:

  • Use TaskThreads instead of TaskFibers: To do this, add task_class TaskThread to your Celluloid-based classes. This will use a true thread with a full-size stack for each task instead of a fiber. This has more overhead but avoids the 4kB stack limit.
  • Use defer to run code in a background thread: For stack-heavy code, the defer keyword can be used to summon a background thread from Celluloid's thread pool to execute something that can't fit inside a fiber
  • Use the exclusive class method to mark a method exclusive: When you use the exclusive class method, Celluloid will run that method inside the parent thread instead of wrapping it in a Fiber. This sidesteps the Fiber stack size issues.

MRI: You are experiencing a compile-time misconfiguration

If your code seems to work on your local development machine but blows up with a FiberStackError elsewhere, you may be experiencing a bug with the way your Ruby was compiled. If you are using RVM, update to the latest release (instructions) and reinstall Ruby. Updating to the latest Ruby patch release is also recommended.

Adhearsion-specific note

If you're experiencing this exception in an Adhearsion application, please see the specific notes relevant to Adhearsion for instructions.

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.