aiomysql icon indicating copy to clipboard operation
aiomysql copied to clipboard

Invalidate ResultProxy after releasing a connection

Open zarybnicky opened this issue 6 years ago • 2 comments

At work, we spent two days debugging an issue where we had several threads reading from MySQL and we found a non-deterministic bug when after a few hours of running, our service encountered the exception pymysql.err.InternalError: Packet sequence number wrong - got 116 expected 1.

A part of the code reading from MySQL was badly written and looked like this:

async with mysql.acquire() as conn:
    result = conn.execute(text(sql_string))
# [...]
async for row in result:
    pass  # do something with it

After finding the code, it was trivial to fix it (and this was the last place we looked...) so that we don't try to read from the connection after releasing it back into the pool, but it would've been much easier if all ResultProxys were invalidated in such a way that all attempts to read data that's not already cached would result in an exception.

zarybnicky avatar Jan 15 '19 15:01 zarybnicky

Hi there, I'm not sure if I should have opened a new issue to report this but the error we're getting on all of our services using aiomysql are pretty mush the same than what @zarybnicky is reporting.

This error occurs for different endpoints => different SQL queries. So here is what is looks like :

File "***************************.py", line 739, 
in ****** await cur.execute(sql_checktable) File "/usr/local/lib/python3.7/site-packages/aiomysql/cursors.py", line 239, 
in execute await self._query(query) File "/usr/local/lib/python3.7/site-packages/aiomysql/cursors.py", line 457, 
in _query await conn.query(q) File "/usr/local/lib/python3.7/site-packages/aiomysql/connection.py", line 428, 
in query await self._read_query_result(unbuffered=unbuffered) File "/usr/local/lib/python3.7/site-packages/aiomysql/connection.py", line 622, 
in _read_query_result await result.read() File "/usr/local/lib/python3.7/site-packages/aiomysql/connection.py", line 1105, 
in read first_packet = await self.connection._read_packet() File "/usr/local/lib/python3.7/site-packages/aiomysql/connection.py", line 576, 
in _read_packet (packet_number, self._next_seq_id)) pymysql.err.InternalError: Packet sequence number wrong - got 97 expected 1

So in a docker container we are running a python web application with this stack that sends queries to a MySQL 5.7 instance ran in Google Cloud SQL:

  • Python 3.7
  • Gunicorn 19.9.0
  • uvloop 0.12.2
  • aiomysql 0.0.20
  • aiohttp 3.5.4
  • aiojobs 0.2.1

Our implementation of aiomysql is exactly this no matter where in the code:

async with connection_pool.acquire() as conn:
    async with conn.cursor() as cur:
        await cur.execute(sql_query, sql_params)
        query_result = await cur.fetchall()

What do you guys think about all this ?

rberlier avatar Jul 26 '19 10:07 rberlier

@rberlier i have same problem , have you sloved this ?

swordjjjkkk avatar Sep 17 '21 06:09 swordjjjkkk