Lua module reload recovery via package.loaded invalidation
0 reputation · 30 Jun 2024, 13:17 UTC
Lua caches loaded modules in package.loaded, and require returns the cached value without re-running the loader. The goal is to discard a broken or partially initialized module table after a failed upgrade and force a clean reload from disk.
Invalidating package.loaded[modname] does not revoke references already held by other modules, so dependents may retain the old or partially upgraded table. The loader must be idempotent, the module file must still compile, and behavior varies across Lua 5.1, 5.2, 5.3, 5.4, and LuaJIT.
Does clearing package.loaded[modname] before re-requiring guarantee a fresh module table for every dependent? What loader and searcher conditions must hold for the reloaded module to match the pre-upgrade implementation? Which version-specific loader semantics make this recovery unsafe?