package.loaded invalidation during Lua 5.1 to 5.4 migration without downtime
0 reputation · 16 Jan 2024, 04:05 UTC
0 reputation · 16 Jan 2024, 04:05 UTC
The goal is to update module source in a running Lua 5.4 interpreter without stopping the process, by clearing entries in package.loaded and calling require again.
The constraint is that Lua caches compiled modules in package.loaded, and removing an entry only forces the next require to re-execute the file. It does not invalidate closures, coroutines, or already compiled bytecode that captured functions from the previous load. Migration from Lua 5.1 to 5.4 also introduces compatibility boundaries such as removed setfenv and changed load semantics, so the same source may behave differently across versions.
It is unclear whether module reloading through package.loaded is safe for long-running coroutines, or whether zero-downtime migration requires running Lua 5.1 and 5.4 interpreters side by side and switching traffic at the host or proxy level.
Does clearing package.loaded and re-requiring a module leave existing coroutines bound to the old implementation? Can a Lua 5.1 application migrate to Lua 5.4 without downtime using only module-table invalidation, or is side-by-side VM traffic switching necessary?
35275 reputation · 16 Jan 2024, 10:48 UTC
package.loaded and calling require again only forces Lua to re-execute the source file upon the next call. It does not invalidate existing closures, coroutines, or variables that have already captured functions from the previous load. If a coroutine is currently executing or holds a reference to a function from the old module, it will continue using the old logic even after the module is 'reloaded.'
setfenv and changed the semantics of load. A module written for 5.1 may fail or behave unpredictably in a 5.4 environment regardless of how it is loaded.To achieve zero-downtime migration, you must use a side-by-side VM traffic switching strategy at the host or proxy level:
Diagnostic: Does your application rely heavily on global state or long-running coroutines that must maintain their execution context across the version transition?
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.