Menu

Leak is found using MySQLdb in Thread.run()

2007-11-20
2012-09-19
  • wand_of_wishing

    wand_of_wishing - 2007-11-20

    I'm using python 2.5.1 and MySQLdb-1.2.2 in Windows Vista and Windows Server 2003.

    In this example, no leakage has been found.

    import MySQLdb, MySQLdb.connections, sys
    from _mysql_exceptions import *
    while 1 :
    try : #connection
    conn = MySQLdb.connections.Connection(host='some ip',\ user='blahblah', passwd='blahblah', db='some schema')
    except (OperationalError), msg: #Connection failure
    print "DB Connection failed. %s" % msg
    exit(1)
    conn.close()
    print "connection closed. sleep 1"
    sys.stdout.flush()
    import time
    time.sleep (1)
    exit(0)

    In this example, a handle leakage has been found.

    import MySQLdb, MySQLdb.connections, sys, threading
    from _mysql_exceptions import *

    class myTh(threading.Thread) :
    def run(self) :
    try : #connection
    conn = MySQLdb.connections.Connection(host='some ip',\ user='blahblah', passwd='blahblah', db='some schema')
    except (OperationalError), msg: #Connection failure
    print "DB Connection failed. %s" % msg
    exit(1)
    conn.close()
    print "connection closed. sleep 1"
    sys.stdout.flush()

    while 1 :
    th = myTh()
    th.start()
    th.join()
    import time
    time.sleep (1)
    exit(0)

    Is this a bug, or I'm using MySQLdb insanely?

    Is this problem also occurs on linux?

    Is there any solution except making connection outside of Thread?

    Thanks.

     
    • wand_of_wishing

      wand_of_wishing - 2007-11-20

      How can I modify article?...
      indentation has gone away.;;

      In this example, no leakage has been found.

      import MySQLdb, MySQLdb.connections, sys
      from _mysql_exceptions import *
      while 1 :
        try : #connection
          conn = MySQLdb.connections.Connection(host='some ip',\
              user='blahblah', passwd='blahblah', db='some schema')
        except (OperationalError), msg: #Connection failure
          print "DB Connection failed. %s" % msg
          exit(1)
        conn.close()
        print "connection closed. sleep 1"
        sys.stdout.flush()
        import time
        time.sleep (1)
      exit(0)

      In this example, a handle leakage has been found.

      import MySQLdb, MySQLdb.connections, sys, threading
      from _mysql_exceptions import *

      class myTh(threading.Thread) :
        def run(self) :
          try : #connection
            conn = MySQLdb.connections.Connection(host='some ip',\           user='blahblah', passwd='blahblah', db='some schema')
          except (OperationalError), msg: #Connection failure
            print "DB Connection failed. %s" % msg
            exit(1)
          conn.close()
          print "connection closed. sleep 1"
          sys.stdout.flush()

      while 1 :
        th = myTh()
        th.start()
        th.join()
        import time
        time.sleep (1)
      exit(0)

       

Log in to post a comment.