Skip to content

Commit 9244eb7

Browse files
refactor: deduplicate pool close() by calling _drain_idle(), fix is True style
pool.close() had a drain loop character-for-character identical to _drain_idle(). Replace with a call to _drain_idle(). Also change _reset_connection's `conn._in_transaction is True` to the truthiness check `if conn._in_transaction:` for consistency with the rest of the codebase. Fix two mock tests that relied on the `is True` behavior (MagicMock attributes are truthy but not `is True`). Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 035c64e commit 9244eb7

2 files changed

Lines changed: 4 additions & 14 deletions

File tree

src/dqliteclient/pool.py

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ async def _reset_connection(self, conn: DqliteConnection) -> bool:
180180
Returns True if the connection is clean and can be reused,
181181
False if it should be destroyed.
182182
"""
183-
if conn._in_transaction is True:
183+
if conn._in_transaction:
184184
try:
185185
await conn.execute("ROLLBACK")
186186
except Exception:
@@ -248,19 +248,7 @@ async def close(self) -> None:
248248
in-flight tasks before calling close().
249249
"""
250250
self._closed = True
251-
252-
# Close idle connections
253-
while not self._pool.empty():
254-
try:
255-
conn = self._pool.get_nowait()
256-
except asyncio.QueueEmpty:
257-
break
258-
try:
259-
await conn.close()
260-
except BaseException:
261-
pass
262-
finally:
263-
self._size -= 1
251+
await self._drain_idle()
264252

265253
# In-use connections are closed by acquire()'s cleanup when they
266254
# return — the else branch checks _closed and closes instead of

tests/test_pool.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ async def test_cancellation_returns_healthy_connection_to_pool(self) -> None:
142142
mock_conn.is_connected = True
143143
mock_conn.connect = AsyncMock()
144144
mock_conn.close = AsyncMock()
145+
mock_conn._in_transaction = False
145146

146147
with patch.object(pool._cluster, "connect", return_value=mock_conn):
147148
await pool.initialize()
@@ -437,6 +438,7 @@ async def test_user_exception_preserves_healthy_connection(self) -> None:
437438
mock_conn.is_connected = True
438439
mock_conn.connect = AsyncMock()
439440
mock_conn.close = AsyncMock()
441+
mock_conn._in_transaction = False
440442

441443
with patch.object(pool._cluster, "connect", return_value=mock_conn):
442444
await pool.initialize()

0 commit comments

Comments
 (0)