[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: luaclose_mylib function (inverse of "luaopen_mylib" C function)
- From: Philippe Verdy <verdyp@...>
- Date: Fri, 28 Aug 2020 16:37:51 +0200
The bad thing is that the registry maintains the finalizer only as a single ordered list and not according to a dependency tree. This means that libraries cannot be unloaded if another unrelated library was registered after it and actually does not depend on the library we'd liek to unload.
How can we maintain a dependency tree (and no longer depend on the finalizer ordered list) ?
Using finalizers seems the wrong way to do that. a single finalizer should be used for the whole dependency tree (when terminating the whole Lua instance itself), but there's a need to develop better registry maintaining a dependency graph (possibly with reference counters, but this is not necessarily the best approach as it can be broken easily, causing a library to be unloaded prematurely when it is still in use): this should better be part of the GC, possibly using "weak references" for libraries that can be easily unloaded when needed and reloaded later possibly with their own persistant external storage (some DB interface or external network REST API?) for some data needed to restore its previous state.
>>>>> "bel" == bel <email@example.com> writes:
bel> Is there any way the library could ever be unloaded, before the
bel> Lua state is destroyed?
What Lua does with dynamically loaded libraries is this: there is a
table stored in the registry which contains the pathnames and handles of
all dynamically opened objects, and this table has a __gc metamethod
that closes all of those handles. Since this is in the registry and has
had its finalizer set before loading any library, and since finalizers
are called in reverse order of setting, this means that libraries won't
be unloaded until after any finalizers that they set themselves have
run, if I understand things correctly.
There is no way to "unrequire" a module short of closing the state
(unless you do really dangerous things in C code, like replacing the
registry table or the CLIBS table in the registry, or calling its __gc
If there are multiple Lua states, it's up to the OS facilities being
used to load dynamic libraries to do the necessary reference-counting so
that a library opened multiple times is not unloaded until all
references are closed.