fix: prevent thread and memory leaks from PostHog telemetry (#4535)

Co-authored-by: utkarsh240799 <utkarsh240799@users.noreply.github.com>
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-authored-by: kartik-mem0 <kartik.labhshetwar@mem0.ai>
This commit is contained in:
Utkarsh
2026-04-03 20:01:04 +05:30
committed by GitHub
co-authored by utkarsh240799 Claude Opus 4.6 kartik-mem0
parent 33d2bc495d
commit fcbb70ab3b
3 changed files with 382 additions and 13 deletions
+29
View File
@@ -304,6 +304,23 @@ class Memory(MemoryBase):
)
capture_event("mem0.init", self, {"sync_type": "sync"})
def close(self):
"""Release resources held by this Memory instance (SQLite connections, etc.).
The global telemetry singleton is intentionally *not* shut down here
because it is shared across all Memory instances in the process. It is
cleaned up automatically at process exit via an ``atexit`` handler.
"""
if hasattr(self, "db") and self.db is not None:
self.db.close()
def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
self.close()
return False
@classmethod
def from_config(cls, config_dict: Dict[str, Any]):
try:
@@ -1425,6 +1442,18 @@ class AsyncMemory(MemoryBase):
self._telemetry_vector_store = VectorStoreFactory.create(self.config.vector_store.provider, telemetry_config)
capture_event("mem0.init", self, {"sync_type": "async"})
def close(self):
"""Release resources held by this AsyncMemory instance."""
if hasattr(self, "db") and self.db is not None:
self.db.close()
async def __aenter__(self):
return self
async def __aexit__(self, exc_type, exc_val, exc_tb):
self.close()
return False
@classmethod
def from_config(cls, config_dict: Dict[str, Any]):
try: