Bug #73232 mysql.connector.django is leaking connections
Submitted: 8 Jul 2014 13:29 Modified: 5 Sep 2014 13:41
Reporter: Zemian Deng Email Updates:
Status: Can't repeat Impact on me:
None 
Category:Connector / Python Severity:S2 (Serious)
Version:1.2.2 OS:Any
Assigned to: CPU Architecture:Any
Tags: Django

[8 Jul 2014 13:29] Zemian Deng
Description:
When using "mysql.connector.django" package as database backend in a django app, it opens connection objects and does not close it properly, even if there is no traffic. This result in connection leak as soon as you start the django app!

How to repeat:
1. Create a vanilla basic django app (or just use the "mysite" from django tutorial https://docs.djangoproject.com/en/1.6/intro/tutorial01/)
2. Change the settings.py in DATABASES/default/ENGINE with "mysql.connector.django", and update NAME, USERNAME, PASSWORD etc to match to a simple database for testing.
3. Start the django app with "python manage.py runserver"
4. Login into mysql shell and monitor with "show full processlist" command. Immediately there are connection objects created even no traffic to the site.
5. Access the django app with a browser and you will see the connections in  processlist increasing.

Suggested fix:
I think this has somethign to do with DatabaseWrapper.__init__ from mysql/connector/django/base.py file (line 465).
[8 Jul 2014 13:31] Zemian Deng
FYI, I started a topic in forum, but no one replied. http://forums.mysql.com/read.php?50,616862,616862#msg-616862
[9 Jul 2014 12:40] Zemian Deng
I have a patch here that can fix the leak (see Files attachement). The problem is the DatabaseOperations is using a instance of converter from an connection object. I cached the converter in the DatabaseWrapper and then immediately close the connection after the __init__(). Then later using this "cache" converter in DatabaseOperations. So far the converter doesn't seem to need a live connection to work.

Let me know if you this works for you or not.
[9 Jul 2014 12:41] Zemian Deng
Path file to fix conn leak for django

Attachment: myconnpy1.2.2_django_base.py.patch (application/octet-stream, text), 1.98 KiB.

[9 Jul 2014 12:43] Zemian Deng
BTW, I am already an Oracle employee (zemian.deng@oracle.com), do you still need me to sign the Contributor Agreement form?
[5 Sep 2014 11:57] Andrii Nikitin
Thank you for reporting this problem.

I wasn't able to repeat it in 1.2.3
I created generic project using your link with Django 1.7 and see new connections are established in webserver console, but processlist shows the same amount of connections.

I checked base.py and it looks really different - I will close with "Can't repeat" resolution, assuming the bug was fixed by some side changes.
[5 Sep 2014 13:41] Zemian Deng
Hi Andrii,

Yes I can confirmed that the latest connector 1.2.3 does not have this issue any more. But previous 1.2.2 is definitely repeatable (which is what I reported against with).

Thanks for the update!
Zemian