Skip to content

Commit

Permalink
Implement thread-safe access to SESSION_MESSAGES
Browse files Browse the repository at this point in the history
  • Loading branch information
mimisavage authored Oct 31, 2024
1 parent a8a8a58 commit 163c7f9
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions apps/shared/tests/test_error_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,18 @@ def test_message_format_validation(self, error_handler):
async def test_concurrent_message_addition(self, error_handler):
"""Test adding messages concurrently"""
import asyncio
from asyncio import Lock

lock = Lock()

async def add_message(message):
error_handler.SESSION_MESSAGES[error_handler.SESSION_ID].append(
{"role": "user", "content": message}
)
async with lock:
error_handler.SESSION_MESSAGES[error_handler.SESSION_ID].append(
{"role": "user", "content": message}
)
await asyncio.sleep(0.1)

error_handler.SESSION_MESSAGES[error_handler.SESSION_ID].clear()
# Create multiple concurrent tasks
tasks = [add_message(f"Message {i}") for i in range(5)]
await asyncio.gather(*tasks)
Expand Down

0 comments on commit 163c7f9

Please sign in to comment.