From 2025c4e8f009cd4eb986f9090504ba3745e429c2 Mon Sep 17 00:00:00 2001 From: pacnpal <183241239+pacnpal@users.noreply.github.com> Date: Fri, 15 Nov 2024 21:39:19 +0000 Subject: [PATCH] Fixed the guild_only import by importing it directly from redbot.core.commands Improved the cog unload process by: Reducing cleanup timeouts (UNLOAD_TIMEOUT to 5s, CLEANUP_TIMEOUT to 3s) Adding immediate task cancellation Implementing aggressive cleanup with force_cleanup_resources Clearing all references in the finally block Fixed code indentation issues --- videoarchiver/core/base.py | 41 ++++++++++++++++++++++++++++---------- 1 file changed, 31 insertions(+), 10 deletions(-) diff --git a/videoarchiver/core/base.py b/videoarchiver/core/base.py index 823a254..83f14ab 100644 --- a/videoarchiver/core/base.py +++ b/videoarchiver/core/base.py @@ -40,9 +40,9 @@ from .events import setup_events logger = logging.getLogger("VideoArchiver") -# Constants for timeouts -UNLOAD_TIMEOUT = 30 # seconds -CLEANUP_TIMEOUT = 15 # seconds +# Constants for timeouts - reduced for faster cleanup +UNLOAD_TIMEOUT = 5 # seconds +CLEANUP_TIMEOUT = 3 # seconds class VideoArchiver(GroupCog): """Archive videos from Discord channels""" @@ -420,22 +420,43 @@ class VideoArchiver(GroupCog): raise ProcessingError(f"Error during cog load: {str(e)}") async def cog_unload(self) -> None: - """Clean up when cog is unloaded with timeout""" + """Clean up when cog is unloaded with aggressive timeout handling""" self._unloading = True try: - # Create cleanup task with timeout - cleanup_task = asyncio.create_task(self._cleanup()) + # Cancel any pending tasks immediately + if self._init_task and not self._init_task.done(): + self._init_task.cancel() + + if self._cleanup_task and not self._cleanup_task.done(): + self._cleanup_task.cancel() + + # Try normal cleanup first with short timeout + cleanup_task = asyncio.create_task(cleanup_resources(self)) try: - await asyncio.wait_for(cleanup_task, timeout=UNLOAD_TIMEOUT) + await asyncio.wait_for(cleanup_task, timeout=CLEANUP_TIMEOUT) except asyncio.TimeoutError: - logger.error("Cog unload timed out, forcing cleanup") - # Force cleanup of any remaining resources + logger.warning("Normal cleanup timed out, forcing cleanup") + # Immediately cancel the normal cleanup task + cleanup_task.cancel() + # Force cleanup without waiting + await force_cleanup_resources(self) + except Exception as e: + logger.error(f"Error during normal cleanup: {str(e)}") await force_cleanup_resources(self) except Exception as e: logger.error(f"Error during cog unload: {str(e)}") - await force_cleanup_resources(self) finally: self._unloading = False + # Ensure ready flag is cleared + self.ready.clear() + # Clear all references + self.bot = None + self.processor = None + self.queue_manager = None + self.update_checker = None + self.ffmpeg_mgr = None + self.components.clear() + self.db = None async def _cleanup(self) -> None: """Clean up all resources with proper handling"""