Wrap _read_loop() and _write_loop() in a try that kills the main thread

This commit is contained in:
jesopo 2019-06-13 17:11:46 +01:00
parent f793f5fb92
commit 0e5ba747a9

View file

@ -237,8 +237,10 @@ class Bot(object):
return thread
def run(self):
self._read_thread = self._daemon_thread(self._read_loop)
self._write_thread = self._daemon_thread(self._write_loop)
self._read_thread = self._daemon_thread(
lambda: self._loop_catch(self._read_loop))
self._write_thread = self._daemon_thread(
lambda: self._loop_catch(self._write_loop))
self._event_loop()
def _kill(self):
@ -267,6 +269,13 @@ class Bot(object):
def _post_read_factory(self, server, lines):
return lambda: server._post_read(lines)
def _loop_catch(self, loop: typing.Callable[[], None]):
try:
loop()
except:
self._event_queue.put(TriggerEvent(TriggerEventType.Kill))
raise
def _write_loop(self):
while self.running:
with self._write_condition: