diff --git a/py/services/recipe_scanner.py b/py/services/recipe_scanner.py index 29b93561..008fb771 100644 --- a/py/services/recipe_scanner.py +++ b/py/services/recipe_scanner.py @@ -11,9 +11,25 @@ from .lora_scanner import LoraScanner from .civitai_client import CivitaiClient from ..utils.utils import fuzzy_match import sys +from contextlib import asynccontextmanager logger = logging.getLogger(__name__) +@asynccontextmanager +async def async_timeout(timeout: float): + task = asyncio.current_task() + loop = asyncio.get_running_loop() + handle = loop.call_later(timeout, task.cancel) + try: + yield + except asyncio.CancelledError: + raise asyncio.TimeoutError() + finally: + handle.cancel() + +# Use native asyncio.timeout (Python 3.11+) if available, else use async_timeout. +_timeout = getattr(asyncio, "timeout", async_timeout) + class RecipeScanner: """Service for scanning and managing recipe images""" @@ -65,7 +81,7 @@ class RecipeScanner: # Try to acquire the lock with a timeout to prevent deadlocks try: # Use a timeout for acquiring the lock - async with asyncio.timeout(1.0): + async with _timeout(1.0): async with self._initialization_lock: # Check again after acquiring the lock if self._cache is not None and not force_refresh: @@ -448,4 +464,4 @@ class RecipeScanner: 'total_pages': (total_items + page_size - 1) // page_size } - return result \ No newline at end of file + return result \ No newline at end of file