From bac28bc5cab23927d10eb8d6fbbde1ac117899bd Mon Sep 17 00:00:00 2001 From: David Wilson Date: Thu, 1 Nov 2018 20:06:09 +0000 Subject: [PATCH] issue #76, #370: add fix for disconnect cleanup test Simply listen to RouteMonitor's Context "disconnect" and forget contexts according to RouteMonitor's rules, rather than duplicate them (and screw it up). --- ansible_mitogen/services.py | 27 ++++++++++----------------- 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/ansible_mitogen/services.py b/ansible_mitogen/services.py index a5f31b1c..c97d71bb 100644 --- a/ansible_mitogen/services.py +++ b/ansible_mitogen/services.py @@ -297,23 +297,19 @@ class ContextService(mitogen.service.Service): finally: self._lock.release() - def _on_stream_disconnect(self, stream): + def _on_context_disconnect(self, context): """ - Respond to Stream disconnection by deleting any record of contexts - reached via that stream. This method runs in the Broker thread and must - not to block. + Respond to Context disconnect event by deleting any record of the no + longer reachable context. This method runs in the Broker thread and + must not to block. """ # TODO: there is a race between creation of a context and disconnection # of its related stream. An error reply should be sent to any message # in _latches_by_key below. self._lock.acquire() try: - routes = self.router.route_monitor.get_routes(stream) - for context in list(self._key_by_context): - if context.context_id in routes: - LOG.info('Dropping %r due to disconnect of %r', - context, stream) - self._forget_context_unlocked(context) + LOG.info('Forgetting %r due to stream disconnect', context) + self._forget_context_unlocked(context) finally: self._lock.release() @@ -379,13 +375,10 @@ class ContextService(mitogen.service.Service): context = method(via=via, unidirectional=True, **spec['kwargs']) if via and spec.get('enable_lru'): self._update_lru(context, spec, via) - else: - # For directly connected contexts, listen to the associated - # Stream's disconnect event and use it to invalidate dependent - # Contexts. - stream = self.router.stream_by_id(context.context_id) - mitogen.core.listen(stream, 'disconnect', - lambda: self._on_stream_disconnect(stream)) + + # Forget the context when its disconnect event fires. + mitogen.core.listen(context, 'disconnect', + lambda: self._on_context_disconnect(context)) self._send_module_forwards(context) init_child_result = context.call(