Coroutines do not unwind the stack in case of errors

Back to how it was, a coroutine does not unwind its stack in case of
errors (and therefore do not close its to-be-closed variables). This
allows the stack to be examined after the error. The program can
use 'coroutine.kill' to close the variables.

The function created by 'coroutine.wrap', however, closes the
coroutine's variables in case of errors, as it is impossible to examine
the stack any way.
This commit is contained in:
Roberto Ierusalimschy
2019-05-09 11:13:45 -03:00
parent 01bded3d8c
commit 389116d8ab
6 changed files with 80 additions and 26 deletions

View File

@@ -734,18 +734,24 @@ a, b = coroutine.resume(co, 100)
assert(a and b == 30)
-- check traceback of suspended coroutines
-- check traceback of suspended (or dead with error) coroutines
function f(i)
if i == 0 then error(i)
else coroutine.yield(); f(i-1)
end
end
function f(i) coroutine.yield(i == 0); f(i - 1) end
co = coroutine.create(function (x) f(x) end)
a, b = coroutine.resume(co, 3)
t = {"'coroutine.yield'", "'f'", "in function <"}
repeat
while coroutine.status(co) == "suspended" do
checktraceback(co, t)
a, b = coroutine.resume(co)
table.insert(t, 2, "'f'") -- one more recursive call to 'f'
until b
end
t[1] = "'error'"
checktraceback(co, t)