Skip to content

Commit e9dd82c

Browse files
fix: address reviewer feedback on LockMap implementation
- Fix AttributeError: Change _session.py reset() to use .drop() instead of .pop() - Fix LRU eviction logic: Continue past locked entries instead of breaking - Update docstring: Clarify asyncio-safe vs thread-safe guarantees Co-authored-by: Mervin Praison <MervinPraison@users.noreply.github.com>
1 parent 855a5e9 commit e9dd82c

2 files changed

Lines changed: 9 additions & 3 deletions

File tree

src/praisonai/praisonai/_lockmap.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,14 @@
1111

1212

1313
class LockMap:
14-
"""Thread-safe per-key asyncio.Lock map with automatic cleanup.
14+
"""Asyncio-safe per-key asyncio.Lock map with automatic cleanup.
1515
1616
Provides a lock per key with LRU eviction and TTL-based cleanup to prevent
1717
unbounded memory growth in long-running applications.
18+
19+
Note: Safe for use within a single asyncio event loop (cooperative
20+
multitasking means no interleaving between non-awaited calls), but NOT
21+
safe for concurrent access from multiple OS threads.
1822
"""
1923

2024
def __init__(self, *, max_entries: int = 10_000, ttl_seconds: float = 3600.0):
@@ -68,8 +72,10 @@ def _evict_stale(self, now: float) -> None:
6872
if lock.locked():
6973
# Don't evict locks currently held; bump them to the end
7074
self._locks.move_to_end(k)
71-
break
75+
# Continue trying to evict other unlocked entries
76+
continue
7277
self._locks.popitem(last=False)
78+
break
7379

7480
def drop(self, key: Hashable) -> None:
7581
"""Manually remove a lock for the given key."""

src/praisonai/praisonai/bots/_session.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@ def reset(self, user_id: str) -> bool:
522522
existed = storage_key in self._histories
523523
self._histories.pop(storage_key, None)
524524
self._last_active.pop(storage_key, None)
525-
self._locks.pop(storage_key, None)
525+
self._locks.drop(storage_key)
526526

527527
if self._store is not None:
528528
persist_key = self._session_key(user_id)

0 commit comments

Comments
 (0)