Question
Can Lua safely unload a required module for zero‑downtime updates?
Mira Nea
0 reputation · 25 Nov 2021, 15:45 UTC
135.6K views0
Goal
Enable a small Lua application to migrate to new code without stopping the process, by reloading modules at runtime.
Constraints and uncertainty
requirecaches modules inpackage.loaded, so subsequent calls return the original instance even after source changes.- Setting
package.loaded[mod] = nilforces a reload, but this technique is undocumented and may interfere with LuaJIT’s JIT compilation. - The
debug.reloadfunction exists only in some distributions and is not part of the core API, limiting portability. - No standard mechanism exists to unload a module that holds global resources or participates in circular dependencies.
Which approach guarantees that all references to the old module are released? Does clearing package.loaded suffice on LuaJIT without leaving stale JIT code? What portable pattern can be used across Lua 5.4 and LuaJIT?