From fc5fc08962651039305b93ef0731ba0672a9203e Mon Sep 17 00:00:00 2001 From: jack1142 <6032823+jack1142@users.noreply.github.com> Date: Sun, 26 Jan 2020 18:16:13 +0100 Subject: [PATCH] [Downloader] Log errors from initialization task (#3444) * Update downloader.py * Create 3444.misc.rst * enhance(downloader): don't type infinitely on init error * fix(downloader): unindent `_ready_raised` check * Update downloader.py --- changelog.d/downloader/3444.misc.rst | 1 + redbot/cogs/downloader/downloader.py | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+) create mode 100644 changelog.d/downloader/3444.misc.rst diff --git a/changelog.d/downloader/3444.misc.rst b/changelog.d/downloader/3444.misc.rst new file mode 100644 index 000000000..9a3798bfb --- /dev/null +++ b/changelog.d/downloader/3444.misc.rst @@ -0,0 +1 @@ + Log errors that may happen in initialization task. diff --git a/redbot/cogs/downloader/downloader.py b/redbot/cogs/downloader/downloader.py index c0099bf14..52dc73a1d 100644 --- a/redbot/cogs/downloader/downloader.py +++ b/redbot/cogs/downloader/downloader.py @@ -55,6 +55,7 @@ class Downloader(commands.Cog): self._repo_manager = RepoManager() self._ready = asyncio.Event() self._init_task = None + self._ready_raised = False def _create_lib_folder(self, *, remove_first: bool = False) -> None: if remove_first: @@ -67,13 +68,30 @@ class Downloader(commands.Cog): async def cog_before_invoke(self, ctx: commands.Context) -> None: async with ctx.typing(): await self._ready.wait() + if self._ready_raised: + await ctx.send( + "There was an error during Downloader's initialization." + " Check logs for more information." + ) + raise commands.CheckFailure() def cog_unload(self): if self._init_task is not None: self._init_task.cancel() def create_init_task(self): + def _done_callback(task: asyncio.Task) -> None: + exc = task.exception() + if exc is not None: + log.error( + "An unexpected error occurred during Downloader's initialization.", + exc_info=exc, + ) + self._ready_raised = True + self._ready.set() + self._init_task = asyncio.create_task(self.initialize()) + self._init_task.add_done_callback(_done_callback) async def initialize(self) -> None: await self._repo_manager.initialize()