Skip to content
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

Fix race condition in redis.lock.Lock.release #1031

Closed
wants to merge 1 commit into from

Conversation

bmerry
Copy link
Contributor

@bmerry bmerry commented Sep 19, 2018

The release code wasn't using transaction correctly. It calls watch,
which puts the pipeline into immediate execution mode, checks the
current value of the key, then deletes it. At no stage does it call
multi to put it back into pipeline mode. As a result, the command
stack is empty and so no MULTI/EXEC is done, and the watch has no
effect.

This leads to a race condition when using expiring locks:

  • Client 1 acquires the lock, with a timeout
  • Client 1 calls release()
  • Client 1 watches the key
  • Client 1 gets the key, sees that it is still correct
  • Client 1's timeout expires, so the key disappears
  • Client 2 acquires the lock
  • Client 1 deletes the key
  • Client 3 also acquires the lock

Now client 3 has stolen the lock from under client 2.

The release code wasn't using transaction correctly. It calls `watch`,
which puts the pipeline into immediate execution mode, checks the
current value of the key, then deletes it. At no stage does it call
`multi` to put it back into pipeline mode. As a result, the command
stack is empty and so no MULTI/EXEC is done, and the watch has no
effect.

This leads to a race condition when using expiring locks:

- Client 1 acquires the lock, with a timeout
- Client 1 calls release()
- Client 1 watches the key
- Client 1 gets the key, sees that it is still correct
- Client 1's timeout expires, so the key disappears
- Client 2 acquires the lock
- Client 1 deletes the key
- Client 3 also acquires the lock

Now client 3 has stolen the lock from under client 2.
@andymccurdy
Copy link
Contributor

Fixed in redis-py 3.0. LuaLock replaces Lock completely.

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 this pull request may close these issues.

2 participants