Skip to content

Commit

Permalink
Fix the usage of condition variable (#646)
Browse files Browse the repository at this point in the history
JIRA issue: DCOS_OSS-5369
  • Loading branch information
Ivan Chernetsky committed Jul 25, 2019
1 parent ebebfbc commit 87c281b
Showing 1 changed file with 25 additions and 29 deletions.
54 changes: 25 additions & 29 deletions marathon_lb.py
Original file line number Diff line number Diff line change
Expand Up @@ -1937,16 +1937,17 @@ def start(self):
self.__thread.start()

def try_reset(self):
with self.__condition:
logger.info('({}): starting event processor thread'.format(
threading.get_ident()))
while True:
self.__condition.acquire()
logger.info('({}): starting event processor thread'.format(
threading.get_ident()))

while True:
pending_reset = False
pending_reload = False

with self.__condition:
if self.__stop:
logger.info('({}): stopping event processor thread'.format(
threading.get_ident()))
self.__condition.release()
return

if not self.__pending_reset and not self.__pending_reload:
Expand All @@ -1959,17 +1960,15 @@ def try_reset(self):
self.__pending_reset = False
self.__pending_reload = False

self.__condition.release()

# Reset takes precedence over reload
if pending_reset:
self.do_reset()
elif pending_reload:
self.do_reload()
else:
# Timed out waiting on the condition variable, just do a
# full reset for good measure (as was done before).
self.do_reset()
# Reset takes precedence over reload
if pending_reset:
self.do_reset()
elif pending_reload:
self.do_reload()
else:
# Timed out waiting on the condition variable, just do a
# full reset for good measure (as was done before).
self.do_reset()

def do_reset(self):
try:
Expand Down Expand Up @@ -2009,22 +2008,19 @@ def do_reload(self):
logger.exception("Unexpected error!")

def stop(self):
self.__condition.acquire()
self.__stop = True
self.__condition.notify()
self.__condition.release()
with self.__condition:
self.__stop = True
self.__condition.notify()

def reset_from_tasks(self):
self.__condition.acquire()
self.__pending_reset = True
self.__condition.notify()
self.__condition.release()
with self.__condition:
self.__pending_reset = True
self.__condition.notify()

def reload_existing_config(self):
self.__condition.acquire()
self.__pending_reload = True
self.__condition.notify()
self.__condition.release()
with self.__condition:
self.__pending_reload = True
self.__condition.notify()

def handle_event(self, event):
if event['eventType'] in self.relevant_events:
Expand Down

0 comments on commit 87c281b

Please sign in to comment.