[Python-modules-team] Bug#804340: python-django: DatabaseError in development server on multi-core CPU computer with capital-letter-named django application

Moritz Strohm ncc1988 at posteo.de
Sat Nov 7 16:24:05 UTC 2015


Package: python-django
Version: 1.7.7-1+deb8u2
Severity: normal

Hello


I get the following error messages when I try to access any application running
on Django's development server on a computer with multi-core CPU after I
enabled one of my applications that has capital letters in its name in the
Django project:

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
$SOME_THREAD_ID and this is thread id $ANOTHER_THREAD_ID.

Plus, I get the following traceback:

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 self.application(environ, start_response)
  File "/usr/lib/python2.7/dist-packages/django/core/handlers/wsgi.py", line
175, in __call__
    signals.request_started.send(sender=self.__class__)
  File "/usr/lib/python2.7/dist-packages/django/dispatch/dispatcher.py", line
198, in send
    response = receiver(signal=self, sender=sender, **named)
  File "/usr/lib/python2.7/dist-packages/django/db/__init__.py", line 110, in
close_old_connections
    conn.close_if_unusable_or_obsolete()
  File "/usr/lib/python2.7/dist-packages/django/db/backends/__init__.py", line
497, in close_if_unusable_or_obsolete
    self.close()
  File "/usr/lib/python2.7/dist-packages/django/db/backends/sqlite3/base.py",
line 391, in close
    self.validate_thread_sharing()
  File "/usr/lib/python2.7/dist-packages/django/db/backends/__init__.py", line
515, in validate_thread_sharing
    % (self.alias, self._thread_ident, thread.get_ident()))


I noticed that the first one or two requests work when I start the application
server. After that I only see "A server error occurred.  Please contact the
administrator" in the browser, regardless which app I try to access. If I
remove the capital-letter-named application from the django project everythink
works fine. The application itself has simple database models and doesn't do
anything extraordinary, it just answers HTTP requests, talks to another program
via JSON-RPC and creates QR-codes.

My investigation revealed that this error seems to be based in the threading
support of the development server. The error occured first on a multi-core CPU
computer. On another computer (i386, single core, same Debian OS and Django
version) it didn't appear at all. Plus, if I start the development server with
--nothreading on the multi-core CPU computer everything is running fine.


You can reproduce this error by following these steps on a computer with multi-
core CPU:

- create a new django project: django-admin startproject project
- Go into the project's directory and download the application "TinycryptoPOS":
git clone https://gitlab.com/ncc1988/TinycryptoPOS.git
- install dependencies with pip as root: "pip qrcode" and "pip pyjsonrpc"
- activate the application:
  - add 'TinycryptoPOS', to the INSTALLED_APPS tuple in ./project/settings.py
  - add the url configuration to urls.py:
    from TinycryptoPOS.urls import urls as tcurls
    and add the following line in the urlpatterns tuple:
    url(r'^TinycryptoPOS/', include(tcurls)),
- now run: python manage.py syncdb
- run the development server: python manage.py runserver

Now when you try to access the admin interface the first one or two requests
will work but then you will see "A server error occured..." in your browser and
you will see the error messages in the console I pasted above.

If you run the development server with --nothreading everything will work fine.

I tried to start the development server in python3, but there it gave me syntax
errors so I can't tell if switching to python3 will "solve" the problem.
Furthermore I disabled the dependency "qrcode" which is imported in
TinycryptoPOS's models.py but this wasn't the problem either.


Regards
Moritz Strohm



-- System Information:
Debian Release: 8.1
  APT prefers stable-updates
  APT policy: (500, 'stable-updates'), (500, 'oldoldstable'), (500, 'stable'), (500, 'oldstable')
Architecture: amd64 (x86_64)
Foreign Architectures: i386

Kernel: Linux 3.16.0-4-amd64 (SMP w/4 CPU cores)
Locale: LANG=de_DE.utf8, LC_CTYPE=de_DE.utf8 (charmap=UTF-8)
Shell: /bin/sh linked to /bin/dash
Init: systemd (via /run/systemd/system)

Versions of packages python-django depends on:
ii  python-django-common  1.7.7-1+deb8u2
pn  python:any            <none>

Versions of packages python-django recommends:
ii  libjs-jquery     1.7.2+dfsg-3.2
ii  python-sqlparse  0.1.13-2
ii  python-tz        2012c+dfsg-0.1

Versions of packages python-django suggests:
pn  bpython                                        <none>
pn  geoip-database-extra | geoip-database-contrib  <none>
ii  gettext                                        0.19.3-2
pn  ipython                                        <none>
pn  libgdal1                                       <none>
pn  python-bcrypt                                  <none>
pn  python-django-doc                              <none>
pn  python-flup                                    <none>
pn  python-memcache                                <none>
pn  python-mysqldb                                 <none>
ii  python-pil                                     2.6.1-2
pn  python-psycopg2                                <none>
pn  python-sqlite                                  <none>
pn  python-yaml                                    <none>

-- no debconf information



More information about the Python-modules-team mailing list