Skip to content

EVALSHA doesn't work with TcpFakeServer #432

@oliverhaas-scayle

Description

@oliverhaas-scayle

I'm trying to use TcpFakeServer to test some code that uses Lua scripts, but EVALSHA keeps failing with "No matching script. Please use EVAL."

Here's what I'm doing:

import redis
from fakeredis import TcpFakeServer
from threading import Thread
import time

# Start TcpFakeServer
server = TcpFakeServer(("127.0.0.1", 6380), server_type="redis")
thread = Thread(target=server.serve_forever, daemon=True)
thread.start()
time.sleep(0.1)

# Connect and try to use a Lua script
client = redis.Redis(host="127.0.0.1", port=6380)
client.set("mykey", "myvalue")

# Register and execute script
script = client.register_script(b"return redis.call('GET', KEYS[1])")

# Workaround which worked a bit, but not fully
# client.script_load(b"return redis.call('GET', KEYS[1])")

result = script(keys=["mykey"], args=[])
print(f"Result: {result}")

This throws redis.exceptions.ResponseError: No matching script. Please use EVAL.

If I manually call client.script_load(...) before executing the script (see commented line), it works partly, and that also defeats the purpose of register_script() which is supposed to handle this automatically.

From what I understand, redis-py's Script.__call__() first tries EVALSHA, and if that fails with NoScriptError, it automatically calls script_load() and retries. But it seems like TcpFakeServer is missing the "NOSCRIPT" prefix that redis-py uses to identify this specific error type. Something seems off with how error responses are being formatted or parsed when going through the TCP connection.

Using direct fakeredis connections (not TcpFakeServer) works fine btw, this only happens with the TCP server.

It's getting a bit late and I was going in circles quite a bit, so apologies if I'm missing something.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions