Skip to content

Commit f43713e

Browse files
Ignacio Van Droogenbroeckclaude
andcommitted
fix: Handle closed DuckDB connections gracefully in pool cleanup
When a query fails and closes the connection, the pool cleanup was trying to execute SELECT 1 on the closed connection, causing "Connection already closed!" errors on subsequent queries. Now if a connection is invalid during cleanup, we: 1. Close it properly 2. Create a fresh connection 3. Return the fresh connection to the pool This ensures the pool always contains valid connections and prevents cascading connection errors. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 3215070 commit f43713e

1 file changed

Lines changed: 15 additions & 7 deletions

File tree

api/duckdb_pool_simple.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,8 @@ def get_connection(self, timeout: float = 5.0):
145145
# CRITICAL: Always return and clean connection
146146
if conn is not None:
147147
try:
148-
# Reset connection state (clears DuckDB internal caches)
149-
# Use a simple query to clear any cached results
148+
# Test if connection is still valid
149+
# If the connection is closed, this will raise an exception
150150
result = conn.execute("SELECT 1").fetchall()
151151
del result
152152

@@ -158,13 +158,21 @@ def get_connection(self, timeout: float = 5.0):
158158
if collected > 100:
159159
logger.debug(f"Connection cleanup: collected {collected} objects")
160160

161+
# Connection is valid, return to pool
162+
self.pool.put(conn)
163+
161164
except Exception as e:
162-
# If cleanup fails, log but don't crash
163-
logger.debug(f"Connection cleanup warning: {e}")
165+
# Connection is invalid/closed - create a fresh one
166+
logger.warning(f"Connection invalid during cleanup ({e}), creating fresh connection")
167+
try:
168+
# Close the bad connection properly
169+
conn.close()
170+
except Exception:
171+
pass
164172

165-
finally:
166-
# Always return connection to pool
167-
self.pool.put(conn)
173+
# Create and return a fresh connection to the pool
174+
fresh_conn = duckdb.connect(self.db_path)
175+
self.pool.put(fresh_conn)
168176

169177
def get_metrics(self) -> Dict[str, Any]:
170178
"""

0 commit comments

Comments
 (0)