Skip to content

Commit

Permalink
SmartPlugin: Improved error handling in stop_asyncio()
Browse files Browse the repository at this point in the history
  • Loading branch information
msinn committed Mar 24, 2024
1 parent ff7f747 commit 1d9e89c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
10 changes: 7 additions & 3 deletions dev/sample_plugin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ def update_item(self, item, caller=None, source=None, dest=None):
if self.alive and caller != self.get_fullname():
# code to execute if the plugin is not stopped
# and only, if the item has not been changed by this plugin:
self.logger.info(f"update_item: {item.property.path} has been changed outside this plugin by caller '{self.callerinfo(caller, source)}'")
self.logger.info(f"update_item: '{item.property.path}' has been changed outside this plugin by caller '{self.callerinfo(caller, source)}'")

pass

Expand Down Expand Up @@ -198,12 +198,16 @@ async def plugin_coro(self):
"""
Coroutine for the plugin session (only needed, if using asyncio)
This coroutine opens the session to the hue bridge and
only terminate, when the plugin ois stopped
This coroutine is run as the PluginTask and should
only terminate, when the plugin is stopped
"""
self.logger.notice("plugin_coro started")

self.alive = True

# ...

self.alive = False

self.logger.notice("plugin_coro finished")
return
13 changes: 7 additions & 6 deletions lib/model/smartplugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -1043,12 +1043,13 @@ def stop_asyncio(self):
"""
self.logger.info("Shutting down asyncio loop and thread...")
try:
# Send termination command to plugin_coro to stop the plugin
asyncio.run_coroutine_threadsafe(self.run_queue.put('STOP'), self._asyncio_loop)
except Exception as e:
self.logger.notice(f"stop_asyncio: Exception '{e}' in run_queue.put")
time.sleep(3)
if self._asyncio_loop is not None:
try:
# Send termination command to plugin_coro to stop the plugin
asyncio.run_coroutine_threadsafe(self.run_queue.put('STOP'), self._asyncio_loop)
except Exception as e:
self.logger.notice(f"stop_asyncio: Exception '{e}' in run_queue.put ({self._asyncio_loop=})")
time.sleep(3)
try:
self.pluginThread.join()
self.logger.debug("_asyncio_loop_thread of plugin stopped")
Expand Down

0 comments on commit 1d9e89c

Please sign in to comment.