lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


I was trying to determine if updating a module, while a Lua VM is running, can ever be done safely.

 

There is no problem if a script is entirely in 1 file. Just write to disk and then the next load make everything current.

 

A problem can occur when a script is in 2 or more files.

Assume that some thread periodically writes new module updates to disk, 1 module at a time.

Assume module “A” requires module “B”

 

Whether the modules exist on disk, in memory or in a database, in a dynamic language it is always possible that:

1)      Module “A” has started executing

2)      The thread updates module “B” to disk, sets package.loaded["B"] = nil

3)      Lua VM executes “require ‘B’” line, loads module “B”

4)      Module “A” has some mismatch (params, etc) because “A” is not updated which causes an exception

5)      Thread updates module “A” to disk, sets package.loaded["A"] = nil

6)      On next call to module “A”, Lua VM loads “A”

7)      Now everything is up to date and everything is safe again

 

It seems to me that this cannot be made safe, even if mutexes were used between the thread writing the modules to disk and the loader loading modules. For example, the loader would have to build a call tree of every module required by A and this may not be possible to evaluate without interpreting.

 

Or am I making this problem too hard and there is a simple way to synchronize writes to disk and the loading of “required” modules?

 

John Rodriguez