#29257: If creation of a db cursor fails, the resulting traceback is misleading
-------------------------------------+-------------------------------------
               Reporter:  Jerome     |          Owner:  nobody
  Leclanche                          |
                   Type:             |         Status:  new
  Uncategorized                      |
              Component:  Database   |        Version:  2.0
  layer (models, ORM)                |
               Severity:  Normal     |       Keywords:
           Triage Stage:             |      Has patch:  1
  Unreviewed                         |
    Needs documentation:  0          |    Needs tests:  0
Patch needs improvement:  0          |  Easy pickings:  0
                  UI/UX:  0          |
-------------------------------------+-------------------------------------
 When a schema is out of sync (eg. running tests for a project where a
 model has fields not present in the migration).

 The creation of a cursor involves a sql query which includes all of the
 field names. Creating that cursor will fail.
 However, the exception is currently caught by a try/except which always
 attempts to *close* the cursor. Closing the cursor is not try/excepted,
 which results in that particular query failing and a traceback that looks
 like "ERROR:  cursor "_django_curs_140260213699904_6" does not exist".

 Patch:


 {{{

 commit 7c7a1f53acbf7d94f0a9b360f973711a7c9fbfbd (HEAD ->
 refs/heads/master)
 Author: Jerome Leclanche <[email protected]>
 Date:   Tue Mar 20 09:57:26 2018 +0200

     Raise outer exception when failing to close a cursor after an error

 diff --git a/django/db/models/sql/compiler.py
 b/django/db/models/sql/compiler.py
 index 1fdbd156b6..f96be3c4ea 100644
 --- a/django/db/models/sql/compiler.py
 +++ b/django/db/models/sql/compiler.py
 @@ -1051,10 +1051,15 @@ class SQLCompiler:
              cursor = self.connection.cursor()
          try:
              cursor.execute(sql, params)
 -        except Exception:
 +        except Exception as e:
              # Might fail for server-side cursors (e.g. connection closed)
 -            cursor.close()
 -            raise
 +            try:
 +                cursor.close()
 +            except Exception:
 +                # If we got an error creating the cursor, then closing it
 +                # will always fail. Raise the outer exception instead of
 the
 +                # obscure "cursor _django_curs_xxxx does not exist".
 +                raise e from None

          if result_type == CURSOR:
              # Give the caller the cursor to process and close.

 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/29257>
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 unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/053.c6b8e7f270fa22656f673cb35a05f20d%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to