From b8ecf044a1dd971df609d355cd6f72c12371a013 Mon Sep 17 00:00:00 2001 From: banteg <4562643+banteg@users.noreply.github.com> Date: Mon, 29 Dec 2025 14:07:12 +0400 Subject: [PATCH] refactor(exec_bridge): use weak session locks --- .../src/codex_telegram_bridge/exec_bridge.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/codex_telegram_bridge/src/codex_telegram_bridge/exec_bridge.py b/codex_telegram_bridge/src/codex_telegram_bridge/exec_bridge.py index cebb560..4bf0fdd 100644 --- a/codex_telegram_bridge/src/codex_telegram_bridge/exec_bridge.py +++ b/codex_telegram_bridge/src/codex_telegram_bridge/exec_bridge.py @@ -13,6 +13,7 @@ from collections import deque from collections.abc import Awaitable, Callable from dataclasses import dataclass from typing import Any +from weakref import WeakValueDictionary import typer @@ -154,16 +155,16 @@ class CodexExecRunner: self.extra_args = extra_args # Per-session locks to prevent concurrent resumes to the same session_id. - self._session_locks: dict[str, asyncio.Lock] = {} - self._locks_guard = asyncio.Lock() + self._session_locks: WeakValueDictionary[str, asyncio.Lock] = ( + WeakValueDictionary() + ) async def _lock_for(self, session_id: str) -> asyncio.Lock: - async with self._locks_guard: - lock = self._session_locks.get(session_id) - if lock is None: - lock = asyncio.Lock() - self._session_locks[session_id] = lock - return lock + lock = self._session_locks.get(session_id) + if lock is None: + lock = asyncio.Lock() + self._session_locks[session_id] = lock + return lock async def run( self,