3

I'm using luajit2.0.4 as lua interpreter. here is the situation:

after I load a dynamic lib like cjson.so with

cjson = require('cjson')
a=cjson.new() -- blahblah... do things i want

then I need to overwrite the lib file, ( a situation of hot upgrade, and here cjson.so_1 and cjson.so might be the same)

[root@localhost lib]# cp cjson.so_1 cjson.so
cp: overwrite `cjson.so'? y

it will force kernel to truncate the physical memory of cjson.so's pmap, and causing a page missing then when I call

a=cjson.new()

again, the kernel will recopy the cjson.so lib into memory, this time not parsing the global symbols. so when I call new() which need to call external functions like malloc(), a segment fault will occur.

I've already found a way to reload by writing a unload funtion in C,

and my question is can I do this by lua code itself?

Knova Chan
  • 135
  • 8
  • From _[this conversation](http://lua.2524044.n2.nabble.com/How-to-reload-C-functions-added-with-quot-require-quot-td7585583.html)_ it appears that re-loading a library in lua requires using C. The link provides some example code on how the problem was approached, but it did require using C. – ryyker Nov 15 '17 at 13:49
  • @ryyker yes, that's the temporary solution I mentioned – Knova Chan Nov 15 '17 at 13:56
  • The answer to your question _can I do this by lua code itself?_ is no. evidently, it us because `lua` is an interpreted language. U,e, its library is _in use_, so attempting to unload the library by calling `lua` syntax that is being interpreted and executed by the library at that time is not possible. – ryyker Nov 15 '17 at 14:25

1 Answers1

2

Sadly, this is not possible.

You'd have to approach the situation using C/C++, but not using Lua, due to the way in which Lua works, and I wish there would've been a different answer but Lua has got it's limits.

SleepyMode
  • 120
  • 11
  • yes, and maybe we should commit a patch for lua to solve this – Knova Chan Nov 17 '17 at 06:22
  • @KnovaChan - Your suggestion suggests you still do not understand _why_ this cannot be done. Can you sit in a chair with you feet off of the ground, then remove the chair? or can you remove your shoes when you are standing on them? Replacing any executable by calling one of it's libraries, or functions, while it is being used is the same. – ryyker Nov 17 '17 at 15:59
  • @ryyker thx again, but I still think that as long as it can be done in C, we can always wrap it and make an API for lua to invoke right? In fact, when we want to unload a library, we certainly won't be using it any longer. You can take a look at the link you give in your first comment, if that code is in origin lua core, then unloadlib can be done in pure lua. – Knova Chan Nov 18 '17 at 08:25