From 53dc42009a554377ce3ba9170ad851149cfd8ebb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?th=E1=BB=8Bnh?= Date: Tue, 8 Oct 2024 15:36:56 +0700 Subject: [PATCH] Use a simple lock on memory creation task to prevent race conditions that cause abandoned tasks --- backend/routers/transcribe_v2.py | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/backend/routers/transcribe_v2.py b/backend/routers/transcribe_v2.py index ba4087a18..4b9a2f875 100644 --- a/backend/routers/transcribe_v2.py +++ b/backend/routers/transcribe_v2.py @@ -160,6 +160,7 @@ async def _create_current_memory(): return await _create_memory(memory) + memory_creation_task_lock = False memory_creation_task = None seconds_to_trim = None seconds_to_add = None @@ -209,15 +210,24 @@ def _get_or_create_in_progress_memory(segments: List[dict]): async def create_memory_creation_task(): nonlocal memory_creation_task + nonlocal memory_creation_task_lock - if memory_creation_task is not None: - memory_creation_task.cancel() - try: - await memory_creation_task - except asyncio.CancelledError: - print("memory_creation_task is cancelled now") + if memory_creation_task_lock: + print("memory_creation_task_lock > lock") + return - memory_creation_task = asyncio.create_task(_trigger_create_memory_with_delay(memory_creation_timeout)) + memory_creation_task_lock = True + try: + if memory_creation_task is not None: + memory_creation_task.cancel() + try: + await memory_creation_task + except asyncio.CancelledError: + print("memory_creation_task is cancelled now") + + memory_creation_task = asyncio.create_task(_trigger_create_memory_with_delay(memory_creation_timeout)) + finally: + memory_creation_task_lock = False def stream_transcript(segments, _): nonlocal websocket