Skip to content

Commit 69cbfd0

Browse files
committed
Make sure idle never-acquired pool connections are closed due to inactivity
The `max_inactive_connection_lifetime` setting currently fails to apply to the connection which were never acquired because `_setup_inactive_callback()` is only called in `release()`. Fixes: MagicStack#389
1 parent a6b7775 commit 69cbfd0

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

‎asyncpg/pool.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ async def connect(self):
124124

125125
self._con=awaitself._pool._get_new_connection()
126126
self._generation=self._pool._generation
127+
self._maybe_cancel_inactive_callback()
128+
self._setup_inactive_callback()
127129

128130
asyncdefacquire(self) ->PoolConnectionProxy:
129131
ifself._conisNoneorself._con.is_closed():

‎tests/test_pool.py‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,24 @@ async def worker(pool):
668668

669669
self.assertGreaterEqual(N, 50)
670670

671+
asyncdeftest_pool_max_inactive_time_05(self):
672+
# Test that idle never-acquired connections abide by
673+
# the max inactive lifetime.
674+
asyncwithself.create_pool(
675+
database='postgres', min_size=2, max_size=2,
676+
max_inactive_connection_lifetime=0.2) aspool:
677+
678+
self.assertIsNotNone(pool._holders[0]._con)
679+
self.assertIsNotNone(pool._holders[1]._con)
680+
681+
awaitpool.execute('SELECT pg_sleep(0.3)')
682+
awaitasyncio.sleep(0.3, loop=self.loop)
683+
684+
self.assertIs(pool._holders[0]._con, None)
685+
# The connection in the second holder was never used,
686+
# but should be closed nonetheless.
687+
self.assertIs(pool._holders[1]._con, None)
688+
671689
asyncdeftest_pool_handles_inactive_connection_errors(self):
672690
pool=awaitself.create_pool(database='postgres',
673691
min_size=1, max_size=1)

0 commit comments

Comments
(0)