microsoft/garnet · critical · GarnetException

Could not initialize Lua sandbox state: {errMsg}

Error message

Could not initialize Lua sandbox state: {errMsg}

What it means

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.

Source

Thrown at libs/server/Lua/LuaRunner.cs:366

                try
                {
                    if (state.StackTop >= 1)
                    {
                        // We control the definition of LoaderBlock, so we know this is a string
                        state.KnownStringToBuffer(1, out var errSpan);
                        errMsg = Encoding.UTF8.GetString(errSpan);
                    }
                    else
                    {
                        errMsg = "No error provided";
                    }
                }
                catch
                {
                    errMsg = "Error when fetching pcall error";
                }

                throw new GarnetException($"Could not initialize Lua sandbox state: {errMsg}");
            }

            state.GetGlobal(LuaType.Table, "sandbox_env\0"u8);
            if (!state.TryRef(out sandboxEnvRegistryIndex))
            {
                throw new GarnetException("Insufficient space in VM for sandbox_env ref");
            }

            state.GetGlobal(LuaType.Function, "load_sandboxed\0"u8);
            if (!state.TryRef(out loadSandboxedRegistryIndex))
            {
                throw new GarnetException("Insufficient space in VM for load_sandboxed ref");
            }

            state.GetGlobal(LuaType.Function, "reset_keys_and_argv\0"u8);
            if (!state.TryRef(out resetKeysAndArgvRegistryIndex))
            {
                throw new GarnetException("Insufficient space in VM for reset_keys_and_argv ref");

View on GitHub (pinned to 951b0fc683)

Solutions

  1. Read errMsg carefully - it is the specific Lua-side failure (e.g., missing global, nil index).
  2. Verify the Garnet Lua build exposes the globals/functions the sandbox expects.
  3. Rebuild/redeploy a consistent Garnet package.
Defensive patterns

Strategy: try-catch

Try / catch

catch (GarnetException ex) when (ex.Message.StartsWith("Could not initialize Lua sandbox state")) {
    logger.LogCritical("Sandbox PCall failed: {Msg}", ex.Message);
    throw;
}

Prevention

When it happens

Trigger: 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).

Common situations: 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.

Related errors


AI-assisted analysis of microsoft/garnet@951b0fc683 (2026-08-13). Data as JSON: /api/errors/648834e1595ae906. Report an issue: GitHub.