diff --git a/core/detection/pipeline.py b/core/detection/pipeline.py index df1106f..076cdc9 100644 --- a/core/detection/pipeline.py +++ b/core/detection/pipeline.py @@ -1108,6 +1108,8 @@ class DetectionPipeline: self.branch_processor.cleanup() if self.license_plate_manager: - asyncio.create_task(self.license_plate_manager.close()) + # Schedule cleanup task and track it to prevent warnings + cleanup_task = asyncio.create_task(self.license_plate_manager.close()) + cleanup_task.add_done_callback(lambda _: None) # Suppress "Task exception was never retrieved" logger.info("Detection pipeline cleaned up") \ No newline at end of file diff --git a/core/storage/license_plate.py b/core/storage/license_plate.py index 4c5b703..19cbf73 100644 --- a/core/storage/license_plate.py +++ b/core/storage/license_plate.py @@ -123,20 +123,24 @@ class LicensePlateManager: except asyncio.CancelledError: logger.info("License plate subscription task cancelled") - # Properly close the async generator - if listen_generator is not None: - try: - await listen_generator.aclose() - except Exception as e: - logger.debug(f"Error closing listen generator: {e}") + # Don't try to close generator here - let it be handled by the context + # The async generator will be properly closed by the cancellation mechanism + raise # Re-raise to maintain proper cancellation semantics except Exception as e: logger.error(f"Error in license plate message listener: {e}", exc_info=True) - # Properly close the async generator on error + # Only attempt cleanup if it's not a cancellation + finally: + # Safe cleanup of async generator if listen_generator is not None: try: - await listen_generator.aclose() + # Check if we can safely close without conflicting with ongoing operations + if hasattr(listen_generator, 'aclose') and not asyncio.current_task().cancelled(): + await listen_generator.aclose() + except (RuntimeError, AttributeError) as e: + # Generator is already closing or in invalid state - safe to ignore + logger.debug(f"Generator cleanup skipped (safe): {e}") except Exception as e: - logger.debug(f"Error closing listen generator: {e}") + logger.debug(f"Generator cleanup error (non-critical): {e}") async def _process_license_plate_result(self, data: Dict[str, Any]): """