[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: <close> and os.exit()
- From: Francisco Olarte <folarte@...>
- Date: Fri, 29 May 2020 20:56:39 +0200
On Fri, May 29, 2020 at 7:38 PM Phil Leblanc <philanc@gmail.com> wrote:
> When os.exit() is called, by default the finalizer for <close>
> variables is not executed. If the second (optional) argument to
> os.exit() is true, then the Lua state is closed, and finalizers are
> executed.
> I suggest to change the default for the second exit() argument so that
> the default be to call finalizers upon exit.
> In special cases where the intent is ro _not_ call the finalizers, the
> optional argument to exit should be set to true.
> So the os.exit signature would be: os.exit ([code [, noclose]])
> and the behavior would be...
Not only will it not close, if I read the docs right it wont call __gc
finalizers either.
This is the documented behaviour in 5.3, I have not look further.
I'm not discussing wheter true or false is better as default
behaviour, but I think changing it is a backwards incompatible change
which has not enough advantages.
> In a way, it is the same logic as in C:
> exit(1);
> // the default behavior is to call the atexit()-registered functions.
> _exit();
> // In the special cases where atexit()-registered functions should
> // not be called, _exit() should be used instead of exit
But malloced() memory is not freed. And in C++ automatic storage
duration destructors are not caller ( behviour is quirky in C++, but
exit is not to be called lightly ).
If you depend on doing RAII like thingies ( <close> ), you should
probably employ a tactic similar to what you do in C++ for this cases
( raise an exit exception, catch it outside all your blocks, exit
there, this is easy to do with pcall/error ). And people may want to
exit fast, or exit instead of returning from a main loop precisely
because they do not want to have their finalizers called, and may have
it coded in many places.
And if you really dislike that, you can always do somehting like ( untested )
do
local o = os.exit
os.original_exit = o
os.exit = function(code, noclose) o(code, not noclose) end
end
or even
os.exit = function(code) o(code, true) end
os._exit = function(code) o(code, false) end
Francisco Olarte.