Skip to content

Commit

Permalink
Handle exceptions in scheduler and main threads
Browse files Browse the repository at this point in the history
Unhandled exceptions in the scheduler thread should no longer cause
scheduled tasks to stop executing.

We also improve both loops by cleanly handling BaseExceptions such as
KeyboardInterrupt and SystemExit
  • Loading branch information
unode committed May 9, 2023
1 parent 9bfd4a3 commit d4fd2b9
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion mmpy_bot/threadpool.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ def handle_work(self):
function(*arguments)
except Exception:
log.exception("Unhandled exception in main loop")
except BaseException:
# Can be KeyboardInterrupt, SystemExit, ...
self.alive = False
# Notify the pool that we finished working
self._queue.task_done()
self._busy_workers.get()
Expand All @@ -73,7 +76,13 @@ def run_pending():
log.info("Scheduler thread started.")
while self.alive:
time.sleep(trigger_period)
default_scheduler.run_pending()
try:
default_scheduler.run_pending()
except Exception:
log.exception("Unhandled exception in main loop")
except BaseException:
# Can be KeyboardInterrupt, SystemExit, ...
self.alive = False
log.info("Scheduler thread stopped.")

self.add_task(run_pending)
Expand Down

0 comments on commit d4fd2b9

Please sign in to comment.