-
Notifications
You must be signed in to change notification settings - Fork 1.1k
PYTHON-2334: Fix gevent race condition #472
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
Changes from all commits
4418e5f
921e658
918ad32
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,22 +40,21 @@ def acquire(self, blocking=True, timeout=None): | |
raise ValueError("can't specify timeout for non-blocking acquire") | ||
rc = False | ||
endtime = None | ||
self._cond.acquire() | ||
while self._value == 0: | ||
if not blocking: | ||
break | ||
if timeout is not None: | ||
if endtime is None: | ||
endtime = _time() + timeout | ||
else: | ||
timeout = endtime - _time() | ||
if timeout <= 0: | ||
break | ||
self._cond.wait(timeout) | ||
else: | ||
self._value = self._value - 1 | ||
rc = True | ||
self._cond.release() | ||
with self._cond: | ||
while self._value == 0: | ||
if not blocking: | ||
break | ||
if timeout is not None: | ||
if endtime is None: | ||
endtime = _time() + timeout | ||
else: | ||
timeout = endtime - _time() | ||
if timeout <= 0: | ||
break | ||
self._cond.wait(timeout) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will release, and re-lock self._cond Condition has 2 acquires in it, If _waiter.acquire() raises, Condition will re-acquire in the finally clause. If _acquire_restore raises, then Condition won't re-acquire and a double-release is possible. I can't think of a good way to solve that. One possible solution would be to update gevent to monkey patch Condition with a version that guarantees self._lock re-acquisition prior to raising. A double release here is somewhat less concerning though, as it would only cause one waiter to hang until the next notify, instead of leaking a lock. Reference: |
||
else: | ||
self._value = self._value - 1 | ||
rc = True | ||
return rc | ||
|
||
__enter__ = acquire | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If gevent raises on the acquire here, self._socket_semaphore's lock will be permanently decremented.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be simpler to move the
active_sockets
modification down below inside the existing try/except:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought of that, however the second try/except block will decrement the active sockets if an exception is raised.
If you raise within the first with self.lock, you will not have first incremented active_sockets, so you will end up permanently decreasing active_sockets.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point. What about adding a flag:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That would work... Should I add that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated, seems to still work