Lua script excution failing with "attempt to compare number with nil"

587 views
Skip to first unread message

gl...@telecave.net

unread,
Mar 22, 2016, 2:24:34 AM3/22/16
to Redis DB

Hi,

 

Sporadically, Redis fails executing following Lua script:

 

var newDeadlineDouble = newDeadline.UtcDateTime.ToOADate();

 

var timeoutslist = someRedisSortedSetkey;

double score = some_score_from_querying_above_sortedset;

double scoremin = score - 0.000000000004; // double number encoding/parsing produces some inconsistencies requiring a range for comparison

double scoremax = score + 0.000000000004;

var id = somerediskey;

 

var script = "local score = tonumber(redis.call('zscore', '" + timeoutslist + "', KEYS[1]))\n" +

             "if score > tonumber(ARGV[1]) and score < tonumber(ARGV[2]) then redis.call('zadd', '" + timeoutslist + "', ARGV[3], KEYS[1]) return redis.call('hgetall', KEYS[2]) end";

 

var task = db.ScriptEvaluateAsync(script, new RedisKey[] { id, id }, new RedisValue[] { scoremin, scoremax, newDeadlineDouble });

await task.ConfigureAwait(false);

 

Exception:

StackExchange.Redis.RedisServerException: ERR Error running script (call to f_2bb5d9f8f933748c46de66a1b175e89c339533e5): @user_script:2: user_script:2: attempt to compare number with nil

 

I don’t see how any comparison could run into nil.

Is this a known issue?

Any suggestions for a workaround, please?

 

This issue happens infrequently (about 10x a day) in multiple environments under some moderate load against Redis (Window) 3.0.500, but I have seen already in earlier versions.

Regards,

Gunter

Marc Gravell

unread,
Mar 22, 2016, 4:38:21 AM3/22/16
to redi...@googlegroups.com
My immediate assumption would be that this happens when the parent key (someRedisSortedSetKey) doesn't exist, or has no member with the field name from KEYS[1] (id). You may want to check what comes back from zscore against nil here.

As side notes:

- a member name inside a sorted set is not a "key" in this context, and should be passed as an ARGV
- the parent key name **is** a "key" in this context, and should be passed in KEYS, not concatenated into the command

Getting these confused won't necessarily hurt you *now*, but it would be a blocker on "cluster", as it'll make commands go to the wrong servers. Additionally, you might want to like at "verbatim string literals" in C#, which allow you to write multi-line strings very conveniently, for example:

const string myLuaCommand=@"do this
and this
and another line here";

Marc

--
You received this message because you are subscribed to the Google Groups "Redis DB" group.
To unsubscribe from this group and stop receiving emails from it, send an email to redis-db+u...@googlegroups.com.
To post to this group, send email to redi...@googlegroups.com.
Visit this group at https://groups.google.com/group/redis-db.
For more options, visit https://groups.google.com/d/optout.



--
Regards,

Marc
Reply all
Reply to author
Forward
0 new messages