question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

InternalError: Packet sequence number wrong

See original GitHub issue

Hello,

I came across an issue on production that I can’t replicate in the local environment. The same code path often works, but sometimes throws this error. I don’t understand, why this is happening.

I believe there is an issue with multi-thread nature of fastapi and internal usage of pymysql by databases. I’m pretty sure pymysql is single threaded.

But databases was supposed to support asyncio, correct? What am I doing wrong, please?

crud_server.py

db = databases.Database(settings.SQLALCHEMY_DATABASE_URI)

async def get_servers(is_maintenance: bool) -> List[Server]:
    query = ServerTable.select().where(ServerTable.c.is_maintenance == is_maintenance)
    if not db.is_connected:
        await db.connect()
    return await db.fetch_all(query)

ServerTable.py

metadata = MetaData(schema=settings.DBNAME_MAIN)

ServerTable = sqlalchemy.Table(
    "server",
    metadata,
    Column("id", String(15), primary_key=True),
    Column("name", String(100), nullable=False),
    Column("country", String(100), nullable=False),
    Column("group_master", String(15), nullable=True),
    Column("group_slave", String(15), nullable=True, index=True),
    Column("ip", String(length=45), nullable=False),
    Column("max_connections", Integer(), nullable=False),
    Column(
        "is_maintenance",
        Boolean(),
        nullable=False,
        server_default=text("0"),
        default=text("0"),
        index=True,
    ),
    Column("dow_maintenance", String(3), nullable=True),
    Column("hour_maintenance", Integer(), nullable=True),
    Column("minutes_maintenance", Integer(), nullable=True),
    Column("datetime_maintenance", TIMESTAMP(), nullable=True),
)

Error:

  File "/home/admin/tg/app/routers/server_status.py", line 25, in server_status
    servers = await crud_server.get_servers(is_maintenance=False)
  File "/home/admin/tg/app/database/crud_server.py", line 15, in get_servers
    return await db.fetch_all(query)
  File "/home/admin/.pyenv/versions/3.9.4/envs/venv/lib/python3.9/site-packages/databases/core.py", line 140, in fetch_all
    return await connection.fetch_all(query, values)
  File "/home/admin/.pyenv/versions/3.9.4/envs/venv/lib/python3.9/site-packages/databases/core.py", line 239, in fetch_all
    return await self._connection.fetch_all(built_query)
  File "/home/admin/.pyenv/versions/3.9.4/envs/venv/lib/python3.9/site-packages/databases/backends/mysql.py", line 108, in fetch_all
    await cursor.execute(query, args)
  File "/home/admin/.pyenv/versions/3.9.4/envs/venv/lib/python3.9/site-packages/aiomysql/cursors.py", line 239, in execute
    await self._query(query)
  File "/home/admin/.pyenv/versions/3.9.4/envs/venv/lib/python3.9/site-packages/aiomysql/cursors.py", line 457, in _query
    await conn.query(q)
  File "/home/admin/.pyenv/versions/3.9.4/envs/venv/lib/python3.9/site-packages/aiomysql/connection.py", line 428, in query
    await self._read_query_result(unbuffered=unbuffered)
  File "/home/admin/.pyenv/versions/3.9.4/envs/venv/lib/python3.9/site-packages/aiomysql/connection.py", line 622, in _read_query_result
    await result.read()
  File "/home/admin/.pyenv/versions/3.9.4/envs/venv/lib/python3.9/site-packages/aiomysql/connection.py", line 1105, in read
    first_packet = await self.connection._read_packet()
  File "/home/admin/.pyenv/versions/3.9.4/envs/venv/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

I would really appreciate a hint, what I’m doing wrong. Thank you so much

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:19 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
frankie567commented, May 20, 2021

I suppose switching to the latest SQLAlchemy 14, which supports asynchio won’t help either, because the underlying driver to MySQL database is buggy, correct?

Yes, unfortunately. SQLALchemy 14 expects you to provide your own asyncio-enabled driver… And aiomysql seems the only option for now.

1reaction
houmiecommented, May 12, 2021

Hi Frankie,

I no longer see the issue reoccurring. Thanks for your help. I will close the issue and reopen again if it happens again.

In a nutshell two things may have helped.

  1. Update of requirements.txt brought in latest FastAPI and SqlAlchemy 1.4, which may have fixed the issue.
  2. As you said the conditional check of database connection is creating side effects.

Last but not least the code is much cleaner by using asgi-lifespan. 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

pymysql.err.InternalError: Packet sequence number wrong
I think the reason was that you using multithread to connect to mysql at one time, the mysql server return the wrong response...
Read more >
Packet sequence number wrong · Issue #422 - GitHub
InternalError : Packet sequence number wrong - got 101 expected 1. The packet numbers vary, but I'm getting this for every query now, ......
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
Just a warning that this error is a general database connectivity error that is caused for a multitude of reasons.
Read more >
pymysql.err.InternalError: Packet sequence number wrong ...
InternalError : Packet sequence number wrong - got 102 expected 77". I attached the full log after this mail. I check the provided...
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