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.
pull/193/head
David Wilson 6 years ago
parent 813d139d48
commit 38c0ad1eea

@ -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.

@ -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')

Loading…
Cancel
Save