From 04a11794cb19dc6dd683c69411060e9175dabd08 Mon Sep 17 00:00:00 2001 From: Ragim Ibragimov Date: Mon, 3 Jun 2024 10:58:30 +0300 Subject: [PATCH] reset connection state --- asynch/pool.py | 1 + tests/test_pool.py | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/asynch/pool.py b/asynch/pool.py index 6c574c9..67b516b 100644 --- a/asynch/pool.py +++ b/asynch/pool.py @@ -144,6 +144,7 @@ async def release(self, connection: Connection): if self._closing: await connection.close() else: + connection._connection.reset_state() self._free.append(connection) fut = self._loop.create_task(self._wakeup()) return fut diff --git a/tests/test_pool.py b/tests/test_pool.py index 663ea42..5eae065 100644 --- a/tests/test_pool.py +++ b/tests/test_pool.py @@ -1,4 +1,6 @@ import pytest +from socket import gaierror +from asyncio import gather from asynch.connection import Connection @@ -30,3 +32,33 @@ async def test_acquire(pool): await pool.release(conn) assert pool.freesize == 1 assert pool.size == 1 + + +@pytest.mark.asyncio +async def test_clean_pool_connection_on_error(pool): + assert pool + + async def raise_net_error(): + raise gaierror + + for _ in range(100): + async with pool.acquire() as conn: + async with conn.cursor() as cursor: + try: + # simulate network error while executing the query + await gather( + cursor.execute('SELECT 1'), + raise_net_error() + ) + except gaierror: + pass + + assert conn._connection.writer is None + assert conn._connection.reader is None + assert conn._connection.block_reader is None + assert conn._connection.block_reader_raw is None + assert conn._connection.block_writer is None + assert conn._connection.connected is False + assert conn._connection.client_trace_context is None + assert conn._connection.server_info is None + assert conn._connection.is_query_executing is False