From 163c7f909db42353708cb07148f66df243b9174e Mon Sep 17 00:00:00 2001 From: Mimi Sav Date: Thu, 31 Oct 2024 09:39:23 +0100 Subject: [PATCH] Implement thread-safe access to SESSION_MESSAGES --- apps/shared/tests/test_error_handler.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/apps/shared/tests/test_error_handler.py b/apps/shared/tests/test_error_handler.py index 09cb6744..09080409 100644 --- a/apps/shared/tests/test_error_handler.py +++ b/apps/shared/tests/test_error_handler.py @@ -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)