Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redis Lua script error: Script attempted to access nonexistent global variable 'print' script

Tags:

redis

lua

I'm learning Lua. Given below, is a Lua script to handle some operations

local function reverseDb ()
  local key =  KEYS[1]
  local value = ARGV[1]
  redis.call('SREM',key,value)
  return value
end

if pcall(reverseDb) then
  print("success")
else
  print("error")
  redis.call('SADD',key,value)
end

While running it throws an error:

Script attempted to access nonexistent global variable 'print' script: 39f13cb3bddd638c815531acbc2dc6434a0329c6, on @user_script:17.

I don't understand why the error is there. Any clue?

like image 895
PRJ Avatar asked Apr 22 '26 12:04

PRJ


1 Answers

Redis runs Lua script in a sandbox context, and limits the usage of global variables, only parts of global variables/functions are allowed. Obviously, print is not allowed.

In your case, you can return the error message instead of printing it.

if pcall(reverseDb) then
  return "success"
else
  redis.call('SADD',key,value)
  return "error"
end
like image 59
for_stack Avatar answered Apr 24 '26 04:04

for_stack



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!