From 8511e90b7df5daf139aba290b9c9c1595927e7b3 Mon Sep 17 00:00:00 2001 From: Roberto I Date: Wed, 22 Jul 2026 14:42:55 -0300 Subject: Bug: GC checks stack space before running finalizer If some stack does not have a 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. --- lgc.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'lgc.c') diff --git a/lgc.c b/lgc.c index c01660ab..52cea240 100644 --- a/lgc.c +++ b/lgc.c @@ -1242,7 +1242,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->tobefnz != NULL && !g->gcemergency && luaD_checkminstack(L)) callallpendingfinalizers(L); } @@ -1632,11 +1632,12 @@ static lu_mem singlestep (lua_State *L) { break; } case GCScallfin: { /* call remaining finalizers */ - if (g->tobefnz && !g->gcemergency) { + if (g->tobefnz && !g->gcemergency && luaD_checkminstack(L)) { g->gcstopem = 0; /* ok collections during finalizers */ work = runafewfinalizers(L, GCFINMAX) * GCFINALIZECOST; } - 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 */ work = 0; } -- cgit v1.2.3-55-g6feb