Cancelling a task during MySQL protocol I/O does not prove that the server stopped or that the response was drained. With a direct Connector/Python 26.10.0 connection, discard that socket with shutdown(); do not ping it, close its cursor, send QUIT, or reuse it.
Last updated: November 13, 2026.
import asyncio
from mysql.connector.aio import connect
async def close_normally(connection, cursor):
async with asyncio.timeout(1):
if cursor is not None:
await cursor.close()
await connection.close()
async def fetch_all(config, sql, params=()):
connection = await connect(**config)
cursor = None
try:
async with asyncio.timeout(2):
cursor = await connection.cursor()
await cursor.execute(sql, params)
rows = await cursor.fetchall()
except (TimeoutError, asyncio.CancelledError) as original:
try:
# 26.10.0: close the socket without cursor cleanup, ping, or QUIT.
async with asyncio.timeout(1):
await connection.shutdown()
except asyncio.CancelledError:
raise
except Exception as cleanup_error:
original.add_note(f"shutdown failed: {cleanup_error!r}")
raise
except BaseException as original:
try:
await close_normally(connection, cursor)
except asyncio.CancelledError:
raise
except Exception as cleanup_error:
original.add_note(f"cleanup failed: {cleanup_error!r}")
raise
else:
await close_normally(connection, cursor)
return rowsThis example owns one direct connection. Cursor creation, execution, and fetching share the same Python 3.11+ deadline. Each path cleans up once: successful work and ordinary errors use bounded graceful cleanup; timeout or cancellation during protocol I/O uses shutdown() and re-raises the original exception.
Why shutdown is different from close
The example targets Python 3.13.16 and mysql-connector-python 26.10.0; it requires Python 3.11 or newer for asyncio.timeout().
In that connector release, is_connected() is asynchronous and sends a ping. It is the wrong probe after interrupted I/O. close() first closes tracked cursors and attempts QUIT, whereas shutdown() closes the socket without QUIT. The 26.10.0 socket implementation closes and aborts its transport before awaiting wait_closed(), so a repeated cancellation cannot make the affected connection safe for reuse. The code propagates cancellation and creates no background cleanup task.
The linked issue #112 is a useful reproduction, not proof that every release is affected or that any proposed patch works. Confirm the behavior again when changing connector versions.
Keep pooled connections separate
Do not pass a pooled wrapper to this direct-connection helper. In Connector/Python 26.10.0, PooledMySQLConnection.close() returns its physical connection to the pool after an optional ping and session reset; it does not document per-checkout invalidation. This article therefore does not invent an eviction call or reach into the wrapper’s private _cnx attribute. Use direct connections for cancellable work, or follow a pool’s documented, version-specific discard operation.
For runtime verification, confirm that a slow query has reached MySQL before timing out or cancelling it. Cover cancellation during execution and fetching, cursor-creation failure, and repeated cancellation during cleanup. The discarded object must reject reuse; a separately opened replacement must return exact sentinel column names and values. Run with coroutine warnings enabled and check for leaked server sessions and masked exceptions.
Server-side statement limits can reduce wasted work, but they do not drain a client socket. Continue with MySQL connection limits, recovering failed MySQL connections, and reading database error details.