Skip to content

Commit a085bc5

Browse files
committed
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.
1 parent 83aa96e commit a085bc5

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

aiomysql/cursors.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,8 @@ class SSCursor(Cursor):
598598

599599
async def close(self):
600600
conn = self._connection
601-
if conn is None:
601+
if conn is None or conn.closed:
602+
self._connection = None
602603
return
603604

604605
if self._result is not None and self._result is conn._result:

tests/test_sscursor.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,15 @@ async def test_ssursor(connection):
6565
await cursor.close()
6666

6767

68+
@pytest.mark.run_loop
69+
async def test_ssursor_conn_closed(connection):
70+
conn = connection
71+
cursor = await conn.cursor(SSCursor)
72+
73+
conn.close()
74+
await cursor.close()
75+
76+
6877
@pytest.mark.run_loop
6978
async def test_sscursor_fetchall(connection):
7079
conn = connection

0 commit comments

Comments
 (0)