Skip to content

Commit e81f356

Browse files
authored
Merge pull request #339 from TimothyFitz/conn-pool-check-exception
Don't reuse connections when StreamReader has an exception
2 parents 272196a + b19d816 commit e81f356

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

aiomysql/pool.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ async def _fill_free_pool(self, override_min):
148148
n = 0
149149
while n < free_size:
150150
conn = self._free[-1]
151-
if conn._reader.at_eof():
151+
if conn._reader.at_eof() or conn._reader.exception():
152152
self._free.pop()
153153
conn.close()
154154

tests/test_pool.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,7 @@ async def test_cancelled_connection(pool_creator, loop):
493493
assert list(res) == [(2, 0)]
494494

495495

496+
@pytest.mark.run_loop
496497
async def test_pool_with_connection_recycling(pool_creator, loop):
497498
pool = await pool_creator(minsize=1, maxsize=1, pool_recycle=3)
498499
async with pool.get() as conn:
@@ -509,3 +510,19 @@ async def test_pool_with_connection_recycling(pool_creator, loop):
509510
await cur.execute('SELECT 1;')
510511
val = await cur.fetchone()
511512
assert (1,) == val
513+
514+
515+
@pytest.mark.run_loop
516+
async def test_pool_drops_connection_with_exception(pool_creator, loop):
517+
pool = await pool_creator(minsize=1, maxsize=1)
518+
519+
async with pool.get() as conn:
520+
cur = await conn.cursor()
521+
await cur.execute('SELECT 1;')
522+
523+
connection, = pool._free
524+
connection._writer._protocol.connection_lost(IOError())
525+
526+
async with pool.get() as conn:
527+
cur = await conn.cursor()
528+
await cur.execute('SELECT 1;')

0 commit comments

Comments
 (0)