Facing deadlock issue while acquiring the redis lock

949 views
Skip to first unread message

Rinky Mangal

unread,
Apr 19, 2018, 4:45:57 AM4/19/18
to Redis DB

I am working on POC of redis cluster. I am using it for following two use cases:

  • distributed cache
  • distributed lock

But I am observing deadlock while acquiring the redis lock.

I have attached one python script to reproduce this issue.

In order to execute this script follow these steps:

  1. mkdir temp_venv
  2. virtualenv temp_venv
  3. source temp_venv/bin/activate
  4. pip install redis
  5. cp test.py temp_venv/lib/python2.7/site-packages/
  6. cd temp_venv/lib/python2.7/site-packages/
  7. Execute script:
    python2.7 test.py 6379
  8. open other terminal and execute tcpkill to block connection:
    tcpkill host
  9. Execute script:
    python2.7 test.py 6379
  10. Terminate tcpkill by 'CTRL + c'
  11. Execute script: python2.7 test.py 6379
  12. Repeat the steps 8 to 11 two to three times. We will stuck infinitely while aquiring the lock.

My redis engine  version is 3.2.10.

I am beginner to redis. Please help me to resolve this issue.
test.sh

Josiah Carlson

unread,
Apr 19, 2018, 1:50:29 PM4/19/18
to redi...@googlegroups.com
Hey Rinky,

You really should read my book. It's called Redis in Action, it uses Python, and has 23 pages on locking in 2 chapters. You can read it online for free by visiting Salvatore's employer's site, or you can buy it online in English, Japanese, and now Chinese. </pitch>

In this case, your test locks the data without a timeout. You can read the documentation on the implications of not including a timeout here:

Because you killed the connection ability, you removed the locker's ability to delete the lock, and created your own deadlock. Set a timeout, be happy (and read those pages on locking).

 - Josiah


--
You received this message because you are subscribed to the Google Groups "Redis DB" group.
To unsubscribe from this group and stop receiving emails from it, send an email to redis-db+unsubscribe@googlegroups.com.
To post to this group, send email to redi...@googlegroups.com.
Visit this group at https://groups.google.com/group/redis-db.
For more options, visit https://groups.google.com/d/optout.

hva...@gmail.com

unread,
Apr 19, 2018, 11:01:57 PM4/19/18
to Redis DB
It looks like you're using the built-in lock method in the redis-py Python client.
There's a comment block at the top of the Lock() method, visible in the file https://github.com/andymccurdy/redis-py/blob/master/redis/lock.py:

"""
A shared, distributed Lock. Using Redis for locking allows the Lock
to be shared across processes and/or machines.

It's left to the user to resolve deadlock issues and make sure
multiple clients play nicely together.
"""

It looks like you're using a Lock() method that doesn't resolve deadlock issues for you.  Look around, there might be different Python clients that do, or, as Josiah mentioned, you can find resources that cover the action of exclusive locking more comprehensively (such as his book).

Reply all
Reply to author
Forward
0 new messages