[ThreadGroup] Don't remove timer when stop timers

Graceful exit is not working since we removed the timers
before waiting them to complete.

Closes-Bug: #1769825
Change-Id: I2e52b680a09d337d9b598a4826132df8c7b0b4d9
This commit is contained in:
TommyLike 2018-04-03 15:39:31 +08:00
parent ccbc263e2e
commit 20a2178171
2 changed files with 14 additions and 4 deletions

View File

@ -80,6 +80,7 @@ class ThreadGroupTestCase(test_base.BaseTestCase):
self.assertEqual(0, len(self.tg.threads))
self.assertTrue(end_time - start_time < 1)
self.assertEqual(0, len(self.tg.timers))
def test_stop_gracefully(self):
@ -92,6 +93,7 @@ class ThreadGroupTestCase(test_base.BaseTestCase):
self.assertEqual(0, len(self.tg.threads))
self.assertTrue(end_time - start_time >= 1)
self.assertEqual(0, len(self.tg.timers))
def test_cancel_early(self):

View File

@ -125,9 +125,11 @@ class ThreadGroup(object):
lambda x: x.stop(),
lambda x: LOG.exception('Error stopping thread.'))
def stop_timers(self):
def stop_timers(self, wait=False):
for timer in self.timers:
timer.stop()
if wait:
self._wait_timers()
self.timers = []
def stop(self, graceful=False):
@ -137,17 +139,17 @@ class ThreadGroup(object):
Never kill threads.
* In case of graceful=False, kill threads immediately.
"""
self.stop_timers()
self.stop_timers(wait=graceful)
if graceful:
# In case of graceful=True, wait for all threads to be
# finished, never kill threads
self.wait()
self._wait_threads()
else:
# In case of graceful=False(Default), kill threads
# immediately
self._stop_threads()
def wait(self):
def _wait_timers(self):
for x in self.timers:
try:
x.wait()
@ -156,10 +158,16 @@ class ThreadGroup(object):
pass
except Exception:
LOG.exception('Error waiting on timer.')
def _wait_threads(self):
self._perform_action_on_threads(
lambda x: x.wait(),
lambda x: LOG.exception('Error waiting on thread.'))
def wait(self):
self._wait_timers()
self._wait_threads()
def _any_threads_alive(self):
current = threading.current_thread()
for x in self.threads[:]: