microsoft/garnet · critical · GarnetException

Could not initialize Lua VM: {innerError}

Error message

Could not initialize Lua VM: {innerError}

What it means

Thrown while constructing a LuaRunner when the built-in Lua loader/sandbox block fails to load into the fresh Lua state. innerError is the message Lua itself pushed onto the stack (state.LoadBuffer returned non-OK and left a string). Because the loader block is shipped with Garnet (PrepareLoaderBlockBytes), a load failure usually means the Lua runtime and the embedded loader block are out of sync - a build/compatibility problem rather than user input.

Source

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

            var redisVersionParsed = Version.Parse(redisVersion);
            var redisVersionNum =
                ((byte)redisVersionParsed.Major << 16) |
                ((byte)redisVersionParsed.Minor << 8) |
                ((byte)redisVersionParsed.Build << 0);
            state.PushInteger(redisVersionNum);
            if (!state.TrySetGlobal("garnet_REDIS_VERSION_NUM\0"u8))
            {
                throw new GarnetException("Insufficient space in Lua VM for redis version number global");
            }

            var loadRes = state.LoadBuffer(PrepareLoaderBlockBytes(allowedFunctions, logger).Span);
            if (loadRes != LuaStatus.OK)
            {
                if (state.StackTop == 1 && state.Type(1) == LuaType.String)
                {
                    state.KnownStringToBuffer(1, out var buff);
                    var innerError = Encoding.UTF8.GetString(buff);
                    throw new GarnetException($"Could not initialize Lua VM: {innerError}");
                }

                throw new GarnetException("Could not initialize Lua VM");
            }

            var sandboxRes = state.PCall(0, -1);
            if (sandboxRes != LuaStatus.OK)
            {
                string errMsg;
                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

View on GitHub (pinned to 951b0fc683)

Solutions

  1. Reinstall/rebuild Garnet from a clean source tree so the Lua runtime and the embedded loader block match.
  2. Confirm you are not mixing DLLs/binaries from different Garnet versions in the same deployment.
  3. Capture the full innerError text in logs - it is the Lua compiler's own message and pinpoints the broken loader construct.
Defensive patterns

Strategy: try-catch

Try / catch

try {
    runner = new LuaRunner(scriptSrc, ...);
} catch (GarnetException ex) when (ex.Message.StartsWith("Could not initialize Lua VM")) {
    logger.LogCritical(ex, "Lua runtime/loader mismatch; redeploy a consistent Garnet build");
    throw;
}

Prevention

When it happens

Trigger: A LuaRunner instance is created (the first time a session uses FUNCTION LOAD, FCALL, or EVAL) and state.LoadBuffer(PrepareLoaderBlockBytes(...)) returns a status other than LuaStatus.OK with a string error on top of the Lua stack.

Common situations: Running a Garnet build whose Lua native dependency differs from the one the embedded loader block was authored against; a corrupted Garnet installation; mixing assemblies/binaries from different Garnet versions in one deployment.

Related errors


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