This will lead to the same deadlock condition with the read and the second write lock watching each other independent of the currently acquired write lock.
Snippet to Reproduce the Problem
import logging
logging.basicConfig(format="%(asctime)s %(levelname)s:%(message)s", level=logging.DEBUG)
import threading
import time
from kazoo.client import KazooClient
k = KazooClient()
k.start()
lock_path = "/lock"
writer_running = threading.Event()
reader_running = threading.Event()
def writer():
logging.info("Writer running")
writer_running.set()
with k.WriteLock(lock_path):
logging.info("Got write lock")
def reader():
logging.info("Reader running")
reader_running.set()
with k.ReadLock(lock_path):
logging.info("Got read lock")
writer_thread = threading.Thread(target=writer)
reader_thread = threading.Thread(target=reader)
with k.WriteLock(lock_path):
logging.info("Starting reader")
reader_thread.start()
reader_running.wait()
logging.info("Starting writer")
writer_thread.start()
writer_running.wait()
logging.info("Waiting for locks to block")
time.sleep(5)
logging.info("Outer lock released")
logging.info("Joining reader/writer threads ...")
reader_thread.join()
writer_thread.join()
logging.error("This will never show up")
Expected Behavior
Read/write lock recipe never deadlocks.
Actual Behavior
Read/write lock recipe deadlocks under certain conditions. The deadlock seems to happen as follows:
We now have a deadlock with the read lock and the second write lock waiting for each other to be "released".
There can be a variation of this when the second write lock's znode is created before the read lock calls: https://github.com/python-zk/kazoo/blob/6337fd6f72b59fb20886f980f2e0d6d41525dc35/kazoo/recipe/lock.py#L263
This will lead to the same deadlock condition with the read and the second write lock watching each other independent of the currently acquired write lock.
Snippet to Reproduce the Problem
Logs with logging in DEBUG mode
Specifications
pip list
command: