From 38c0ad1eeaad06f2ccb3274e7e51512c022fc62b Mon Sep 17 00:00:00 2001 From: David Wilson Date: Wed, 11 Apr 2018 01:51:27 +0100 Subject: [PATCH] core: don't deregister Router handles until Broker exit. Lots of "invalid handle: ..., 102" messages started appearing during exit recently because ordering changed slightly, and local handles were sent _DEAD even though the broker loop was still progressing through shutdown. The "shutdown" event is too early to close handles: it is the start of the grace period where streams and downstream contexts can finish up any work and deliver buffered data, including FORWARD_LOG messages that haven't arrived yet. So instead, - move the _DEAD logic to the "exit" event, - get rid of Context.on_shutdown() entirely, it's been unused for over a month, - get rid of the "crash" event, since it always fires prior to "exit", and its only use was to send _DEAD to local handles, which now happens during exit anyway. --- docs/signals.rst | 5 ----- mitogen/core.py | 15 ++------------- 2 files changed, 2 insertions(+), 18 deletions(-) diff --git a/docs/signals.rst b/docs/signals.rst index 312cbc5e..8a314b5c 100644 --- a/docs/signals.rst +++ b/docs/signals.rst @@ -47,11 +47,6 @@ These signals are used internally by Mitogen. - ``shutdown`` - Fired on the Broker thread after Broker.shutdown() is called. - * - :py:class:`mitogen.core.Broker` - - ``crash`` - - Fired when a crash occurs on the broker thread. Used by client apps to - hasten shutdown (e.g. by disconnect - * - :py:class:`mitogen.core.Broker` - ``shutdown`` - Fired after Broker.shutdown() is called. diff --git a/mitogen/core.py b/mitogen/core.py index 7f891382..8b5ba63a 100644 --- a/mitogen/core.py +++ b/mitogen/core.py @@ -925,9 +925,6 @@ class Context(object): _v and LOG.debug('%r.on_disconnect()', self) fire(self, 'disconnect') - def on_shutdown(self, broker): - pass - def send(self, msg): """send `obj` to `handle`, and tell the broker we have output. May be called from any thread.""" @@ -1221,8 +1218,7 @@ class Router(object): def __init__(self, broker): self.broker = broker - listen(broker, 'crash', self._cleanup_handlers) - listen(broker, 'shutdown', self.on_broker_shutdown) + listen(broker, 'exit', self._on_broker_exit) # Here seems as good a place as any. global _v, _vv @@ -1247,13 +1243,7 @@ class Router(object): del self._stream_by_id[context.context_id] context.on_disconnect() - def on_broker_shutdown(self): - for context in self._context_by_id.itervalues(): - context.on_shutdown(self.broker) - - self._cleanup_handlers() - - def _cleanup_handlers(self): + def _on_broker_exit(self): while self._handle_map: _, (_, func, _) = self._handle_map.popitem() func(_DEAD) @@ -1465,7 +1455,6 @@ class Broker(object): side.stream.on_disconnect(self) except Exception: LOG.exception('_broker_main() crashed') - fire(self, 'crash') fire(self, 'exit')