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.
This commit is contained in:
Roberto Ierusalimschy
2021-12-22 09:00:52 -03:00
parent 86ec152433
commit 597a53bbc6
3 changed files with 34 additions and 5 deletions

View File

@@ -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])]]