• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!
  • 2026 staff recruitment is open! Check it out and consider applying!

LuaJIT Breaking Change

fusion32

Intermediate OT User
Joined
Jun 7, 2010
Messages
39
Solutions
2
Reaction score
113
For anyone using LuaJIT, there has been a breaking change with commit 5c64775. In summary, finalizers (__gc metamethod) now run on a separate lua thread, which causes the lua state for these callbacks to be different from the main lua state. This is only really a problem if the interfacing code assumes the lua state is always the same, which is the case for the OTClient.

For a concrete example on the OTClient, LuaInterface::luaCppFunctionCallback is used as the wrapping callback anytime you use LuaInterface::pushCppFunction, which is then used when setting the __gc metamethod for classes declared with LuaInterface::registerClass.
Code:
int LuaInterface::luaCppFunctionCallback(lua_State *L){
    void *funcPtr = g_lua.popUpvalueUserdata();             // fails in finalizers: assumes L == g_lua.L
    void *funcPtr = lua_touserdata(L, lua_upvalueindex(1)); // works in finalizers
    assert(funcPtr != NULL);
    //...
}

I have created an issue (#1420) on the LuaJIT issue tracker but it's unlikely that this change will be reversed, since it was dismissed as bad design (which I don't completely disagree). The simplest solution would be to use some version from before the problematic commit (which is from Nov 10 2025). Another solution would be to have specific callbacks for finalizers which only access the explicit lua state parameter. And the most obvious but more involved solution would be to avoid assuming the lua state for any callbacks.

Anyway, I'm not gonna try to guess which solution is better. I just figured I should make this problem known before repos like VCPKG on Windows or APT on Debian/Ubuntu get an updated version and all of a sudden stuff starts breaking.

PS: I haven't tested it with TFS or OT but from a quick glance, their callbacks don't assume the lua state, so it wouldn't apply then.
 
Back
Top