Skip to content

Commit

Permalink
Use a simple lock on memory creation task to prevent race conditions …
Browse files Browse the repository at this point in the history
…that cause abandoned tasks
  • Loading branch information
beastoin committed Oct 8, 2024
1 parent 8fadcf9 commit 53dc420
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions backend/routers/transcribe_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 53dc420

Please sign in to comment.