Lua coroutines that allow nesting
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.

Build status Chat

Coroutines that allow nesting

Coroutines are a powerful notion to suspend and resume execution of lightweight threads. It can be used for instance for scheduling, as in copas, or for iteration.

When mixing these two uses, a problem arises: there is no way to specify which coroutine.resume should intercept a coroutine.yield.

This module provides a thin wrapper around the standard coroutine module to allow the definition of complex behaviors with nested coroutines.


This module is available in luarocks:

    luarocks install coronest


Everywhere you are using the standard Lua coroutine module, replace it with an instance of the nested coroutines:

    local coroutine = require "coroutine.make" ()

For an example, please look at examples/usage.lua.

Compatibility and Testing

Nested coroutines should be compatible with any version of Lua supporting coroutines (well, at least from 5.1). As the module is written in pure Lua, it also works with LuaJIT.

This module comes with some tests:

  • tests/nested.lua are important checks on the behavior of nested coroutines;
  • tests/lua-5.*.lua are tests imported from the Lua testsuite, but using coroutine.make instead of standard coroutines; they allow to check that nested coroutines do not break anything.


This module has been built after this discussion on StackOverflow. Even if Alban Linard (saucisson) owns this repository, the main contributor is Philipp Janda (siffiejoe).