[Django] #25714: DatabaseError: DatabaseWrapper objects created in a thread can only be used in that same thread.

906 views
Skip to first unread message

Django

unread,
Nov 8, 2015, 8:46:10 PM11/8/15
to django-...@googlegroups.com
#25714: DatabaseError: DatabaseWrapper objects created in a thread can only be used
in that same thread.
----------------------------------------------+--------------------
Reporter: brian | Owner: nobody
Type: Bug | Status: new
Component: Database layer (models, ORM) | Version: 1.8
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
----------------------------------------------+--------------------
{{{
Traceback (most recent call last):
File "/usr/lib/python2.7/wsgiref/handlers.py", line 85, in run
self.result = application(self.environ, self.start_response)
File "/usr/lib/python2.7/dist-
packages/django/contrib/staticfiles/handlers.py", line 64, in __call__
return super(StaticFilesHandler, self).__call__(environ,
start_response)
File "/usr/lib/python2.7/dist-packages/django/core/handlers/wsgi.py",
line 177, in __call__
signals.request_started.send(sender=self.__class__, environ=environ)
File "/usr/lib/python2.7/dist-packages/django/dispatch/dispatcher.py",
line 201, in send
response = receiver(signal=self, sender=sender, **named)
File "/usr/lib/python2.7/dist-packages/django/db/__init__.py", line 64,
in close_old_connections
conn.close_if_unusable_or_obsolete()
File "/usr/lib/python2.7/dist-packages/django/db/backends/base/base.py",
line 403, in close_if_unusable_or_obsolete
self.close()
File "/usr/lib/python2.7/dist-
packages/django/db/backends/sqlite3/base.py", line 221, in close
self.validate_thread_sharing()
File "/usr/lib/python2.7/dist-packages/django/db/backends/base/base.py",
line 421, in validate_thread_sharing
% (self.alias, self._thread_ident, thread.get_ident()))
DatabaseError: DatabaseWrapper objects created in a thread can only be
used in that same thread. The object with alias 'default' was created in
thread id 140201111500544 and this is thread id 140201088387984.
}}}

Using instructions found in Debian bug report:
https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=804340

This looks like a database issue to me, however the reporter of the Debian
bug thinks it is something to do with the capital-letter-named django
application. Which seems weird to me.

A search for similar bugs found #17998 - not idea if this is related or
not.

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

Django

unread,
Nov 8, 2015, 8:47:15 PM11/8/15
to django-...@googlegroups.com
#25714: DatabaseError: DatabaseWrapper objects created in a thread can only be used
in that same thread.
-------------------------------------+-------------------------------------

Reporter: brian | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by brian):

* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0


Comment:

Forgot to clarify - The Debian bug report is against Django 1.7.7, however
I reproduced the above with Django 1.8.4

--
Ticket URL: <https://code.djangoproject.com/ticket/25714#comment:1>

Django

unread,
Nov 9, 2015, 2:46:19 AM11/9/15
to django-...@googlegroups.com
#25714: DatabaseError: DatabaseWrapper objects created in a thread can only be used
in that same thread.
-------------------------------------+-------------------------------------

Reporter: brian | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by aaugustin):

I can't tell from the original report which database the person is using
as the PostgreSQL, MySQL and SQLite adapters are installed. Django allows
sharing SQLite connections across threads but not PostgreSQL and MySQL
connections.

Generally speaking database connections should only be accessed through
`from django.db import connection` or `from django.db import connections;
connections[alias]`. This returns thread local objects and avoids this
problem.

The TinycryptoPOS application appears to run a daemon; I'm not sure what
it does exactly but I suspect that's how a connection ends up being
accessed incorrectly. If every Django user saw that error, we'd probably
have heard about it by now, so I suspect the bug is in TinycryptoPOS.

--
Ticket URL: <https://code.djangoproject.com/ticket/25714#comment:2>

Django

unread,
Nov 9, 2015, 2:48:21 AM11/9/15
to django-...@googlegroups.com
#25714: DatabaseError: DatabaseWrapper objects created in a thread can only be used
in that same thread.
-------------------------------------+-------------------------------------

Reporter: brian | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by aaugustin):

Closing the Debian bug and forwarding here was the right thing to do. The
discussion about single-core / multi-core is irrelevant -- of course you
can't reproduce an issue that's obviously related to threads when running
single-threaded.

--
Ticket URL: <https://code.djangoproject.com/ticket/25714#comment:3>

Django

unread,
Nov 10, 2015, 6:45:18 PM11/10/15
to django-...@googlegroups.com
#25714: DatabaseError: DatabaseWrapper objects created in a thread can only be used
in that same thread.
-------------------------------------+-------------------------------------

Reporter: brian | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by brian):

From the Debian bug report:

> I found the origin of the problem: It was the pyjsonrpc library that is
included into TinycryptoPOS. After I commented out every part of code that
uses something from pyjsonrpc the error disappeared.
>
> You just have to import pyjsonrpc in your Django application without
using code from it and the error appears... but only on amd64. I have
verified this with another Django application.
>
> I think this is clearly a bug in pyjsonrpc and not a django bug.
>
> Thank you for your help and time and also thanks to the people on the
django mailing list for giving me the hint to the right direction.

I have looked at pyjsonrpc (download from PyPI, link to github project is
dead), I am not convinced it could cause this problem. However it uses
things like gevent and WebSocketClient from ws4py.client.geventclient -
both of which I haven't used myself, so I might be mistaken.

--
Ticket URL: <https://code.djangoproject.com/ticket/25714#comment:4>

Django

unread,
Nov 11, 2015, 10:33:27 AM11/11/15
to django-...@googlegroups.com
#25714: DatabaseError: DatabaseWrapper objects created in a thread can only be used
in that same thread.
-------------------------------------+-------------------------------------
Reporter: brian | Owner: nobody
Type: Bug | Status: closed

Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Normal | Resolution: needsinfo
Keywords: | Triage Stage:
| Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timgraham):

* status: new => closed
* resolution: => needsinfo


Comment:

If this is a Django problem, I think you'll need to propose a fix or at
least provide the reasoning why.

--
Ticket URL: <https://code.djangoproject.com/ticket/25714#comment:5>

Reply all
Reply to author
Forward
0 new messages