From 597a53bbc681089d85b082b46c2e2428dec43b86 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 22 Dec 2021 09:00:52 -0300 Subject: Bug: finalizer calling exit can corrupt finalization order 'os.exit' can call lua_close again, separating new finalizers created after all previous finalizers were already separated. --- testes/main.lua | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'testes') diff --git a/testes/main.lua b/testes/main.lua index 52c77954..9def6386 100644 --- a/testes/main.lua +++ b/testes/main.lua @@ -261,6 +261,34 @@ u2 = setmetatable({}, {__gc = function () error("ZYX") end}) RUN('lua -W %s 2> %s', prog, out) checkprogout("ZYX)\nXYZ)\n") +-- bug since 5.2: finalizer called when closing a state could +-- subvert finalization order +prepfile[[ +-- should be called last +print("creating 1") +setmetatable({}, {__gc = function () print(1) end}) + +print("creating 2") +setmetatable({}, {__gc = function () + print("2") + print("creating 3") + -- this finalizer should not be called, as object will be + -- created after 'lua_close' has been called + setmetatable({}, {__gc = function () print(3) end}) + print(collectgarbage()) -- cannot call collector here + os.exit(0, true) +end}) +]] +RUN('lua -W %s > %s', prog, out) +checkout[[ +creating 1 +creating 2 +2 +creating 3 +nil +1 +]] + -- test many arguments prepfile[[print(({...})[30])]] -- cgit v1.2.3-55-g6feb