pymysql.err.InternalError: Packet sequence number wrong - got X expected 1

See original GitHub issue

Describe 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

  1. Create models.py file:
from tortoise import Model, fields


class TableName(Model):
    id = fields.IntField(pk=True)
    some_field = fields.CharField(250)
  1. Create main.py file:
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)
  1. 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 .

  1. Run API with command:

docker run -d -p 6000:6000 image-name:tag-name

  1. After 3 hours of working, run curl query:

curl -i -X POST --data '{"some_field": "blabla"}' http://localhost:6000/rows

  1. 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:closed
  • Created 2 years ago
  • Comments:27 (10 by maintainers)

github_iconTop GitHub Comments

2reactions
ryanrain2016commented, Aug 12, 2021

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_recycle parameter. Just config it(little than 28800) in db_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.

0reactions
long2icecommented, Aug 24, 2021

嗯,这个issue先关了

Read more comments on GitHub >

github_iconTop Results From Across the Web

PyMysql error : Packet Sequence Number Wrong - got 1 ...
It can't share a connection created by main thread with all sub-threads. It will result in the following error: pymysql.err.InternalError: ...
Read more >
Packet sequence number wrong - got 1 expected 0 #73 - GitHub
I encountered the same error today. I tested it on two almost identical machines, one of them got the error but not the...
Read more >
pymysql.err.InternalError: Packet sequence number wrong
Just a warning that this error is a general database connectivity error that is caused for a multitude of reasons.
Read more >
Packet sequence number wrong (PyMySQL driver or…?)
pymysql.err.InternalError: Packet sequence number wrong - got 102 expected 8. I see this error in different variations for the packet sequence.
Read more >
pymysql.err.InternalError: Packet sequence number wrong
Describe the bug. I use aiohttp+tortoise-orm+mysql for simple API, which can only insert a row to a single mysql database table called " ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found