Skip to content

Commit 1cef5e0

Browse files
author
matdev83
committed
fix: Prevent memory leak in ToolAccessPolicyService
- Replace the unbounded _policy_cache dictionary with a size-limited OrderedDict to prevent potential memory leaks from unlimited cache growth.
1 parent 884e079 commit 1cef5e0

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

src/core/services/tool_access_policy_service.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import logging
66
import re
77
import threading
8+
from collections import OrderedDict
89
from dataclasses import dataclass, field
910
from typing import Any
1011

@@ -136,7 +137,10 @@ def __init__(
136137
self._total_evaluation_time_ms = 0.0
137138

138139
# Policy lookup cache: (model_name, agent) -> AccessPolicy | None
139-
self._policy_cache: dict[tuple[str, str | None], AccessPolicy | None] = {}
140+
self._policy_cache: OrderedDict[tuple[str, str | None], AccessPolicy | None] = (
141+
OrderedDict()
142+
)
143+
self._policy_cache_size = 128
140144
self._cache_hits = 0
141145
self._cache_misses = 0
142146
self._cache_lock = threading.Lock()
@@ -244,6 +248,7 @@ def _select_policy(
244248
cache_key = (model_name, agent)
245249
with self._cache_lock:
246250
if cache_key in self._policy_cache:
251+
self._policy_cache.move_to_end(cache_key)
247252
self._cache_hits += 1
248253
return self._policy_cache[cache_key]
249254
self._cache_misses += 1
@@ -260,6 +265,8 @@ def _select_policy(
260265
# Cache the result
261266
with self._cache_lock:
262267
self._policy_cache[cache_key] = selected_policy
268+
if len(self._policy_cache) > self._policy_cache_size:
269+
self._policy_cache.popitem(last=False)
263270
return selected_policy
264271

265272
def filter_tool_definitions(

0 commit comments

Comments
 (0)