From a085bc53dea496d50b83c44d4638d0c052793b85 Mon Sep 17 00:00:00 2001 From: FichteFoll Date: Mon, 9 Sep 2024 16:52:48 +0200 Subject: [PATCH] SSCursor: Fix connection closed check Since the connection closes itself on an `asyncio.CancelledError`, it cannot be used afterwards. `SSCursor.close` attempts to clear some unfinished state first before closing using `Connection._finish_unbuffered_query`, but that call fails if the connection has been closed already and SSCursor itself does not properly check that. --- aiomysql/cursors.py | 3 ++- tests/test_sscursor.py | 9 +++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/aiomysql/cursors.py b/aiomysql/cursors.py index 35d29d72..164fef95 100644 --- a/aiomysql/cursors.py +++ b/aiomysql/cursors.py @@ -598,7 +598,8 @@ class SSCursor(Cursor): async def close(self): conn = self._connection - if conn is None: + if conn is None or conn.closed: + self._connection = None return if self._result is not None and self._result is conn._result: diff --git a/tests/test_sscursor.py b/tests/test_sscursor.py index de9da609..36f2a389 100644 --- a/tests/test_sscursor.py +++ b/tests/test_sscursor.py @@ -65,6 +65,15 @@ async def test_ssursor(connection): await cursor.close() +@pytest.mark.run_loop +async def test_ssursor_conn_closed(connection): + conn = connection + cursor = await conn.cursor(SSCursor) + + conn.close() + await cursor.close() + + @pytest.mark.run_loop async def test_sscursor_fetchall(connection): conn = connection