snower / TorMySQL

The highest performance asynchronous MySQL driver by PyMySQL
MIT License
308 stars 63 forks source link

Already reading & Command Out of Sync #21

Closed fatpo closed 7 years ago

fatpo commented 7 years ago

a bug make me confuse, the whole process of this bug like:

1: AssertionError: Already reading
2: OperationalError: (2014, 'Command Out of Sync')
3: InternalError: Packet sequence number wrong - got 2 expected 1
4: InternalError: Packet sequence number wrong - got 102 expected 1
...

it often comes when i use conn.commit() without yield

yield conn.commit()    √
conn.commit()          ×

but this time, i have no idea...

my log:

2017-06-28 11:27:09,607 BusinessFactory.py-246 [INFO] [674710] 【获取今日推荐文章接口】
2017-06-28 11:27:09,608 GetArticle.py-28 [INFO] page=1
2017-06-28 11:27:09,614 GetArticle.py-36 [INFO] app文章len=10
2017-06-28 11:27:09,635 APIMain.py-18 [INFO] 【版本:230, 协议:pb】
2017-06-28 11:27:09,638 BusinessFactory.py-246 [INFO] [672137] 【获取系统设置接口】
2017-06-28 11:27:09,644 BusinessFactory.py-246 [INFO] [672137] 【获取用户Config接口】
2017-06-28 11:27:09,648 GetUserConfig.py-50 [ERROR] Already reading
Traceback (most recent call last):
  File "/data/www/chanzai/biz_handlers/system/GetUserConfig.py", line 32, in run
    user_info = yield BaseUserDao.get_user(self.context, conn, user_id)
  File "/data/python_venv/chanzai_venv/lib/python2.7/site-packages/tornado/gen.py", line 870, in run
    value = future.result()
  File "/data/python_venv/chanzai_venv/lib/python2.7/site-packages/tornado/concurrent.py", line 215, in result
    raise_exc_info(self._exc_info)
  File "/data/python_venv/chanzai_venv/lib/python2.7/site-packages/tornado/gen.py", line 876, in run
    yielded = self.gen.throw(*exc_info)
  File "/data/www/chanzai/dao/user/BaseUser.py", line 59, in get_user
    yield cursor.execute(sql)
  File "/data/python_venv/chanzai_venv/lib/python2.7/site-packages/tornado/gen.py", line 870, in run
    value = future.result()
  File "/data/python_venv/chanzai_venv/lib/python2.7/site-packages/tornado/concurrent.py", line 215, in result
    raise_exc_info(self._exc_info)
  File "/data/python_venv/chanzai_venv/lib/python2.7/site-packages/tormysql/util.py", line 14, in finish
    result = fun(*args, **kwargs)
  File "/data/python_venv/chanzai_venv/lib/python2.7/site-packages/pymysql/cursors.py", line 166, in execute
    result = self._query(query)
  File "/data/python_venv/chanzai_venv/lib/python2.7/site-packages/pymysql/cursors.py", line 322, in _query
    conn.query(q)
  File "/data/python_venv/chanzai_venv/lib/python2.7/site-packages/pymysql/connections.py", line 837, in query
    self._affected_rows = self._read_query_result(unbuffered=unbuffered)
  File "/data/python_venv/chanzai_venv/lib/python2.7/site-packages/pymysql/connections.py", line 1021, in _read_query_result
    result.read()
  File "/data/python_venv/chanzai_venv/lib/python2.7/site-packages/pymysql/connections.py", line 1304, in read
    first_packet = self.connection._read_packet()
  File "/data/python_venv/chanzai_venv/lib/python2.7/site-packages/pymysql/connections.py", line 963, in _read_packet
    packet_header = self._read_bytes(4)
  File "/data/python_venv/chanzai_venv/lib/python2.7/site-packages/tormysql/connections.py", line 308, in _read_bytes
    future = self._rfile.read_bytes(num_bytes)
  File "/data/python_venv/chanzai_venv/lib/python2.7/site-packages/tormysql/connections.py", line 97, in read
    assert self._read_future is None, "Already reading"
AssertionError: Already reading
2017-06-28 11:27:09,650 web.py-1908 [INFO] 200 POST /api (127.0.0.1) 17.10ms
2017-06-28 11:27:09,661 GetArticle.py-77 [ERROR] (1064, u"42000You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 1")
Traceback (most recent call last):
  File "/data/www/chanzai/biz_handlers/article/GetArticle.py", line 68, in run
    img_info = yield ImgDao.get(self.context, conn, tag_info.img_id)
  File "/data/python_venv/chanzai_venv/lib/python2.7/site-packages/tornado/gen.py", line 870, in run
    value = future.result()
  File "/data/python_venv/chanzai_venv/lib/python2.7/site-packages/tornado/concurrent.py", line 215, in result
    raise_exc_info(self._exc_info)
  File "/data/python_venv/chanzai_venv/lib/python2.7/site-packages/tornado/gen.py", line 876, in run
    yielded = self.gen.throw(*exc_info)
  File "/data/www/chanzai/dao/support/img/Img.py", line 32, in get
    yield cursor.execute(sql)
  File "/data/python_venv/chanzai_venv/lib/python2.7/site-packages/tornado/gen.py", line 870, in run
    value = future.result()
  File "/data/python_venv/chanzai_venv/lib/python2.7/site-packages/tornado/concurrent.py", line 215, in result
    raise_exc_info(self._exc_info)
  File "/data/python_venv/chanzai_venv/lib/python2.7/site-packages/tormysql/util.py", line 14, in finish
    result = fun(*args, **kwargs)
  File "/data/python_venv/chanzai_venv/lib/python2.7/site-packages/pymysql/cursors.py", line 166, in execute
    result = self._query(query)
  File "/data/python_venv/chanzai_venv/lib/python2.7/site-packages/pymysql/cursors.py", line 322, in _query
    conn.query(q)
  File "/data/python_venv/chanzai_venv/lib/python2.7/site-packages/pymysql/connections.py", line 837, in query
    self._affected_rows = self._read_query_result(unbuffered=unbuffered)
  File "/data/python_venv/chanzai_venv/lib/python2.7/site-packages/pymysql/connections.py", line 1021, in _read_query_result
    result.read()
  File "/data/python_venv/chanzai_venv/lib/python2.7/site-packages/pymysql/connections.py", line 1304, in read
    first_packet = self.connection._read_packet()
  File "/data/python_venv/chanzai_venv/lib/python2.7/site-packages/pymysql/connections.py", line 983, in _read_packet
    packet.check_error()
  File "/data/python_venv/chanzai_venv/lib/python2.7/site-packages/pymysql/connections.py", line 395, in check_error
    err.raise_mysql_exception(self._data)
  File "/data/python_venv/chanzai_venv/lib/python2.7/site-packages/pymysql/err.py", line 102, in raise_mysql_exception
    raise errorclass(errno, errval)
ProgrammingError: (1064, u"42000You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 1")
2017-06-28 11:27:09,667 web.py-1908 [INFO] 200 POST /api?type=json (127.0.0.1) 63.50ms
2017-06-28 11:27:13,228 APIMain.py-18 [INFO] 【版本:2.2.2, 协议:json】
2017-06-28 11:27:13,229 web.py-1496 [ERROR] Uncaught exception POST /api?type=json (127.0.0.1)
HTTPServerRequest(protocol='http', host='chanzai.zizaihome.com', method='POST', uri='/api?type=json', version='HTTP/1.0', remote_ip='127.0.0.1', headers={'Content-Length': '351', 'Accept-Language': 'zh-cn', 'Accept-Encoding': 'gzip, deflate', 'X-Forwarded-For': '119.137.52.138', 'Host': 'chanzai.zizaihome.com', 'Accept': 'application/json', 'User-Agent': 'shanzai/17062700 CFNetwork/811.4.18 Darwin/16.4.0', 'Connection': 'close', 'Cache-Control': 'no-cache', 'X-Real-Ip': '119.137.52.138', 'Content-Type': 'application/json'})
Traceback (most recent call last):
  File "/data/python_venv/chanzai_venv/lib/python2.7/site-packages/tornado/web.py", line 1415, in _execute
    result = yield result
  File "/data/python_venv/chanzai_venv/lib/python2.7/site-packages/tornado/gen.py", line 870, in run
    value = future.result()
  File "/data/python_venv/chanzai_venv/lib/python2.7/site-packages/tornado/concurrent.py", line 215, in result
    raise_exc_info(self._exc_info)
  File "/data/python_venv/chanzai_venv/lib/python2.7/site-packages/tornado/gen.py", line 876, in run
    yielded = self.gen.throw(*exc_info)
  File "/data/www/chanzai/api_handlers/main/APIMain.py", line 28, in post
    yield conn.commit()
  File "/data/python_venv/chanzai_venv/lib/python2.7/site-packages/tornado/gen.py", line 870, in run
    value = future.result()
  File "/data/python_venv/chanzai_venv/lib/python2.7/site-packages/tornado/concurrent.py", line 215, in result
    raise_exc_info(self._exc_info)
  File "/data/python_venv/chanzai_venv/lib/python2.7/site-packages/tormysql/util.py", line 14, in finish
    result = fun(*args, **kwargs)
  File "/data/python_venv/chanzai_venv/lib/python2.7/site-packages/pymysql/connections.py", line 769, in commit
    self._read_ok_packet()
  File "/data/python_venv/chanzai_venv/lib/python2.7/site-packages/pymysql/connections.py", line 750, in _read_ok_packet
    raise err.OperationalError(2014, "Command Out of Sync")
OperationalError: (2014, 'Command Out of Sync')
2017-06-28 11:27:13,231 web.py-1908 [ERROR] 500 POST /api?type=json (127.0.0.1) 3.42ms
2017-06-28 11:27:18,547 APIMain.py-18 [INFO] 【版本:2.2.2, 协议:json】
2017-06-28 11:27:18,549 web.py-1496 [ERROR] Uncaught exception POST /api?type=json (127.0.0.1)
HTTPServerRequest(protocol='http', host='chanzai.zizaihome.com', method='POST', uri='/api?type=json', version='HTTP/1.0', remote_ip='127.0.0.1', headers={'Content-Length': '351', 'Accept-Language': 'zh-cn', 'Accept-Encoding': 'gzip, deflate', 'X-Forwarded-For': '119.137.52.138', 'Host': 'chanzai.zizaihome.com', 'Accept': 'application/json', 'User-Agent': 'shanzai/17062700 CFNetwork/811.4.18 Darwin/16.4.0', 'Connection': 'close', 'Cache-Control': 'no-cache', 'X-Real-Ip': '119.137.52.138', 'Content-Type': 'application/json'})
Traceback (most recent call last):
  File "/data/python_venv/chanzai_venv/lib/python2.7/site-packages/tornado/web.py", line 1415, in _execute
    result = yield result
  File "/data/python_venv/chanzai_venv/lib/python2.7/site-packages/tornado/gen.py", line 870, in run
    value = future.result()
  File "/data/python_venv/chanzai_venv/lib/python2.7/site-packages/tornado/concurrent.py", line 215, in result
    raise_exc_info(self._exc_info)
  File "/data/python_venv/chanzai_venv/lib/python2.7/site-packages/tornado/gen.py", line 876, in run
    yielded = self.gen.throw(*exc_info)
  File "/data/www/chanzai/api_handlers/main/APIMain.py", line 28, in post
    yield conn.commit()
  File "/data/python_venv/chanzai_venv/lib/python2.7/site-packages/tornado/gen.py", line 870, in run
    value = future.result()
  File "/data/python_venv/chanzai_venv/lib/python2.7/site-packages/tornado/concurrent.py", line 215, in result
    raise_exc_info(self._exc_info)
  File "/data/python_venv/chanzai_venv/lib/python2.7/site-packages/tormysql/util.py", line 14, in finish
    result = fun(*args, **kwargs)
  File "/data/python_venv/chanzai_venv/lib/python2.7/site-packages/pymysql/connections.py", line 769, in commit
    self._read_ok_packet()
  File "/data/python_venv/chanzai_venv/lib/python2.7/site-packages/pymysql/connections.py", line 748, in _read_ok_packet
    pkt = self._read_packet()
  File "/data/python_venv/chanzai_venv/lib/python2.7/site-packages/pymysql/connections.py", line 970, in _read_packet
    (packet_number, self._next_seq_id))
InternalError: Packet sequence number wrong - got 2 expected 1
2017-06-28 11:27:18,550 web.py-1908 [ERROR] 500 POST /api?type=json (127.0.0.1) 3.73ms
2017-06-28 11:27:20,178 APIMain.py-18 [INFO] 【版本:2.2.2, 协议:json】
2017-06-28 11:27:20,179 web.py-1496 [ERROR] Uncaught exception POST /api?type=json (127.0.0.1)
HTTPServerRequest(protocol='http', host='chanzai.zizaihome.com', method='POST', uri='/api?type=json', version='HTTP/1.0', remote_ip='127.0.0.1', headers={'Content-Length': '351', 'Accept-Language': 'zh-cn', 'Accept-Encoding': 'gzip, deflate', 'X-Forwarded-For': '119.137.52.138', 'Host': 'chanzai.zizaihome.com', 'Accept': 'application/json', 'User-Agent': 'shanzai/17062700 CFNetwork/811.4.18 Darwin/16.4.0', 'Connection': 'close', 'Cache-Control': 'no-cache', 'X-Real-Ip': '119.137.52.138', 'Content-Type': 'application/json'})
Traceback (most recent call last):
  File "/data/python_venv/chanzai_venv/lib/python2.7/site-packages/tornado/web.py", line 1415, in _execute
    result = yield result
  File "/data/python_venv/chanzai_venv/lib/python2.7/site-packages/tornado/gen.py", line 870, in run
    value = future.result()
  File "/data/python_venv/chanzai_venv/lib/python2.7/site-packages/tornado/concurrent.py", line 215, in result
    raise_exc_info(self._exc_info)
  File "/data/python_venv/chanzai_venv/lib/python2.7/site-packages/tornado/gen.py", line 876, in run
    yielded = self.gen.throw(*exc_info)
  File "/data/www/chanzai/api_handlers/main/APIMain.py", line 28, in post
    yield conn.commit()
  File "/data/python_venv/chanzai_venv/lib/python2.7/site-packages/tornado/gen.py", line 870, in run
    value = future.result()
  File "/data/python_venv/chanzai_venv/lib/python2.7/site-packages/tornado/concurrent.py", line 215, in result
    raise_exc_info(self._exc_info)
  File "/data/python_venv/chanzai_venv/lib/python2.7/site-packages/tormysql/util.py", line 14, in finish
    result = fun(*args, **kwargs)
  File "/data/python_venv/chanzai_venv/lib/python2.7/site-packages/pymysql/connections.py", line 769, in commit
    self._read_ok_packet()
  File "/data/python_venv/chanzai_venv/lib/python2.7/site-packages/pymysql/connections.py", line 748, in _read_ok_packet
    pkt = self._read_packet()
  File "/data/python_venv/chanzai_venv/lib/python2.7/site-packages/pymysql/connections.py", line 970, in _read_packet
    (packet_number, self._next_seq_id))
InternalError: Packet sequence number wrong - got 102 expected 1
2017-06-28 11:27:20,180 web.py-1908 [ERROR] 500 POST /api?type=json (127.0.0.1) 4.32ms
fatpo commented 7 years ago

i got it.... two conn, life time, make it clear.... 就是说跨库查询的时候,conn的生命周期要弄对...

ERROR CODE:

             # 先获取禅在app的文章运营列表
            pool = self.pools[0]
            with (yield pool.Connection()) as conn:
                yield conn.commit()
                article_infos = yield ArticleDao.get_articles(self.context, conn, page, white_list=settings.RECOMMEND_WHITE_LIST)
                logging.info("app文章len=%s" % len(article_infos))

            zzh_pool = self.pools[1]
            # 再跟进app的文章列表一个个去自在家查
            with (yield zzh_pool.Connection()) as zzh_conn:
                yield zzh_conn.commit()

                for i in article_infos:
                    zzh_article_info = yield ZZHArticleDao.get_article(self.context, zzh_conn, i.zzh_article_id)
                    if not zzh_article_info:
                        logging.error("奇怪,自在家文章不存在: %s" % i.zzh_article_id)
                        continue

                    pb_article = self.rsp_info.rsp.articles.add()
                    ArticleService.wrap_info_to_pb(zzh_article_info, pb_article)

                    # 文章标题和图片改成禅在后台运营的
                    pb_article.title = i.title
                    img_info = yield ImgDao.get(self.context, conn, i.img_id)
                    pb_article.img_url = settings.QINIU_HTTPS_HOST + img_info.imgurl if img_info else zzh_article_info.pic

CORRECT CODE:

            # 先获取禅在app的文章运营列表
            pool = self.pools[0]
            zzh_pool = self.pools[1]
            with (yield pool.Connection()) as conn:
                yield conn.commit()
                article_infos = yield ArticleDao.get_articles(self.context, conn, page, white_list=settings.RECOMMEND_WHITE_LIST)
                logging.info("app文章len=%s" % len(article_infos))

                # 再跟进app的文章列表一个个去自在家查
                with (yield zzh_pool.Connection()) as zzh_conn:
                    yield zzh_conn.commit()

                    for i in article_infos:
                        zzh_article_info = yield ZZHArticleDao.get_article(self.context, zzh_conn, i.zzh_article_id)
                        if not zzh_article_info:
                            logging.error("奇怪,自在家文章不存在: %s" % i.zzh_article_id)
                            continue

                        pb_article = self.rsp_info.rsp.articles.add()
                        ArticleService.wrap_info_to_pb(zzh_article_info, pb_article)

                        # 文章标题和图片改成禅在后台运营的
                        pb_article.title = i.title
                        img_info = yield ImgDao.get(self.context, conn, i.img_id)
                        pb_article.img_url = settings.QINIU_HTTPS_HOST + img_info.imgurl if img_info else zzh_article_info.pic