From 03221ee9abf0d118f4bd6a55e3c9b006d1ecb242 Mon Sep 17 00:00:00 2001 From: David Wilson Date: Sun, 18 Feb 2018 11:33:41 +0545 Subject: [PATCH] ansible: gracefully shut down the service thread at exit. --- ansible_mitogen/strategy/mitogen.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/ansible_mitogen/strategy/mitogen.py b/ansible_mitogen/strategy/mitogen.py index a7bbb44f..b3b9563b 100644 --- a/ansible_mitogen/strategy/mitogen.py +++ b/ansible_mitogen/strategy/mitogen.py @@ -188,10 +188,12 @@ class StrategyModule(ansible.plugins.strategy.linear.StrategyModule): self.listener = mitogen.unix.Listener(self.router) os.environ['MITOGEN_LISTENER_PATH'] = self.listener.path - # TODO: gracefully shutdown and join on this at exit. + def _setup_services(self): + """ + Construct a ContextService and a thread to service requests for it. + """ self.service = ContextService(self.router) self.service_thread = threading.Thread(target=self.service.run) - self.service_thread.setDaemon(True) self.service_thread.start() def _run_with_master(self, iterator, play_context, result): @@ -201,11 +203,13 @@ class StrategyModule(ansible.plugins.strategy.linear.StrategyModule): """ mitogen.utils.log_to_file() self._setup_master() + self._setup_services() try: return super(StrategyModule, self).run(iterator, play_context) finally: - self.router.broker.shutdown() os.unlink(self.listener.path) + self.router.broker.shutdown() + self.service_thread.join(timeout=10) def _install_wrappers(self): """