StackExchange.Redis icon indicating copy to clipboard operation
StackExchange.Redis copied to clipboard

Invalid Lua script evaluation

Open olizarevichroman opened this issue 3 years ago • 6 comments

Hi, i'm calling this simple Lua script passing Redis key to get string value, but key is passed to the ARGV collection. Also i've noticed that my key passed twice, first as an argument and then as a key. Profiler log provided below

var script = "return redis.call('get', @settingIdKey)");
var preparedScript = LuaScript.Prepare(script);

var redisResult = await database.ScriptEvaluateAsync(preparedScript, new
{
    settingIdKey = (RedisKey)"my key",
});

Profiler log:

"SCRIPT" "LOAD" "return redis.call('get', ARGV[1])"
"EVAL" "return redis.call('get', ARGV[1])" "1" "my key" "my key"
[0 lua] "get" "my key"

Based on the documentation it should treat it as a key

olizarevichroman avatar Oct 16 '22 14:10 olizarevichroman

Indeed, this is a side-effect of the current auto-parameterization implementation. If you want fine-grained control over the semantics, I suggest using the simple ScriptEvaluate API and passing the args/keys explicitly.

mgravell avatar Oct 16 '22 17:10 mgravell

Is it supposed to be fixed? Or this "side-effect" is expected behavior?

olizarevichroman avatar Oct 16 '22 17:10 olizarevichroman

Is this not just broken as-is?

madelson avatar Oct 22 '25 01:10 madelson

Suboptimal: yes. Broken: no. Unless I'm missing something, it is correctly routed and all values used are correct. Keys are generally short, so duplicating the keys isn't pathologically bad. But yes, it could be improved. Honestly, this whole API could be improved - I'll add to my backlog to review it.

mgravell avatar Oct 22 '25 05:10 mgravell

@mgravell I had someone report that the scripts weren't working in their setup because it references all keys through ARGV instead of KEYS (see https://github.com/madelson/DistributedLock/issues/254). I think I've seen the same with an AWS Elasticache cluster but I'm not sure.

madelson avatar Oct 27 '25 23:10 madelson

OK, that's useful context. I'll see if we can take another look. A reliable workaround is to simply use naked Lua rather than the rewriter - but: it is an API that exists, so should either a: work as intended, or b: be deprecated. If you have a reliable repro (of it actually failing, rather than just being soboptimal), that would probably speed me up here, @madelson

mgravell avatar Oct 28 '25 07:10 mgravell