Merge "Clean up watch resources after watcher.stop()"

This commit is contained in:
Zuul 2018-09-21 13:01:46 +00:00 committed by Gerrit Code Review
commit 038cc1667e
2 changed files with 18 additions and 4 deletions

View File

@ -332,3 +332,14 @@ class TestWatcher(test_base.TestCase):
m_handler.assert_has_calls([mock.call(e) for e in events])
m_sys_exit.assert_called_once_with(1)
def test_watch_restart(self):
tg = mock.Mock()
w = watcher.Watcher(lambda e: None, tg)
w.add('/test')
w.start()
tg.add_thread.assert_called_once_with(mock.ANY, '/test')
w.stop()
tg.add_thread = mock.Mock() # Reset mock.
w.start()
tg.add_thread.assert_called_once_with(mock.ANY, '/test')

View File

@ -144,14 +144,17 @@ class Watcher(health.HealthHandler):
def _stop_watch(self, path):
if self._idle.get(path):
if self._thread_group:
if self._thread_group and path in self._watching:
self._watching[path].stop()
# NOTE(dulek): Thread gets killed immediately, so we need to
# take care of this ourselves.
self._watching.pop(path, None)
self._idle.pop(path, None)
def _graceful_watch_exit(self, path):
try:
self.remove(path)
self._watching.pop(path)
self._idle.pop(path)
self._watching.pop(path, None)
self._idle.pop(path, None)
LOG.info("Stopped watching '%s'", path)
except KeyError:
LOG.error("Failed to exit watch gracefully")