Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Packet sequence number wrong #422

Closed
ghost opened this issue Jan 26, 2016 · 16 comments
Closed

Packet sequence number wrong #422

ghost opened this issue Jan 26, 2016 · 16 comments

Comments

@ghost
Copy link

ghost commented Jan 26, 2016

I've been using PyMySQL 0.6.6 for a while now with no issues. However after updating to 0.7.1, I'm getting this error a lot, and PyMySQL has become completely unusable for me because of it.

  File "/usr/local/lib/python3.4/dist-packages/pymysql/cursors.py", line 146, in execute
    result = self._query(query)
  File "/usr/local/lib/python3.4/dist-packages/pymysql/cursors.py", line 296, in _query
    conn.query(q)
  File "/usr/local/lib/python3.4/dist-packages/pymysql/connections.py", line 819, in query
    self._affected_rows = self._read_query_result(unbuffered=unbuffered)
  File "/usr/local/lib/python3.4/dist-packages/pymysql/connections.py", line 1001, in _read_query_result
    result.read()
  File "/usr/local/lib/python3.4/dist-packages/pymysql/connections.py", line 1285, in read
    first_packet = self.connection._read_packet()
  File "/usr/local/lib/python3.4/dist-packages/pymysql/connections.py", line 952, in _read_packet
    (packet_number, self._next_seq_id))
pymysql.err.InternalError: Packet sequence number wrong - got 101 expected 1

The packet numbers vary, but I'm getting this for every query now, no matter how simple.
Things will work as expected for a few minutes after starting my script, but soon after these errors will begin appearing and no queries will work anymore.

I'm not sure if there's something I can do to resolve this in my script (some changes made in newer versions that I need to accommodate for) or if I should just figure out how to install the older version as a temporary solution. As it is now, this is a complete showstopper for me.

@methane
Copy link
Member

methane commented Jan 26, 2016

Please show me your code using PyMySQL.
It may means old PyMySQL works accidentlly.

@ghost
Copy link
Author

ghost commented Jan 26, 2016

I can't provide the full code, but I can show you how I'm using PyMySQL:

# single row query
cursor.execute( "SELECT * FROM uploads WHERE ID = '%s'" % fileid )
row = cursor.fetchone()
print( row['Filename'] )

# multiple row query
cursor.execute( "SELECT * FROM uploads WHERE Status < 4" )
result = cursor.fetchall()
for row in result:
    print( row['ID'], row['Filename'] )

# update query
cursor.execute( "UPDATE uploads SET Status = '%s' WHERE ID = '%s'" % ( status, fileid ) )

All other queries in my code are just like these three.

The only weird thing I can think of is maybe my connection code, however this worked fine in 0.6.6:

connection = pymysql.connect( host=config.db_host, port=3306, user=config.db_username, passwd=config.db_password, db=config.db_database )
connection.autocommit( 1 )
cursor = connection.cursor( pymysql.cursors.DictCursor )

@methane
Copy link
Member

methane commented Jan 26, 2016

Is your application single thread?

@ghost
Copy link
Author

ghost commented Jan 26, 2016

Ahh, it's multithreaded. Could this be the problem? Do I need one connection per thread?

@methane
Copy link
Member

methane commented Jan 26, 2016

Yes.

@methane
Copy link
Member

methane commented Jan 26, 2016

See https://www.python.org/dev/peps/pep-0249/#threadsafety

PyMySQL's threadsafety is 1.

@ghost
Copy link
Author

ghost commented Jan 26, 2016

Oops. My mistake! Didn't realize that. Makes sense.
Funny how the problem didn't present itself in 0.6.6

Thanks for all your help!

@EaconTang
Copy link

Thanks for the issue, I finally fix my problem which fucked me these days!

@kadnan
Copy link

kadnan commented Jun 20, 2017

@EaconTang @dougty Can you please tell how did you fix it?

@ghost
Copy link
Author

ghost commented Jun 20, 2017

@kadnan You need separate pymysql connections for each thread

@kadnan
Copy link

kadnan commented Jun 20, 2017

@dougty I am rather using multiprocessing than multithreading. My code look likes:

with Pool(2) as p:
            result = p.map(parse, links)

here parse() method performs some MYSL INSERT/UPDATE operations where it's getting stuck.

Are you saying I explicitly call pysql.connect() for each process?

@ghost
Copy link
Author

ghost commented Jun 20, 2017

@kadnan Yes, you need one pysql.connect() for each process/thread. As far as I know that's the only way to fix it. PyMySQL is not thread safe, so the same connection can't be used across multiple threads.

@ahmedsaalah
Copy link

ahmedsaalah commented Dec 6, 2017

@dougty @methane could you tell me how make pysql.connect() for each process/thread ?

@jkklee
Copy link

jkklee commented Dec 21, 2017

@dougty @ahmedsaalah I'v faced this problem before, then I created a module which provide connection pools in multi-threads mode. see here
@methane my code maybe ugly, but I'll be appreciate if you can make some suggestions.

@methane
Copy link
Member

methane commented Dec 21, 2017

My suggestion is using existing robust system like SQLAlchemy's engine.

@MyJoiT
Copy link

MyJoiT commented Sep 15, 2018

@methane

SQLAlchemy also has this problem in multithread if you use one session,like this:

# base.py
engine = create_engine(
    get_db_str(),
    echo=False,
    pool_recycle=300,
    poolclass=QueuePool,
    pool_size=10,
    max_overflow=10,
    pool_timeout=30,
    listeners=[Reconnect()]
)

Session = sessionmaker(bind=engine)

# user1.py
from .base import Session
session1 = Session()
session1.query

# user2.py
from .base import Session
session2 = Session()
session2.query

I use ab command send 1000 requests to system. System raise that exception after some requests.

This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants