1

When shutting down the bot from the command line, I need to hit ^C several times because it appears that my event listener thread is not closing properly. Do I need to make changes to the deactivate function?

class Icinga2bot(BotPlugin):
    """
    Use errbot to talk to an Icinga2 monitoring server.
    """

    def report_events(self):
        '''Relay events from the Icinga2 API to a chat channel. Not interactive.
        The event queues are selected in icinga2bot.ini'''
        while not self.stop_thread.is_set():
            queue = i2events()
            room = self.query_room(self.bot_config.CHATROOM_PRESENCE[0])
            debug(room)
            for line in queue:
                self.send(room,line)
                debug(line)


    def activate(self):
        """
        Triggers on plugin activation

        You should delete it if you're not using it to override any default behaviour
        """
        self.stop_thread = threading.Event()
        self.thread = threading.Thread(target = self.report_events)
        self.thread.start()
        super(Icinga2bot, self).activate()

    def deactivate(self):
        """
        Triggers on plugin deactivation

        You should delete it if you're not using it to override any default behaviour
        """
        debug('shutting down bot')
        self.stop_thread.set()
        debug('stop_thread.set() complete')
        self.thread.join()
        debug('thread.join() complete')
        super(Icinga2bot, self).deactivate()

The console window shows that the plugin bogs down between the setting of the stop flag and waiting for the thread to finish.

14:56:28 INFO     errbot.core               Disconnect callback, deactivating all the plugins.
***DEBUG*** shutting down bot
***DEBUG*** stop_thread.set() complete
^C14:58:38 INFO     errbot.core               Disconnect callback, deactivating all the plugins.
***DEBUG*** shutting down bot
***DEBUG*** stop_thread.set() complete
^CTraceback (most recent call last):
  File "/home/errbot/err3/lib/python3.4/site-packages/errbot/backends/xmpp.py", line 527, in serve_forever
    self.conn.serve_forever()
  File "/home/errbot/err3/lib/python3.4/site-packages/errbot/backends/xmpp.py", line 362, in serve_forever
    self.client.process(block=True)
  File "/home/errbot/err3/lib/python3.4/site-packages/sleekxmpp/basexmpp.py", line 246, in process
    return XMLStream.process(self, *args, **kwargs)
  File "/home/errbot/err3/lib/python3.4/site-packages/sleekxmpp/xmlstream/xmlstream.py", line 1454, in process
    self._process()
  File "/home/errbot/err3/lib/python3.4/site-packages/sleekxmpp/xmlstream/xmlstream.py", line 1513, in _process
    self.disconnect()
  File "/home/errbot/err3/lib/python3.4/site-packages/sleekxmpp/xmlstream/xmlstream.py", line 690, in disconnect
    args=(reconnect, wait, send_close))
  File "/home/errbot/err3/lib/python3.4/site-packages/sleekxmpp/thirdparty/statemachine.py", line 70, in transition
    func=func, args=args, kwargs=kwargs)
  File "/home/errbot/err3/lib/python3.4/site-packages/sleekxmpp/thirdparty/statemachine.py", line 111, in transition_any
    return_val = func(*args,**kwargs) if func is not None else True
  File "/home/errbot/err3/lib/python3.4/site-packages/sleekxmpp/xmlstream/xmlstream.py", line 738, in _disconnect
    self.event('disconnected', direct=True)
  File "/home/errbot/err3/lib/python3.4/site-packages/sleekxmpp/xmlstream/xmlstream.py", line 1173, in event
    handler[0](out_data)
  File "/home/errbot/err3/lib/python3.4/site-packages/errbot/backends/xmpp.py", line 504, in disconnected
    self.disconnect_callback()
  File "/home/errbot/err3/lib/python3.4/site-packages/errbot/core.py", line 646, in disconnect_callback
    self.plugin_manager.deactivate_all_plugins()
  File "/home/errbot/err3/lib/python3.4/site-packages/errbot/plugin_manager.py", line 409, in deactivate_all_plugins
    self.deactivatePluginByName(name, BOTPLUGIN_TAG)
  File "/home/errbot/err3/lib/python3.4/site-packages/yapsy/PluginManager.py", line 580, in deactivatePluginByName
    plugin_to_deactivate.deactivate()
  File "/home/errbot/plugins/icinga2bot.py", line 205, in deactivate
    self.thread.join()
  File "/usr/lib64/python3.4/threading.py", line 1060, in join
    self._wait_for_tstate_lock()
  File "/usr/lib64/python3.4/threading.py", line 1076, in _wait_for_tstate_lock
    elif lock.acquire(block, timeout):
KeyboardInterrupt

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "./err3/bin/errbot", line 11, in <module>
    sys.exit(main())
  File "/home/errbot/err3/lib/python3.4/site-packages/errbot/cli.py", line 318, in main
    bootstrap(backend, root_logger, config, restore)
  File "/home/errbot/err3/lib/python3.4/site-packages/errbot/bootstrap.py", line 196, in bootstrap
    bot.serve_forever()
  File "/home/errbot/err3/lib/python3.4/site-packages/errbot/backends/xmpp.py", line 530, in serve_forever
    self.disconnect_callback()
  File "/home/errbot/err3/lib/python3.4/site-packages/errbot/core.py", line 646, in disconnect_callback
    self.plugin_manager.deactivate_all_plugins()
  File "/home/errbot/err3/lib/python3.4/site-packages/errbot/plugin_manager.py", line 409, in deactivate_all_plugins
    self.deactivatePluginByName(name, BOTPLUGIN_TAG)
  File "/home/errbot/err3/lib/python3.4/site-packages/yapsy/PluginManager.py", line 580, in deactivatePluginByName
    plugin_to_deactivate.deactivate()
  File "/home/errbot/plugins/icinga2bot.py", line 205, in deactivate
    self.thread.join()
  File "/usr/lib64/python3.4/threading.py", line 1060, in join
    self._wait_for_tstate_lock()
  File "/usr/lib64/python3.4/threading.py", line 1076, in _wait_for_tstate_lock
    elif lock.acquire(block, timeout):
KeyboardInterrupt
Rache
  • 236
  • 2
  • 11
  • I see a potential race condition, maybe add: `def __init__(self, bot):` `self.stop_thread = threading.Event()` to be sure we have only one stop_thread event object ? – gbin Sep 07 '16 at 21:51
  • @gbin I'm afraid I don't follow. I added the `__init__` definition to the class, but that resulted in the bot not connecting to the event server, and when I added `self.activate()` to the init function I received the error `AttributeError: 'Icinga2bot' object has no attribute '_bot'`. Could you provide more detail? – Rache Sep 09 '16 at 15:55

0 Answers0