From 10a635fc78a779541be3f6d477d8d1099aad0398 Mon Sep 17 00:00:00 2001 From: GeoffreyCoulaud Date: Sat, 3 Jun 2023 14:18:07 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=A7=20Thread-safe=20manager=20error=20?= =?UTF-8?q?reporting?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/store/managers/manager.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/store/managers/manager.py b/src/store/managers/manager.py index c2c94f8..6df6d0f 100644 --- a/src/store/managers/manager.py +++ b/src/store/managers/manager.py @@ -1,6 +1,7 @@ import logging from abc import abstractmethod from typing import Any, Callable +from threading import Lock from src.game import Game @@ -20,6 +21,7 @@ class Manager: max_tries: int = 3 errors: list[Exception] + errors_lock: Lock = None @property def name(self): @@ -28,15 +30,18 @@ class Manager: def __init__(self) -> None: super().__init__() self.errors = [] + self.errors_lock = Lock() def report_error(self, error: Exception): """Report an error that happened in Manager.run""" - self.errors.append(error) + with self.errors_lock: + self.errors.append(error) def collect_errors(self) -> list[Exception]: """Get the errors produced by the manager and remove them from self.errors""" - errors = list(self.errors) - self.errors.clear() + with self.errors_lock: + errors = self.errors.copy() + self.errors.clear() return errors @abstractmethod