Skip to content
Merged
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
27 changes: 3 additions & 24 deletions api/duckdb_pool_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,31 +142,10 @@ def get_connection(self, timeout: float = 5.0):
raise

finally:
# CRITICAL: Always return and clean connection
# CRITICAL: Always return connection to pool
# DuckDB connections are stateless - no need to test or reset
if conn is not None:
try:
# Reset connection state (clears DuckDB internal caches)
# Use a simple query to clear any cached results
result = conn.execute("SELECT 1").fetchall()
del result

# Force aggressive garbage collection to release memory
# This is critical for preventing memory leaks from DuckDB result sets
collected = gc.collect()

# Log GC only for significant collections (reduces log noise)
if collected > 100:
logger.debug(f"Connection cleanup: collected {collected} objects")

except Exception as e:
# If cleanup fails, connection may be closed - don't return it to pool
logger.warning(f"Connection cleanup failed ({e}) - connection not returned to pool")
conn = None # Mark as invalid so we don't return it

finally:
# Always return connection to pool (if still valid)
if conn is not None:
self.pool.put(conn)
self.pool.put(conn)

def get_metrics(self) -> Dict[str, Any]:
"""
Expand Down