-
Notifications
You must be signed in to change notification settings - Fork 471
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use a simple lock on memory creation task to prevent race conditions … #1004
Conversation
WalkthroughThis change introduces a simple lock mechanism to prevent race conditions during memory creation tasks in the transcription service. By using a boolean flag, we ensure that only one task is executed at a time, enhancing the stability and reliability of our system. Changes
Uplevel your code reviews with Entelligence.AI ProEntelligence.AI ProIf you like this project, please support us by purchasing the Pro version. The Pro version has advanced context, superior noise reduction and several proprietary improvements compared to the open source version. Moreover, Entelligence.AI Pro is free for open source projects. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Files selected (1)
- backend/routers/transcribe_v2.py (2)
Review comments generated (2)
- Review: 2
- LGTM: 0
Tips
Chat with Entelligence.AI Bot (@Entelligence.AI
)
- Reply on review comments left by this bot to ask follow-up questions. A review comment is a comment on a diff or a file.
- Invite the bot into a review comment chain by tagging
@Entelligence.AI
in a reply.
Code suggestions
- The bot may make code suggestions, but please review them carefully before committing since the line number ranges may be misaligned.
- You can edit the comment made by the bot and manually tweak the suggestion if it is slightly off.
Pausing incremental reviews
- Add
@Entelligence.AI: ignore
anywhere in the PR description to pause further reviews from the bot.
backend/routers/transcribe_v2.py
Outdated
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this section, you're using the memory_creation_task_lock
to prevent simultaneous task creation. However, the lock is not used correctly. The lock should be acquired before entering the critical section (where the shared resource is accessed) and released after exiting it. In Python's asyncio, you can use the async with
statement to acquire and automatically release the lock. Also, the print statements used for debugging should be removed or replaced with proper logging.
- if memory_creation_task_lock:
- print("memory_creation_task_lock > lock")
- return
-
- 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
+ async with memory_creation_task_lock:
+ if memory_creation_task is not None:
+ memory_creation_task.cancel()
+ try:
+ await memory_creation_task
+ except asyncio.CancelledError:
+ # Replace with proper logging
+ pass
+
+ memory_creation_task = asyncio.create_task(_trigger_create_memory_with_delay(memory_creation_timeout))
This way, the lock is properly used to prevent race conditions during task creation and cancellation.
…that cause abandoned tasks
53dc420
to
a395fea
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Files selected (1)
- backend/routers/transcribe_v2.py (2)
Review comments generated (2)
- Review: 2
- LGTM: 0
Tips
Chat with Entelligence.AI Bot (@Entelligence.AI
)
- Reply on review comments left by this bot to ask follow-up questions. A review comment is a comment on a diff or a file.
- Invite the bot into a review comment chain by tagging
@Entelligence.AI
in a reply.
Code suggestions
- The bot may make code suggestions, but please review them carefully before committing since the line number ranges may be misaligned.
- You can edit the comment made by the bot and manually tweak the suggestion if it is slightly off.
Pausing incremental reviews
- Add
@Entelligence.AI: ignore
anywhere in the PR description to pause further reviews from the bot.
@@ -160,6 +160,7 @@ async def _create_current_memory(): | |||
return | |||
await _create_memory(memory) | |||
|
|||
memory_creation_task_lock = False |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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: | ||
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The lock mechanism introduced here seems to have a race condition itself. The check for memory_creation_task_lock
and the setting of it to True
are not atomic, meaning that two tasks could both pass the check before either has set the lock to True
. This would defeat the purpose of the lock. A better approach might be to use an asyncio Lock or Semaphore which are designed to handle these situations correctly.
- nonlocal memory_creation_task_lock
-
- if memory_creation_task_lock:
- return
-
- 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
+ memory_creation_lock = asyncio.Lock()
+
+ async with memory_creation_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")
+ memory_creation_task = asyncio.create_task(_trigger_create_memory_with_delay(memory_creation_timeout))
…that cause abandoned tasks
Summary by Entelligence.AI