fix: 修复额外的内存泄露问题

1. hybrid_search.py: 修复 _search_vector 方法中的 SQLite 连接泄露
   - 使用 with 语句包装数据库连接
   - 添加异常处理确保连接正确关闭

2. symbol_extractor.py: 添加上下文管理器支持
   - 实现 __enter__ 和 __exit__ 方法
   - 支持 with 语句自动管理资源

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
catlog22
2025-12-21 16:39:38 +08:00
parent 3e9a309079
commit 6eebdb8898
2 changed files with 18 additions and 6 deletions

View File

@@ -47,6 +47,15 @@ class SymbolExtractor:
self.db_conn = sqlite3.connect(str(self.db_path))
self._ensure_tables()
def __enter__(self) -> "SymbolExtractor":
"""Context manager entry: connect to database."""
self.connect()
return self
def __exit__(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> None:
"""Context manager exit: close database connection."""
self.close()
def _ensure_tables(self) -> None:
"""Create symbols and relationships tables if they don't exist."""
if not self.db_conn:

View File

@@ -241,12 +241,15 @@ class HybridSearchEngine:
try:
# Check if semantic chunks table exists
import sqlite3
conn = sqlite3.connect(index_path)
cursor = conn.execute(
"SELECT name FROM sqlite_master WHERE type='table' AND name='semantic_chunks'"
)
has_semantic_table = cursor.fetchone() is not None
conn.close()
try:
with sqlite3.connect(index_path) as conn:
cursor = conn.execute(
"SELECT name FROM sqlite_master WHERE type='table' AND name='semantic_chunks'"
)
has_semantic_table = cursor.fetchone() is not None
except sqlite3.Error as e:
self.logger.error("Database check failed in vector search: %s", e)
return []
if not has_semantic_table:
self.logger.info(