PyMySQL/Tornado-MySQL

OperationalError: (2006, 'MySQL server has gone away (Stream is closed)')

ishansharma29 opened this issue · 4 comments

My code goes like this:

            print "trying to connect to MySQL"
            conn = yield tornado_mysql.connect(host='127.0.0.1',
                                               user='root',
                                               db='camera')
            cur = conn.cursor()
            print cur
            query = "INSERT INTO ...."
            yield cur.execute(query)                         # this is getting executed!
            cur.close()
            # if conn is not None:
            conn.commit()
            yield conn.close_async()                     # tried conn.close() as well, Both throws an exception:
ERROR:tornado.application:Future exception was never retrieved: Traceback (most recent call last):
  File "/Users/ishansharma/workplace/server-camera/venv/lib/python2.7/site-packages/tornado/gen.py", line 1014, in run
    yielded = self.gen.throw(*exc_info)
  File "/Users/ishansharma/workplace/server-camera/venv/lib/python2.7/site-packages/tornado_mysql/connections.py", line 679, in commit
    yield self._read_ok_packet()
  File "/Users/ishansharma/workplace/server-camera/venv/lib/python2.7/site-packages/tornado/gen.py", line 1008, in run
    value = future.result()
  File "/Users/ishansharma/workplace/server-camera/venv/lib/python2.7/site-packages/tornado/concurrent.py", line 232, in result
    raise_exc_info(self._exc_info)
  File "/Users/ishansharma/workplace/server-camera/venv/lib/python2.7/site-packages/tornado/gen.py", line 1014, in run
    yielded = self.gen.throw(*exc_info)
  File "/Users/ishansharma/workplace/server-camera/venv/lib/python2.7/site-packages/tornado_mysql/connections.py", line 654, in _read_ok_packet
    pkt = yield self._read_packet()
  File "/Users/ishansharma/workplace/server-camera/venv/lib/python2.7/site-packages/tornado/gen.py", line 1008, in run
    value = future.result()
  File "/Users/ishansharma/workplace/server-camera/venv/lib/python2.7/site-packages/tornado/concurrent.py", line 232, in result
    raise_exc_info(self._exc_info)
  File "/Users/ishansharma/workplace/server-camera/venv/lib/python2.7/site-packages/tornado/gen.py", line 1014, in run
    yielded = self.gen.throw(*exc_info)
  File "/Users/ishansharma/workplace/server-camera/venv/lib/python2.7/site-packages/tornado_mysql/connections.py", line 845, in _read_packet
    raise OperationalError(2006, "MySQL server has gone away (%s)" % (e,))
OperationalError: (2006, 'MySQL server has gone away (Stream is closed)')

I suppose you closed connection twice.
But I can't confirm since your code is not reproducible code.
No one but you can investigate your problem is caused by Tornado-MySQL or your code.

I think the issue is with Tornado-MySQL because the lines I've written, these are the only lines of code that are being executed and throwing this error. The error is thrown when it executes conn.close() .

In my code, I don't see the connection being closed at any point other then the last line.
Can you try to replicate this issue? I think to do so, all you have to do is change the db='camera' (with your database name) line and complete the insert query I've left half written.

I tried to check Tornado-MySQL code, and I couldn't find any point where the connection is closed. I don't know if making the connection async is making the connection go away or there is an issue with my code (which I really doubt so) or the issue is with Tornado-MySQL.

I think to do so, all you have to do is change the db='camera' (with your database name) line

OK, but...

and complete the insert query I've left half written.

Why don't you do?

And there is need to make your code to valid Tornado application.
I don't use tornado for a long time.
I need to read doc to know how to run coroutine in simple script.

Additionally, if I can't reproduce, I waste my energy and time.