Skip to content

Release a lock from a non-owned thread should raise proper error #3535

Closed
@shenxiangzhuang

Description

@shenxiangzhuang

Reproduce code:

import redis
import threading
import time


# Create Redis client
r = redis.Redis()
lock_name = "lock:example"


def thread1_function():
    print("Thread 1: Starting")
    
    lock = r.lock(lock_name, timeout=5)
    if lock.acquire():
        print("Thread 1: Lock acquired")


def thread2_function():
    print("Thread 2: Starting")
    lock = r.lock(lock_name)
    try:
        lock.release()
    except Exception as e:
        print(f"Thread 2: Lock error: {e}")


# Create and start threads
t1 = threading.Thread(target=thread1_function)
t2 = threading.Thread(target=thread2_function)

t1.start()
time.sleep(1)
t2.start()

# Wait for threads to complete
t1.join()
t2.join()

# clean up
r.delete(lock_name)

Output:

Thread 1: Starting
Thread 1: Lock acquired
Thread 2: Starting
Thread 2: Lock error: Cannot release an unlocked lock

Currently, if we release lock from thread2, we got Lock error with Lock error: Cannot release an unlocked lock, which is not true actually and will mislead the user in a way.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions