Skip to content
This repository has been archived by the owner on Aug 4, 2020. It is now read-only.

Reduce critical section protected by global lock #189

Merged
merged 5 commits into from Feb 15, 2013

Conversation

edevil
Copy link

@edevil edevil commented Feb 13, 2013

This tries to reduce the amount of operations done while holding the global lock. Also addresses bug #184.

timeout = self.pool_timeout
if timeout == -1:
timeout = None
block = self._current_conns >= self._max_conns
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we're not checking the value of _current_conns while holding the lock, I think there's a subtle race here:

  1. thread-1 goes through _new_if_required(), sees that _current_conns == _max_conns, and doesn't create a new connection
  2. In thread-2, _current_conns gets decremented
  3. thread-1 hits this check, sees that _current_conns < _max_conns, and sets block to False
  4. In thread-2 (or some other thread), _current_conns gets incremented, and the queue is still empty
  5. thread-1 doesn't block on trying to get something from the queue, so it fails immediately. It goes through _new_if_required again, but since _current_conns == _max_conns at this point, it doesn't create a connection.
  6. NoConnectionAvailable is raised in thread-1 without every having waited for a connection to become available

I'm not sure about what would need to happen for steps 2 and 4 to occur, but it's probably not safe to bet that they won't ever happen.

@edevil
Copy link
Author

edevil commented Feb 14, 2013

I've addressed the case you mentioned.

@thobbs
Copy link
Member

thobbs commented Feb 15, 2013

Well done. Thank you very much!

thobbs added a commit that referenced this pull request Feb 15, 2013
Reduce critical section protected by global lock
@thobbs thobbs merged commit 53454c1 into pycassa:master Feb 15, 2013
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants