|
15 | 15 | from pymysql.constants import SERVER_STATUS |
16 | 16 | from pymysql.constants import CLIENT |
17 | 17 | from pymysql.constants import COMMAND |
| 18 | +from pymysql.constants import CR |
18 | 19 | from pymysql.constants import FIELD_TYPE |
19 | 20 | from pymysql.util import byte2int, int2byte |
20 | 21 | from pymysql.converters import (escape_item, encoders, decoders, |
@@ -470,7 +471,7 @@ async def set_charset(self, charset): |
470 | 471 |
|
471 | 472 | async def _connect(self): |
472 | 473 | # TODO: Set close callback |
473 | | - # raise OperationalError(2006, |
| 474 | + # raise OperationalError(CR.CR_SERVER_GONE_ERROR, |
474 | 475 | # "MySQL server has gone away (%r)" % (e,)) |
475 | 476 | try: |
476 | 477 | if self._unix_socket and self._host in ('localhost', '127.0.0.1'): |
@@ -569,6 +570,13 @@ async def _read_packet(self, packet_type=MysqlPacket): |
569 | 570 | # we increment in both write_packet and read_packet. The count |
570 | 571 | # is reset at new COMMAND PHASE. |
571 | 572 | if packet_number != self._next_seq_id: |
| 573 | + if packet_number == 0: |
| 574 | + # MySQL 8.0 sends error packet with seqno==0 when shutdown |
| 575 | + self.close() |
| 576 | + raise OperationalError( |
| 577 | + CR.CR_SERVER_LOST, |
| 578 | + "Lost connection to MySQL server during query") |
| 579 | + |
572 | 580 | raise InternalError( |
573 | 581 | "Packet sequence number wrong - got %d expected %d" % |
574 | 582 | (packet_number, self._next_seq_id)) |
@@ -596,10 +604,12 @@ async def _read_bytes(self, num_bytes): |
596 | 604 | data = await self._reader.readexactly(num_bytes) |
597 | 605 | except asyncio.IncompleteReadError as e: |
598 | 606 | msg = "Lost connection to MySQL server during query" |
599 | | - raise OperationalError(2013, msg) from e |
| 607 | + self.close() |
| 608 | + raise OperationalError(CR.CR_SERVER_LOST, msg) from e |
600 | 609 | except (IOError, OSError) as e: |
601 | 610 | msg = "Lost connection to MySQL server during query (%s)" % (e,) |
602 | | - raise OperationalError(2013, msg) from e |
| 611 | + self.close() |
| 612 | + raise OperationalError(CR.CR_SERVER_LOST, msg) from e |
603 | 613 | return data |
604 | 614 |
|
605 | 615 | def _write_bytes(self, data): |
|
0 commit comments