Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix closed MySQL 8.0 connections not always being detected properly
ensure connections are closed properly when the server connection was lost
  • Loading branch information
Nothing4You committed Jan 22, 2022
commit 1c3468661c31be8186b90b97d6030089cdc4bf1d
3 changes: 3 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ To be included in 1.0.0 (unreleased)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

* Don't send sys.argv[0] as program_name to MySQL server by default #620
* Fix timed out MySQL 8.0 connections raising InternalError rather than OperationalError #660
* Fix timed out MySQL 8.0 connections being returned from Pool #660
* Ensure connections are properly closed before raising OperationalError when the server connection is lost #660


0.0.22 (2021-11-14)
Expand Down
16 changes: 13 additions & 3 deletions aiomysql/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from pymysql.constants import SERVER_STATUS
from pymysql.constants import CLIENT
from pymysql.constants import COMMAND
from pymysql.constants import CR
from pymysql.constants import FIELD_TYPE
from pymysql.util import byte2int, int2byte
from pymysql.converters import (escape_item, encoders, decoders,
Expand Down Expand Up @@ -468,7 +469,7 @@ async def set_charset(self, charset):

async def _connect(self):
# TODO: Set close callback
# raise OperationalError(2006,
# raise OperationalError(CR.CR_SERVER_GONE_ERROR,
# "MySQL server has gone away (%r)" % (e,))
try:
if self._unix_socket and self._host in ('localhost', '127.0.0.1'):
Expand Down Expand Up @@ -567,6 +568,13 @@ async def _read_packet(self, packet_type=MysqlPacket):
# we increment in both write_packet and read_packet. The count
# is reset at new COMMAND PHASE.
if packet_number != self._next_seq_id:
if packet_number == 0:
# MySQL 8.0 sends error packet with seqno==0 when shutdown
self.close()
raise OperationalError(
CR.CR_SERVER_LOST,
"Lost connection to MySQL server during query")

raise InternalError(
"Packet sequence number wrong - got %d expected %d" %
(packet_number, self._next_seq_id))
Expand Down Expand Up @@ -594,10 +602,12 @@ async def _read_bytes(self, num_bytes):
data = await self._reader.readexactly(num_bytes)
except asyncio.IncompleteReadError as e:
msg = "Lost connection to MySQL server during query"
raise OperationalError(2013, msg) from e
self.close()
raise OperationalError(CR.CR_SERVER_LOST, msg) from e
except (IOError, OSError) as e:
msg = "Lost connection to MySQL server during query (%s)" % (e,)
raise OperationalError(2013, msg) from e
self.close()
raise OperationalError(CR.CR_SERVER_LOST, msg) from e
return data

def _write_bytes(self, data):
Expand Down
7 changes: 6 additions & 1 deletion aiomysql/pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ async def _acquire(self):
await self._cond.wait()

async def _fill_free_pool(self, override_min):
# iterate over free connections and remove timeouted ones
# iterate over free connections and remove timed out ones
free_size = len(self._free)
n = 0
while n < free_size:
Expand All @@ -152,6 +152,11 @@ async def _fill_free_pool(self, override_min):
self._free.pop()
conn.close()

# TODO: fixme, we should not use internal attributes
elif conn._reader._eof:
self._free.pop()
conn.close()

elif (self._recycle > -1 and
self._loop.time() - conn.last_usage > self._recycle):
self._free.pop()
Expand Down