From d54fe171fcb35a5d80bd848f813225e662188ae2 Mon Sep 17 00:00:00 2001 From: Dita Aji Pratama Date: Fri, 19 Jun 2026 13:31:02 +0700 Subject: [PATCH] Fixing new session error when running multiple hendrik --- services/session_manager_neo.py | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/services/session_manager_neo.py b/services/session_manager_neo.py index c4f3e50..1d5c8d4 100644 --- a/services/session_manager_neo.py +++ b/services/session_manager_neo.py @@ -30,7 +30,7 @@ class NeoSessionManager: self._db = TinyDB(db_path) self._table = self._db.table("sessions") - def create(self, name: str, model_info: dict) -> NeoSession: + def create(self, name: str, model_info: dict, max_retries: int = 5) -> NeoSession: now = datetime.now(timezone.utc).isoformat() doc = { "session_id": str(uuid.uuid4()), @@ -40,8 +40,24 @@ class NeoSessionManager: "model_info": model_info, "messages": [], } - doc_id = self._table.insert(doc) - return self._doc_to_session(self._table.get(doc_id=doc_id)) + + import time + import random + + for attempt in range(max_retries): + try: + doc_id = self._table.insert(doc) + return self._doc_to_session(self._table.get(doc_id=doc_id)) + except ValueError: + if attempt < max_retries - 1: + # Exponential backoff dengan jitter + wait_time = (0.1 * (2 ** attempt)) + random.uniform(0, 0.05) + time.sleep(wait_time) + # Reload DB untuk sinkronisasi + self._db = TinyDB(self._db._root._path) + self._table = self._db.table("sessions") + else: + raise def list(self) -> list[dict]: results = []