garnet: NullReferenceException in ExecuteScript Method When Executing Lua Scripts

Describe the bug

When attempting to execute Lua scripts using GarnetServer, a NullReferenceException is thrown in the ExecuteScript method. This issue occurs when using the EVAL or EVALSHA commands.

A NullReferenceException is thrown, indicating that an object reference is not set to an instance of an object.

Error Message:

2024-09-04 18:28:21 04::28::21 fail: Session[0] [192.168.80.2:6379] [192.168.80.1:42348] [01400C58] Error executing Lua script callback
2024-09-04 18:28:21 04::28::21 crit: Session[0] [192.168.80.2:6379] [192.168.80.1:42348] [01400C58] ProcessMessages threw an exception: System.NullReferenceException: Object reference not set to an instance of an object.    at Garnet.server.RespServerSession.ExecuteScript(Int32 count, LuaRunner scriptRunner) in /src/libs/server/Lua/LuaCommands.cs:line 235    at Garnet.server.RespServerSession.TryEVAL() in /src/libs/server/Lua/LuaCommands.cs:line 97    at Garnet.server.RespServerSession.ProcessArrayCommands[TGarnetApi](RespCommand cmd, TGarnetApi& storageApi) in /src/libs/server/Resp/RespServerSession.cs:line 664    at Garnet.server.RespServerSession.ProcessMessages() in /src/libs/server/Resp/RespServerSession.cs:line 420    at Garnet.server.RespServerSession.TryConsumeMessages(Byte* reqBuffer, Int32 bytesReceived) in /src/libs/server/Resp/RespServerSession.cs:line 299

The LuaRunner object might not be properly initialized or retrieved, leading to a null reference when attempting to execute the script.

This report is related to the Lua implementation in https://github.com/microsoft/garnet/pull/550

Steps to reproduce the bug

  1. Enable Lua scripting in GarnetServer.
  2. Attempt to execute a Lua script using the EVAL or EVALSHA command. Go Code Using RedisLock from https://github.com/bsm/redislock/blob/main/redislock.go:
func (rs *RedisStorage) Lock(ctx context.Context, name string) error {

	key := rs.prefixLock(name)

	for {
		// try to obtain lock
		lock, err := rs.locker.Obtain(ctx, key, lockTTL, &redislock.Options{})

		// lock successfully obtained
		if err == nil {
			// store the lock in sync.map, needed for unlocking
			rs.locks.Store(key, lock)
			// keep the lock fresh as long as we hold it
			go func(ctx context.Context, lock *redislock.Lock) {
				for {
					// refresh the Redis lock
					err := lock.Refresh(ctx, lockTTL, nil)
					if err != nil {
						return
					}

					select {
					case <-time.After(lockRefreshInterval):
					case <-ctx.Done():
						return
					}
				}
			}(ctx, lock)

			return nil
		}

		// check for unexpected error
		if err != redislock.ErrNotObtained {
			return fmt.Errorf("Unable to obtain lock for %s: %v", key, err)
		}

		// lock already exists, wait and try again until cancelled
		select {
		case <-time.After(lockPollInterval):
		case <-ctx.Done():
			return ctx.Err()
		}
	}
}

Related Lua scripts: https://github.com/bsm/redislock/blob/main/obtain.lua https://github.com/bsm/redislock/blob/main/refresh.lua

  1. Observe the NullReferenceException in the logs of garnet server.

Expected behavior

The Lua script should execute without throwing a NullReferenceException.

Screenshots

No response

Release version

v1.0.19

IDE

No response

OS version

No response

Additional context

Docker Compose service configuration: docker-compose.yml

services:
  garnet:
    image: 'ghcr.io/microsoft/garnet:1.0.19'
    ulimits:
      memlock: -1
    ports:
      - "6379:6379"
    volumes:
      - garnetdata:/data
      - ./garnet.conf:/app/garnet.conf
    command: ["--config-import-path", "/app/garnet.conf"]

garnet.conf

{
	/* Enable Lua scripts on server. */
	"EnableLua" : true,

	/* Run Lua scripts as a transaction (lock keys - run script - unlock keys). */
	"LuaTransactionMode" : true,
}

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 30 (12 by maintainers)

Most upvoted comments

Good news - this is now fixed in the latest commit to my linked PR. The example from https://github.com/bsm/redislock?tab=readme-ov-file#examples now works unmodified. If your code still does not work, please provide a stand-alone example similar to the redislock example I linked, so that I can run it directly using go run ..

I’m pretty sure the issue is with this line in release.lua: https://github.com/bsm/redislock/blob/bfd385b776d1e069eca5cb6b6cc41d635cbb4e55/release.lua#L5 because unpack isn’t implemented. There’s still a lot of work to do to get Garnet up to parity with Redis in terms of LUA scripting. I’ll chip away at some of it but it’s not going to be a short-term thing.

Thank you for the simple clean repro! Fixed in the latest commit to the open PR. It was just a small corner case where we did not pass the extra SET parameters (such as expiration) from the script to the storage API.

@badrishc, I wanted to follow up and let you know that I’ve tested the latest commit, and I’m happy to report that it’s working very smoothly without any issues now! The improvements you’ve made to handle Lua scripting in Garnet are fantastic, and I’m confident that many users will appreciate the capability to seamlessly replace Lua Redis implementations with Garnet.

A huge thank you to both you and @mmclure-msft for your tremendous effort and time invested in resolving these issues. It’s wonderful to see the progress that’s been made!

Let’s close this issue if the PR is merged.

I think the main issue is that much of the API documented at https://redis.io/docs/latest/develop/interact/programmability/lua-api/ is not yet implemented. It will probably require community involvement to add the functionality to bring Garnet Lua scripting to parity with Redis. Definitely not a couple of small PRs 😄