lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hi all,

I might be mistaken, but I believe I found a bug in Lua 5.2.2 with
`coroutine.resume` (even after applying all patches). When calling it
with the current coroutine (i.e. passing `coroutine.running()`) it
seems to break the running coroutine. I'm aware that the call itself
makes no practical sense, but it even returns an appropriate error
message ("cannot resume non-suspended coroutine") so I don't think
breaking the state is what it should do. Here's the code to reproduce
the issue:

```
-- Minimal test-case (I think)
print(coroutine.resume(coroutine.create(function()
  coroutine.resume(coroutine.running())
  coroutine.yield()
end)))
-- expected: true
-- actually: false   attempt to yield across a C-call boundary
```

Note that even after the call to `coroutine.resume` the coroutine is
not dead, however: `coroutine.status(coroutine.running())` still
reports "running".

A temporary workaround is to wrap `coroutine.resume` with a function
that checks for this particular case:

```
do
  local resume, running = coroutine.resume, coroutine.running
  function coroutine.resume(co, ...)
    if co == running() then
      return nil, "cannot resume non-suspended coroutine"
    end
    return resume(co, ...)
  end
end
```

Florian


--[[ Lua 5.1.5 does not seem to have this issue, it prints "true" as
expected for the test code. Lua 5.0.3 has no `coroutine.running`, but
storing it in an variable to get the equivalent code also works as
expected (i.e. it also prints "true"). ]]