#17956: Explicit closing cursors is required for the consistency in multithread
environment
-------------------------------------+-------------------------------------
     Reporter:  nnseva               |      Owner:  nobody
         Type:                       |     Status:  new
  Cleanup/optimization               |    Version:  1.3
    Component:  Database layer       |   Keywords:  thread, threading,
  (models, ORM)                      |  cursor, close, finalization,
     Severity:  Normal               |  __del__, out of sync, twisted
 Triage Stage:  Unreviewed           |  Has patch:  1
Easy pickings:  0                    |      UI/UX:  0
-------------------------------------+-------------------------------------
 Using django ORM in multithread environment causes rare (ignored)
 exceptions like '!ProgrammingError: (2014, "Commands out of sync; you
 can't run this command now")' in Cursor.!__del!__ method (this particular
 error happens when using MySQL backend).

 I've met this problem using django ORM (over MySQL) together with
 twisted.thread.deferToThread call.

 The problem happens relatively rare, but is critical for 24/7 services,
 because looks like causing regular memory leaks.

 Deep investigating the code concerned to the exception contexts, I've
 found that almost no one cursor got by django ORM from the backend is
 closed explicitly.

 I've changed the django code to implement explicit cursor closing in all
 found contexts. After these changes my service leaved for 12 hours of
 continuous working shown no one exception like this.

 All changes have been made in one file, django/db/models/sql/compiler.py

 Changes have been made in the following functions and methods:

 - SQLCompiler.execute_sql in SINGLE and MULTI cases
 - SQLInsertCompiler.execute_sql
 - SQLUpdateCompiler.execute_sql
 - order_modified_iter
 - also not_modified_iter iterator (like order_modified_iter) has been
 added to catch iterator finalization stage

 All changes are explicitly closing the cursor got from the backend after
 use.

 The applied patch has been made for django v.1.3.1.final

-- 
Ticket URL: <https://code.djangoproject.com/ticket/17956>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to