GC checks stack space before running finalizer

If the stack does not have some minimum available space, the GC defers
calling a finalizer until the next cycle. That avoids errors while
running a finalizer that the programmer cannot control.
This commit is contained in:
Roberto I
2025-12-13 16:16:59 -03:00
parent 3d03ae5bd6
commit a5522f06d2
3 changed files with 16 additions and 3 deletions

7
lgc.c
View File

@@ -1293,7 +1293,7 @@ static void finishgencycle (lua_State *L, global_State *g) {
correctgraylists(g);
checkSizes(L, g);
g->gcstate = GCSpropagate; /* skip restart */
if (!g->gcemergency)
if (!g->gcemergency && luaD_checkminstack(L))
callallpendingfinalizers(L);
}
@@ -1667,12 +1667,13 @@ static l_mem singlestep (lua_State *L, int fast) {
break;
}
case GCScallfin: { /* call finalizers */
if (g->tobefnz && !g->gcemergency) {
if (g->tobefnz && !g->gcemergency && luaD_checkminstack(L)) {
g->gcstopem = 0; /* ok collections during finalizers */
GCTM(L); /* call one finalizer */
stepresult = CWUFIN;
}
else { /* emergency mode or no more finalizers */
else { /* no more finalizers or emergency mode or no enough stack
to run finalizers */
g->gcstate = GCSpause; /* finish collection */
stepresult = step2pause;
}