lua-users home
lua-l archive

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


I had a similar problem in the past: killing a running Lua process in a safe way from another OS process. 
It was solved by hooking a function into the interpreter that hooked itself again and issued and error until it finally ended the task.

    lua_State *mainthread = G(L)->mainthread;
    lua_sethook( mainthread, fullstop, LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT, 1 );

extern "C" void fullstop( lua_State *L, lua_Debug *ar )
{
    (void)ar;  /* unused arg. */
    lua_sethook( L, fullstop, LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT, 1 );
    luaL_error( L, "stopped");
}

Hope that or a variant thereof helps you.

Oliver


Am 25.02.21 um 21:58 schrieb bel:

I need to create a Lua API function to close/exit a Lua state from a Lua script.
While “os.exit” will exit the entire host application, I just want to “exit” one (of multiple) Lua states and return to the “lua_pcall” in C, while the application (and the other states) keep running.

Something like this:

my_C_function()
    lua_newstate
    lua_pcall -->
        Some Lua code
        Lua pcall -->
            more Lua code
            call "state exit" function -------------|
            Lua code that should not be executed    |
        More Lua code that should not be executed   |
   
[continue here]  <------------------------------|
    lua_close


It cannot be just a luaL_error call, since this will be caught by a pcall in the Lua code.

Is there some recommended method to do this?
It should work with Lua 5.2, 5.3 and 5.4 and use only C and Lua (no C++, no operating system dependent code or CPU dependent code like asm).

I could try to build some setjmp/longjmp code around it, but I’m wondering if there might be some built-in feature, e.g., some Lua error/exceptions that can only be handled in C but not in Lua? In my case, I will call "lua_close" immediately after and I don't need to resume any operation in this state. But for curiosity, is there a way to keep the state "fully operational" after such an operation?