Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 248859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:22:41+00:00 2026-05-11T21:22:41+00:00

TL;DR: I’ve supplied a patch for a bug I found and I’ve got 0

  • 0

TL;DR: I’ve supplied a patch for a bug I found and I’ve got 0 feedback on it. I’m wondering if it’s a bug at all. This is not a rant. Please read this and if you may be affected by it check the fix.

I have found and reported this MySQLdb bug some weeks ago (edit: 6 weeks ago), sent a patch, posted it on a couple of ORM’s forums, mailed the MySQLdb author, mailed some people talking about handling deadlocks, mailed ORM authors and I’m still waiting for any kind of feedback.

This bug caused me a lot of grief and the only explanations I can find on the feedback is that either no one uses “SELECT … FOR UPDATE” in python with mysql or that this is not a bug.

Basically the problem is that deadlocks and “lock wait timeout” exceptions are NOT being raised when issuing a “SELECT … FOR UPDATE” using a MySQLdb cursor.
Instead, the statement fails silently and returns an empty resultset, which any application will interpret as if there were no rows matched.

I’ve tested the SVN version and it’s still affected. Tested on the default installations of Ubuntu Intrepid, Jaunty and Debian Lenny and those are affected too. The current version installed by easy_install (1.2.3c1) is affected.

This affects SQLAlchemy and SQLObject too and probably any ORM that used MySQLdb cursors is affected too.

This script can reproduce a deadlock that will trigger the bug (just change the user/pass in get_conn, it will create the necessary tables):

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()

The output of running this on an unpatched MySQLdb is this:

$ 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: ()

You can see that Thread-2 got 0 rows from a table we know has 1 and only issuing a “SHOW WARNINGS” statement you can see what happened.
If you check “SHOW ENGINE INNODB STATUS” you will see this line in the log “*** WE ROLL BACK TRANSACTION (2)”, everything that happens after the failing select on Thread-2 is on a half rolled back transaction.

After applying the patch (check the ticket for it, url below), this is the output of running the script:

$ 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: ()

In this case an exception is raised on Thread-2 and it rolls back properly.

So, what’s your opinion?, is this a bug? no one cares or am I just crazy?

This is the ticket I opened on SF: http://sourceforge.net/tracker/index.php?func=detail&aid=2776267&group_id=22307&atid=374932

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T21:22:41+00:00Added an answer on May 11, 2026 at 9:22 pm

    Why doesn’t anyone care about this
    MySQLdb bug?

    bugs can take a while to prioritize, research, verify the problem, find a fix, test the fix, make sure the fix fix does not break anything else. I would suggest you deploy a work around, since it could take some time for this fix to arrive for you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 231k
  • Answers 231k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Use the following function like this: Image('/path/to/original.image', '1/1', '150*', './thumb.jpg');… May 13, 2026 at 2:13 am
  • Editorial Team
    Editorial Team added an answer Check you database schema to see if the field (referenced… May 13, 2026 at 2:13 am
  • Editorial Team
    Editorial Team added an answer I figured out the problem - there was a session… May 13, 2026 at 2:13 am

Related Questions

TL;DR: I've supplied a patch for a bug I found and I've got 0
The docs on this are pretty shoddy. There are a number of events you
TL;DR version: I have two threads. One of them might need to Interrupt() the
I've been looking all over the internet and I can't find an acceptable solution
I am trying to start a Buildbot Buildslave on a Windows XP virtual machine:

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.