{"record":{"id":"648834e1595ae906","repo":"microsoft/garnet","slug":"could-not-initialize-lua-sandbox-state-errmsg","errorCode":null,"errorMessage":"Could not initialize Lua sandbox state: {errMsg}","messagePattern":"Could not initialize Lua sandbox state: (.+?)","errorType":"exception","errorClass":"GarnetException","httpStatus":null,"severity":"critical","filePath":"libs/server/Lua/LuaRunner.cs","lineNumber":366,"sourceCode":"                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                    }\n                }\n                catch\n                {\n                    errMsg = \"Error when fetching pcall error\";\n                }\n\n                throw new GarnetException($\"Could not initialize Lua sandbox state: {errMsg}\");\n            }\n\n            state.GetGlobal(LuaType.Table, \"sandbox_env\\0\"u8);\n            if (!state.TryRef(out sandboxEnvRegistryIndex))\n            {\n                throw new GarnetException(\"Insufficient space in VM for sandbox_env ref\");\n            }\n\n            state.GetGlobal(LuaType.Function, \"load_sandboxed\\0\"u8);\n            if (!state.TryRef(out loadSandboxedRegistryIndex))\n            {\n                throw new GarnetException(\"Insufficient space in VM for load_sandboxed ref\");\n            }\n\n            state.GetGlobal(LuaType.Function, \"reset_keys_and_argv\\0\"u8);\n            if (!state.TryRef(out resetKeysAndArgvRegistryIndex))\n            {\n                throw new GarnetException(\"Insufficient space in VM for reset_keys_and_argv ref\");","sourceCodeStart":348,"sourceCodeEnd":384,"githubUrl":"https://github.com/microsoft/garnet/blob/951b0fc6838721f89d102c2bbe1b914e8d39d700/libs/server/Lua/LuaRunner.cs#L348-L384","documentation":"Thrown when the sandbox/loader block loaded successfully but errored while executing: state.PCall(0, -1) (running the loader block to set up sandbox_env, load_sandboxed, etc.) returned non-OK. errMsg is the protected-call error extracted from the stack (or 'No error provided' / 'Error when fetching pcall error' if extraction failed). This means the sandbox setup code itself ran and failed, distinct from a compile failure of the loader.","triggerScenarios":"LuaRunner construction reaches the state.PCall(0, -1) line after a successful LoadBuffer, and the protected call fails. errMsg may be the real Lua message, 'No error provided' (stack empty), or 'Error when fetching pcall error' (extraction threw).","commonSituations":"Loader-block/sandbox code that depends on a Lua global or library not present in the linked Lua build; a build mismatch where the loader block references a Lua API the runtime lacks; memory limits causing sandbox setup to fail.","solutions":["Read errMsg carefully - it is the specific Lua-side failure (e.g., missing global, nil index).","Verify the Garnet Lua build exposes the globals/functions the sandbox expects.","Rebuild/redeploy a consistent Garnet package."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"catch (GarnetException ex) when (ex.Message.StartsWith(\"Could not initialize Lua sandbox state\")) {\n    logger.LogCritical(\"Sandbox PCall failed: {Msg}\", ex.Message);\n    throw;\n}","preventionTips":["Keep the Lua runtime and the Garnet loader block from the same build.","Log errMsg verbatim; it is the authoritative Lua-side failure reason."],"tags":["lua","scripting","initialization","sandbox"],"backgroundTag":null,"analyzedSha":"951b0fc6838721f89d102c2bbe1b914e8d39d700","analyzedAt":"2026-08-13T19:01:32.939Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}