{"record":{"id":"33fa9b8c5d9d8d2f","repo":"microsoft/garnet","slug":"could-not-initialize-lua-vm","errorCode":null,"errorMessage":"Could not initialize Lua VM","messagePattern":"Could not initialize Lua VM","errorType":"exception","errorClass":"GarnetException","httpStatus":null,"severity":"critical","filePath":"libs/server/Lua/LuaRunner.cs","lineNumber":341,"sourceCode":"                ((byte)redisVersionParsed.Minor << 8) |\n                ((byte)redisVersionParsed.Build << 0);\n            state.PushInteger(redisVersionNum);\n            if (!state.TrySetGlobal(\"garnet_REDIS_VERSION_NUM\\0\"u8))\n            {\n                throw new GarnetException(\"Insufficient space in Lua VM for redis version number global\");\n            }\n\n            var loadRes = state.LoadBuffer(PrepareLoaderBlockBytes(allowedFunctions, logger).Span);\n            if (loadRes != LuaStatus.OK)\n            {\n                if (state.StackTop == 1 && state.Type(1) == LuaType.String)\n                {\n                    state.KnownStringToBuffer(1, out var buff);\n                    var innerError = Encoding.UTF8.GetString(buff);\n                    throw new GarnetException($\"Could not initialize Lua VM: {innerError}\");\n                }\n\n                throw new GarnetException(\"Could not initialize Lua VM\");\n            }\n\n            var sandboxRes = state.PCall(0, -1);\n            if (sandboxRes != LuaStatus.OK)\n            {\n                string errMsg;\n                try\n                {\n                    if (state.StackTop >= 1)\n                    {\n                        // We control the definition of LoaderBlock, so we know this is a string\n                        state.KnownStringToBuffer(1, out var errSpan);\n                        errMsg = Encoding.UTF8.GetString(errSpan);\n                    }\n                    else\n                    {\n                        errMsg = \"No error provided\";\n                    }","sourceCodeStart":323,"sourceCodeEnd":359,"githubUrl":"https://github.com/microsoft/garnet/blob/951b0fc6838721f89d102c2bbe1b914e8d39d700/libs/server/Lua/LuaRunner.cs#L323-L359","documentation":"The same LoadBuffer failure path as the detailed variant, but the Lua stack did not contain a string error message (state.StackTop != 1 or state.Type(1) != LuaType.String), so Garnet cannot extract a cause. It is the fallback case: the sandbox failed to load but Lua provided no message.","triggerScenarios":"Identical construction path - state.LoadBuffer(PrepareLoaderBlockBytes(...)) returns non-OK - but the stack state does not match the (StackTop == 1 && Type(1) == String) shape, so no innerError is available.","commonSituations":"Same build/runtime mismatch as the detailed variant; also seen when the Lua state is left in an unexpected shape by a prior failed operation on the same runner.","solutions":["Same as the detailed variant: ensure a consistent, clean Garnet build.","Inspect the Lua state's stack depth/type at the point of failure (add diagnostics) to recover a cause.","Rule out a corrupted or partially-initialized Lua state being reused."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"catch (GarnetException ex) when (ex.Message == \"Could not initialize Lua VM\") {\n    logger.LogCritical(ex, \"Bare Lua init failure with no detail; rebuild/redeploy Garnet\");\n    throw;\n}","preventionTips":["Treat any 'Could not initialize Lua VM' as a fatal build/deploy inconsistency and fail the session/startup.","Avoid reusing a LuaRunner after a prior initialization error; create a fresh state."],"tags":["lua","scripting","initialization","version-mismatch"],"backgroundTag":null,"analyzedSha":"951b0fc6838721f89d102c2bbe1b914e8d39d700","analyzedAt":"2026-08-13T19:01:32.939Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}