Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG]: Local segment manager memory leak fix #3340

Open
wants to merge 2 commits into
base: trayan-12-13-fix_delete_collection_resource_leak
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion chromadb/segment/impl/manager/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def __init__(self, system: System):
// PersistentLocalHnswSegment.get_file_handle_count()
)
self._vector_instances_file_handle_cache = LRUCache(
segment_limit, callback=lambda _, v: v.close_persistent_index()
segment_limit, callback=lambda _, v: v.stop()
)

@trace_method(
Expand Down Expand Up @@ -159,6 +159,7 @@ def delete_segments(self, collection_id: UUID) -> Sequence[UUID]:
collection_id = segment["collection"]
if segment["id"] in self._instances:
if segment["type"] == SegmentType.HNSW_LOCAL_PERSISTED.value:
self._vector_instances_file_handle_cache.evict(collection_id)
instance = self.get_segment(collection_id, VectorReader)
instance.delete()
elif segment["type"] == SegmentType.SQLITE.value:
Expand Down
36 changes: 23 additions & 13 deletions chromadb/utils/lru_cache.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import threading
from collections import OrderedDict
from typing import Any, Callable, Generic, Optional, TypeVar


K = TypeVar("K")
V = TypeVar("V")

Expand All @@ -14,19 +14,29 @@ def __init__(self, capacity: int, callback: Optional[Callable[[K, V], Any]] = No
self.capacity = capacity
self.cache: OrderedDict[K, V] = OrderedDict()
self.callback = callback
self.lock = threading.Lock()

def get(self, key: K) -> Optional[V]:
if key not in self.cache:
return None
value = self.cache.pop(key)
self.cache[key] = value
return value
with self.lock:
if key not in self.cache:
return None
value = self.cache.pop(key)
self.cache[key] = value
return value

def set(self, key: K, value: V) -> None:
if key in self.cache:
self.cache.pop(key)
elif len(self.cache) == self.capacity:
evicted_key, evicted_value = self.cache.popitem(last=False)
if self.callback:
self.callback(evicted_key, evicted_value)
self.cache[key] = value
with self.lock:
if key in self.cache:
self.cache.pop(key)
elif len(self.cache) == self.capacity:
evicted_key, evicted_value = self.cache.popitem(last=False)
if self.callback:
self.callback(evicted_key, evicted_value)
self.cache[key] = value

def evict(self, key: K) -> None:
with self.lock:
if key in self.cache:
value = self.cache.pop(key)
if self.callback:
self.callback(key, value)
Loading