From 6e6b52fe3ba7c3a304b33ff8864bb5d3b9978e6e Mon Sep 17 00:00:00 2001 From: cfdaily Date: Sun, 14 Jun 2026 00:09:27 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20asyncio.Lock=20=E6=87=92=E5=8A=A0?= =?UTF-8?q?=E8=BD=BD=E9=98=B2=20event=20loop=20=E5=85=B3=E9=97=AD=E5=90=8E?= =?UTF-8?q?=20import=20=E5=A4=B1=E8=B4=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/toolchain_routes.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/api/toolchain_routes.py b/src/api/toolchain_routes.py index e8c1e92..39ed51d 100644 --- a/src/api/toolchain_routes.py +++ b/src/api/toolchain_routes.py @@ -50,7 +50,15 @@ router = APIRouter(tags=["toolchain"]) _delivery_cache: Set[str] = set() _delivery_timestamps: List[Tuple[float, str]] = [] _TTL_SECONDS = 7 * 24 * 3600 -_idempotency_lock = asyncio.Lock() +_idempotency_lock: Optional[asyncio.Lock] = None + + +def _get_idempotency_lock() -> asyncio.Lock: + """懒加载 asyncio.Lock,避免模块级创建时 event loop 不存在(Python 3.9)。""" + global _idempotency_lock + if _idempotency_lock is None: + _idempotency_lock = asyncio.Lock() + return _idempotency_lock def _is_duplicate(event: str, delivery: str, @@ -1219,7 +1227,7 @@ async def gitea_webhook( # 2. 幂等检查(需要在 payload 解析后,以支持内容去重) if x_gitea_event and x_gitea_delivery: - async with _idempotency_lock: + async with _get_idempotency_lock(): if _is_duplicate(x_gitea_event, x_gitea_delivery, payload): logger.debug( "Duplicate webhook: %s/%s",