Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
shenxiangzhuang opened this issue Feb 27, 2025 · 1 comment · Fixed by #3534
Closed

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

shenxiangzhuang opened this issue Feb 27, 2025 · 1 comment · Fixed by #3534

Comments

@shenxiangzhuang
Copy link
Contributor

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.

@petyaslavova
Copy link
Collaborator

PR #3534 is linked to this issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants