From 2aa77f38a4f01a2d1ff634b9226ecc393c6b4ee4 Mon Sep 17 00:00:00 2001 From: Thierry Carrez Date: Fri, 13 Nov 2020 13:15:03 +0100 Subject: [PATCH] PTGbot airbag to prevent unexpected crashes Catch all previously-uncaught exceptions from on_pubmsg() and on_privmsg() calls, to prevent the bot from crashing when people start getting creative in their commands. This should of course never happen, but if it does, better continue running than crash. Change-Id: Ic5eeec23652170061cbd2ce3606a3aee22463529 --- ptgbot/bot.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/ptgbot/bot.py b/ptgbot/bot.py index 07e55ae..03de6b9 100644 --- a/ptgbot/bot.py +++ b/ptgbot/bot.py @@ -48,6 +48,17 @@ ANTI_FLOOD_SLEEP = 2 DOC_URL = 'https://opendev.org/openstack/ptgbot/src/branch/master/README.rst' +def make_safe(func): + def inner(*args, **kwargs): + try: + func(*args, **kwargs) + except Exception as e: + msg = "Bot airbag activated: " + str(e) + args[0].log.error(msg) + args[0].send(args[0].channel, msg) + return inner + + class PTGBot(SASL, SSL, irc.bot.SingleServerIRCBot): log = logging.getLogger("ptgbot.bot") @@ -88,6 +99,7 @@ class PTGBot(SASL, SSL, irc.bot.SingleServerIRCBot): else: self.send(channel, "There are no active tracks defined yet") + @make_safe def on_privmsg(self, c, e): if not self.identify_msg_cap: self.log.debug("Ignoring message because identify-msg " @@ -252,6 +264,7 @@ class PTGBot(SASL, SSL, irc.bot.SingleServerIRCBot): "Cancelled subscription %s" % existing_re ) + @make_safe def on_pubmsg(self, c, e): if not self.identify_msg_cap: self.log.debug("Ignoring message because identify-msg "