fix: asyncio: Task was destroyed but it is pending
All checks were successful
Build Worker Base and Application Images / check-base-changes (push) Successful in 8s
Build Worker Base and Application Images / build-base (push) Has been skipped
Build Worker Base and Application Images / build-docker (push) Successful in 2m48s
Build Worker Base and Application Images / deploy-stack (push) Successful in 18s

This commit is contained in:
ziesorx 2025-09-25 01:50:04 +07:00
parent 5065e43837
commit b8de5e191e
2 changed files with 16 additions and 10 deletions

View file

@ -1108,6 +1108,8 @@ class DetectionPipeline:
self.branch_processor.cleanup() self.branch_processor.cleanup()
if self.license_plate_manager: 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") logger.info("Detection pipeline cleaned up")

View file

@ -123,20 +123,24 @@ class LicensePlateManager:
except asyncio.CancelledError: except asyncio.CancelledError:
logger.info("License plate subscription task cancelled") logger.info("License plate subscription task cancelled")
# Properly close the async generator # Don't try to close generator here - let it be handled by the context
if listen_generator is not None: # The async generator will be properly closed by the cancellation mechanism
try: raise # Re-raise to maintain proper cancellation semantics
await listen_generator.aclose()
except Exception as e:
logger.debug(f"Error closing listen generator: {e}")
except Exception as e: except Exception as e:
logger.error(f"Error in license plate message listener: {e}", exc_info=True) 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: if listen_generator is not None:
try: 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: 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]): async def _process_license_plate_result(self, data: Dict[str, Any]):
""" """