From 669084d1d042f778c4de217ee8335ffd4a8b0277 Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Thu, 26 Oct 2023 13:40:37 -0300 Subject: [PATCH] fix(lua) avoid LuaJIT issue with FFI callback roundtrips The Kong Gateway CI started seeing intermittent crashes with "bad callback" errors: See: https://luajit.freelists.narkive.com/sdhSLJSr/how-to-make-bad-callback-more-deterministic ``` The bad callback error happens because some JIT-compiled Lua code calls a C function which in turn calls an FFI callback. ``` https://lua-l.lua.narkive.com/qXJrNlpP/luajit-ffi-windows-bad-callback-error-in-msgwaitformultipleobjects-proof-of-concept From Mike Pall: ``` The problem is that a FFI callback cannot safely be called from a C function which is itself called via the FFI from JIT-compiled code. In your case this is the call to MsgWaitForMultipleObjects. I've put in a lot of heuristics to detect this, and it usually succeeds in disabling compilation for such a function. However in your case the loop is compiled before the callback is ever called, so the detection fails. The straighforward solution is to put the message loop into an extra Lua function and use jit.off(func) ``` See --- lib/resty/wasmx/proxy_wasm.lua | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/resty/wasmx/proxy_wasm.lua b/lib/resty/wasmx/proxy_wasm.lua index 7934c4fd6..ebbf8037c 100644 --- a/lib/resty/wasmx/proxy_wasm.lua +++ b/lib/resty/wasmx/proxy_wasm.lua @@ -113,7 +113,9 @@ do lval = ffi_str(cvalue.data, cvalue.len) end + jit.off() local pok, ok, lvalue, is_const = pcall(lua_prop_setter, lkey, lval) + jit.on() if not pok then ngx.log(ngx.ERR, "error setting property from Lua: ", ok) return FFI_ERROR @@ -133,7 +135,9 @@ do cvalue) local lkey = ffi_str(ckey.data, ckey.len) + jit.off() local pok, ok, lvalue, is_const = pcall(lua_prop_getter, lkey) + jit.on() if not pok then ngx.log(ngx.ERR, "error getting property from Lua: ", ok) return FFI_ERROR