refactor: simplify session locking

This commit is contained in:
banteg
2025-12-29 03:36:57 +04:00
parent f40096db79
commit ed9e2d7360
@@ -10,6 +10,7 @@ import threading
import time import time
import logging import logging
import sys import sys
from weakref import WeakValueDictionary
from logging.handlers import RotatingFileHandler from logging.handlers import RotatingFileHandler
from concurrent.futures import ThreadPoolExecutor from concurrent.futures import ThreadPoolExecutor
from typing import Any from typing import Any
@@ -202,14 +203,16 @@ class CodexExecRunner:
self.extra_args = extra_args self.extra_args = extra_args
# per-session locks to prevent concurrent resumes to same session_id # per-session locks to prevent concurrent resumes to same session_id
self._locks: dict[str, threading.Lock] = {} self._session_locks: WeakValueDictionary[str, threading.Lock] = WeakValueDictionary()
self._locks_guard = threading.Lock() self._locks_guard = threading.Lock()
def _lock_for(self, session_id: str) -> threading.Lock: def _lock_for(self, session_id: str) -> threading.Lock:
with self._locks_guard: with self._locks_guard:
if session_id not in self._locks: lock = self._session_locks.get(session_id)
self._locks[session_id] = threading.Lock() if lock is None:
return self._locks[session_id] lock = threading.Lock()
self._session_locks[session_id] = lock
return lock
def run( def run(
self, self,