10

TL;DR:我已经为我发现的一个错误提供了一个补丁,并且我得到了 0 个反馈。我想知道这是否是一个错误。这不是咆哮。请阅读此内容,如果您可能受到它的影响,请检查修复。

几周前我发现并报告了这个 MySQLdb 错误(编辑:6 周前),发送了一个补丁,将其发布在几个 ORM 的论坛上,邮寄了 MySQLdb 作者,邮寄了一些谈论处理死锁的人,邮寄了 ORM 作者和我'仍在等待任何形式的反馈。

这个错误让我很伤心,我能在反馈中找到的唯一解释是,要么没有人在带有 mysql 的 python 中使用“SELECT ... FOR UPDATE”,要么这不是一个错误。

基本上问题是在使用 MySQLdb 游标发出“SELECT ... FOR UPDATE”时不会引发死锁和“锁定等待超时”异常。相反,该语句会静默失败并返回一个空结果集,任何应用程序都会将其解释为没有匹配的行。

我已经测试了SVN版本,它仍然受到影响。在默认安装的 Ubuntu Intrepid、Jaunty 和 Debian Lenny 上进行了测试,这些也受到影响。easy_install (1.2.3c1) 安装的当前版本受到影响。

这也会影响 SQLAlchemy 和 SQLObject,并且可能任何使用 MySQLdb 游标的 ORM 也会受到影响。

该脚本可以重现将触发错误的死锁(只需更改用户/在 get_conn 中传递,它将创建必要的表):

import time
import threading
import traceback
import logging
import MySQLdb

def get_conn():
    return MySQLdb.connect(host='localhost', db='TESTS',
                           user='tito', passwd='testing123')

class DeadlockTestThread(threading.Thread):
    def __init__(self, order):
        super(DeadlockTestThread, self).__init__()
        self.first_select_done = threading.Event()
        self.do_the_second_one = threading.Event()
        self.order = order

    def log(self, msg):
        logging.info('%s: %s' % (self.getName(), msg))

    def run(self):
        db = get_conn()
        c = db.cursor()
        c.execute('BEGIN;')
        query = 'SELECT * FROM locktest%i FOR UPDATE;'
        try:
            try:
                c.execute(query  % self.order[0])
                self.first_select_done.set()

                self.do_the_second_one.wait()
                c.execute(query  % self.order[1])
                self.log('2nd SELECT OK, we got %i rows' % len(c.fetchall()))

                c.execute('SHOW WARNINGS;')
                self.log('SHOW WARNINGS: %s' % str(c.fetchall()))
            except:
                self.log('Failed! Rolling back')
                c.execute('ROLLBACK;')
                raise
            else:
                c.execute('COMMIT;')
        finally:
            c.close()
            db.close()


def init():
    db = get_conn()

    # Create the tables.
    c = db.cursor()
    c.execute('DROP TABLE IF EXISTS locktest1;')
    c.execute('DROP TABLE IF EXISTS locktest2;')
    c.execute('''CREATE TABLE locktest1 (
                    a int(11), PRIMARY KEY(a)
                  ) ENGINE=innodb;''')
    c.execute('''CREATE TABLE locktest2 (
                    a int(11), PRIMARY KEY(a)
                  ) ENGINE=innodb;''')
    c.close()

    # Insert some data.
    c = db.cursor()
    c.execute('BEGIN;')
    c.execute('INSERT INTO locktest1 VALUES (123456);')
    c.execute('INSERT INTO locktest2 VALUES (123456);')
    c.execute('COMMIT;')
    c.close()

    db.close()

if __name__ == '__main__':
    logging.basicConfig(level=logging.INFO)

    init()

    t1 = DeadlockTestThread(order=[1, 2])
    t2 = DeadlockTestThread(order=[2, 1])

    t1.start()
    t2.start()

    # Wait till both threads did the 1st select.
    t1.first_select_done.wait()
    t2.first_select_done.wait()

    # Let thread 1 continue, it will get wait for the lock 
    # at this point.
    t1.do_the_second_one.set()

    # Just make sure thread 1 is waiting for the lock.
    time.sleep(0.1)

    # This will trigger the deadlock and thread-2 will
    # fail silently, getting 0 rows.
    t2.do_the_second_one.set()

    t1.join()
    t2.join()

在未打补丁的 MySQLdb 上运行它的输出是这样的:

$ python bug_mysqldb_deadlock.py
INFO:root:Thread-2: 2nd SELECT OK, we got 0 rows
INFO:root:Thread-2: SHOW WARNINGS: (('Error', 1213L, 'Deadlock found when trying to get lock; try restarting transaction'),)
INFO:root:Thread-1: 2nd SELECT OK, we got 1 rows
INFO:root:Thread-1: SHOW WARNINGS: ()

您可以看到 Thread-2 从我们知道有 1 的表中获得了 0 行,并且只发出“SHOW WARNINGS”语句,您可以看到发生了什么。如果您检查“SHOW ENGINE INNODB STATUS”,您将在日志“*** WE ROLL BACK TRANSACTION (2)”中看到这一行,在 Thread-2 上选择失败后发生的所有事情都是半回滚事务。

应用补丁后(检查票证,下面的 url),这是运行脚本的输出:

$ python bug_mysqldb_deadlock.py
INFO:root:Thread-2: Failed! Rolling back
Exception in thread Thread-2:
Traceback (most recent call last):
  File "/usr/lib/python2.4/threading.py", line 442, in __bootstrap
    self.run()
  File "bug_mysqldb_deadlock.py", line 33, in run
    c.execute(query  % self.order[1])
  File "/home/koba/Desarollo/InetPub/IBSRL/VirtualEnv-1.0-p2.4/lib/python2.4/site-packages/MySQL_python-1.2.2-py2.4-linux-x86_64.egg/MySQLdb/cursors.py", line 178, in execute
    self.errorhandler(self, exc, value)
  File "/home/koba/Desarollo/InetPub/IBSRL/VirtualEnv-1.0-p2.4/lib/python2.4/site-packages/MySQL_python-1.2.2-py2.4-linux-x86_64.egg/MySQLdb/connections.py", line 35, in defaulterrorhandler
    raise errorclass, errorvalue
OperationalError: (1213, 'Deadlock found when trying to get lock; try restarting transaction')

INFO:root:Thread-1: 2nd SELECT OK, we got 1 rows
INFO:root:Thread-1: SHOW WARNINGS: ()

在这种情况下,在 Thread-2 上会引发异常并正确回滚。

那么,你的意见是什么?,这是一个错误吗?没人关心还是我疯了?

这是我在 SF 上开的票:http: //sourceforge.net/tracker/index.php ?func=detail&aid=2776267&group_id=22307&atid=374932

4

1 回答 1

7

为什么没有人关心这个 MySQLdb 错误?

错误可能需要一段时间来确定优先级,研究,验证问题,找到修复,测试修复,确保修复不会破坏其他任何东西。我建议您部署一个解决方法,因为此修复程序可能需要一些时间才能为您提供。

于 2009-06-03T15:48:58.623 回答