From 0e5ba747a90e578c16f419c837a834512ca33504 Mon Sep 17 00:00:00 2001 From: jesopo Date: Thu, 13 Jun 2019 17:11:46 +0100 Subject: [PATCH] Wrap _read_loop() and _write_loop() in a try that kills the main thread --- src/IRCBot.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/IRCBot.py b/src/IRCBot.py index e058a2ab..d81cfbc5 100644 --- a/src/IRCBot.py +++ b/src/IRCBot.py @@ -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: