pymysql.err.InternalError: Packet sequence number wrong - got X expected 1
See original GitHub issueDescribe the bug I use aiohttp+tortoise-orm+mysql for simple API, which can only insert a row to a single mysql database table called “TableName” and read the rows from it. If my API is idle for some time, say, 3 hours, and I do API request which should return me table rows, it shows “pymysql.err.InternalError: Packet sequence number wrong - got 0 expected 1” (not necessarily 0, it could be other number) error and status 500 instead of returning rows.
It shows this error when run this piece of code:
rows = await TableName.filter(some_field=some_field)
To Reproduce
- Create
models.pyfile:
from tortoise import Model, fields
class TableName(Model):
id = fields.IntField(pk=True)
some_field = fields.CharField(250)
- Create
main.pyfile:
from aiohttp import web
from tortoise.contrib.aiohttp import register_tortoise
from models import TableName
async def list_rows(request):
data = await request.json()
try:
some_field = data['some_field']
except KeyError:
return web.json_response({'message': f'Not all fields were specified'}, status=400)
rows = await TableName.filter(some_field=some_field)
rows_json = [row.__dict__ for row in rows]
return web.json_response(rows_json)
app = web.Application()
app.add_routes([
web.post("/rows", list_rows)
])
register_tortoise(
app, db_url="mysql://user:password@mysql:3306/any-database-name", modules={"models": ["models"]},
generate_schemas=True
)
if __name__ == "__main__":
web.run_app(app, port=6000)
- pack it into docker container with such Dockerfile:
FROM python:3.9.5
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
CMD ["python", "main.py"]
docker build -t image-name:tag-name .
- Run API with command:
docker run -d -p 6000:6000 image-name:tag-name
- After 3 hours of working, run curl query:
curl -i -X POST --data '{"some_field": "blabla"}' http://localhost:6000/rows
- curl will return 500 error, inside container’s logs you will see error:
pymysql.err.InternalError: Packet sequence number wrong - got 35 expected 1
Expected behavior curl query should return json with rows’ values in it.
Additional context Full trace:
ERROR:aiohttp.server:Error handling request
Traceback (most recent call last):
File "/usr/local/lib/python3.9/site-packages/tortoise/backends/mysql/client.py", line 44, in translate_exceptions_
return await func(self, *args)
File "/usr/local/lib/python3.9/site-packages/tortoise/backends/mysql/client.py", line 199, in execute_query
await cursor.execute(query, values)
File "/usr/local/lib/python3.9/site-packages/aiomysql/cursors.py", line 239, in execute
await self._query(query)
File "/usr/local/lib/python3.9/site-packages/aiomysql/cursors.py", line 457, in _query
await conn.query(q)
File "/usr/local/lib/python3.9/site-packages/aiomysql/connection.py", line 428, in query
await self._read_query_result(unbuffered=unbuffered)
File "/usr/local/lib/python3.9/site-packages/aiomysql/connection.py", line 622, in _read_query_result
await result.read()
File "/usr/local/lib/python3.9/site-packages/aiomysql/connection.py", line 1105, in read
first_packet = await self.connection._read_packet()
File "/usr/local/lib/python3.9/site-packages/aiomysql/connection.py", line 574, in _read_packet
raise InternalError(
pymysql.err.InternalError: Packet sequence number wrong - got 0 expected 1
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/lib/python3.9/site-packages/aiohttp/web_protocol.py", line 422, in _handle_request
resp = await self._request_handler(request)
File "/usr/local/lib/python3.9/site-packages/aiohttp/web_app.py", line 499, in _handle
resp = await handler(request)
File "//main.py", line 37, in list_rowa
rows = await TableName.filter(some_field=some_field)
File "/usr/local/lib/python3.9/site-packages/tortoise/queryset.py", line 879, in _execute
instance_list = await self._db.executor_class(
File "/usr/local/lib/python3.9/site-packages/tortoise/backends/base/executor.py", line 124, in execute_select
_, raw_results = await self.db.execute_query(query.get_sql())
File "/usr/local/lib/python3.9/site-packages/tortoise/backends/mysql/client.py", line 52, in translate_exceptions_
raise OperationalError(exc)
tortoise.exceptions.OperationalError: Packet sequence number wrong - got 0 expected 1
Issue Analytics
- State:
- Created 2 years ago
- Comments:27 (10 by maintainers)
Top Related StackOverflow Question
mysql server will close the connection after a long idle time, default is 28800 seconds, that is 8 hours. aiomysql/asyncmy pool can recyle it by passing a
pool_recycleparameter. Just config it(little than 28800) indb_config['connections']['default']['credentials']. that’s ok. but It does’t work with ‘db_url’, since pool_recycle will be str when passing though ‘db_url’. tortoise-orm document has hidden a lot of paramenter to aiomysql/asyncmy pool.嗯,这个issue先关了