aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2025-06-13 14:08:38 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2025-06-13 14:08:38 -0300
commite657a48ea5698bbd9982d878eb65e6615ec94f7e (patch)
tree2a121476070495f31f9adb201aed7bdb2b48c75a
parentfd897027f19288ce2cb0249cb8c1818e2f3f1c4c (diff)
downloadlua-e657a48ea5698bbd9982d878eb65e6615ec94f7e.tar.gz
lua-e657a48ea5698bbd9982d878eb65e6615ec94f7e.tar.bz2
lua-e657a48ea5698bbd9982d878eb65e6615ec94f7e.zip
The main thread cannot be closed
No thread started with pcall (instead of resume) can be closed, because coroutine.close would not respect the expected number of results from the protected call.
-rw-r--r--lcorolib.c3
-rw-r--r--manual/manual.of4
-rw-r--r--testes/coroutine.lua5
3 files changed, 10 insertions, 2 deletions
diff --git a/lcorolib.c b/lcorolib.c
index 5b9736f1..23dd8441 100644
--- a/lcorolib.c
+++ b/lcorolib.c
@@ -190,6 +190,9 @@ static int luaB_close (lua_State *L) {
190 } 190 }
191 } 191 }
192 case COS_RUN: /* running coroutine? */ 192 case COS_RUN: /* running coroutine? */
193 lua_geti(L, LUA_REGISTRYINDEX, LUA_RIDX_MAINTHREAD); /* get main */
194 if (lua_tothread(L, -1) == co)
195 return luaL_error(L, "cannot close main thread");
193 lua_closethread(co, L); /* close itself */ 196 lua_closethread(co, L); /* close itself */
194 lua_assert(0); /* previous call does not return */ 197 lua_assert(0); /* previous call does not return */
195 return 0; 198 return 0;
diff --git a/manual/manual.of b/manual/manual.of
index 7c504d97..baa33d88 100644
--- a/manual/manual.of
+++ b/manual/manual.of
@@ -3284,8 +3284,8 @@ If @id{L} is equal to @id{from},
3284it corresponds to a thread closing itself. 3284it corresponds to a thread closing itself.
3285In that case, 3285In that case,
3286the call does not return; 3286the call does not return;
3287instead, the resume or the protected call 3287instead, the resume that (re)started the thread returns.
3288that (re)started the thread returns. 3288The thread must be running inside a resume.
3289 3289
3290} 3290}
3291 3291
diff --git a/testes/coroutine.lua b/testes/coroutine.lua
index 02536ee5..4881d964 100644
--- a/testes/coroutine.lua
+++ b/testes/coroutine.lua
@@ -158,6 +158,11 @@ do
158 158
159 local main = coroutine.running() 159 local main = coroutine.running()
160 160
161 -- cannot close 'main'
162 local st, msg = pcall(coroutine.close, main);
163 assert(not st and string.find(msg, "main"))
164
165
161 -- cannot close a "normal" coroutine 166 -- cannot close a "normal" coroutine
162 ;(coroutine.wrap(function () 167 ;(coroutine.wrap(function ()
163 local st, msg = pcall(coroutine.close, main) 168 local st, msg = pcall(coroutine.close, main)