Django populate() isn't reentrant

Question:

I keep getting this when I try to load my Django application on production . I tried all the stackoverflow answers but nothing has fixed it. Any other ideas. (I’m using Django 1.5.2 and Apache)

 Traceback (most recent call last):
         File "/var/www/thehomeboard/wwwhome/wsgi.py", line 37, in <module>
           application = get_wsgi_application()
         File "/usr/local/lib/python2.7/dist-packages/django/core/wsgi.py", line 14, in get_wsgi_application
           django.setup()
         File "/usr/local/lib/python2.7/dist-packages/django/__init__.py", line 18, in setup
           apps.populate(settings.INSTALLED_APPS)
         File "/usr/local/lib/python2.7/dist-packages/django/apps/registry.py", line 78, in populate
           raise RuntimeError("populate() isn't reentrant")
       RuntimeError: populate() isn't reentrant
Asked By: ip.

||

Answers:

This RuntimeError first occured for me after upgrading to Django 1.7 (and still is present with Django 1.8). It is usually caused by an Django application which raises an error, but that error is swallowed somehow.

Here’s a workaround which works for me. Add it to your wsgi.py and the real error should be logged:

import os
import time
import traceback
import signal
import sys
from django.core.wsgi import get_wsgi_application

try:
    application = get_wsgi_application()
    print 'WSGI without exception'
except Exception:
    print 'handling WSGI exception'
    # Error loading applications
    if 'mod_wsgi' in sys.modules:
        traceback.print_exc()
        os.kill(os.getpid(), signal.SIGINT)
        time.sleep(2.5)

See this thread on modwsgi for more details.

Answered By: Dirk Eschler

In the end the problem that I had was that I tried to run a second Django app and did not have the following defined in my apache config:

WSGIDaemonProcess ...
WSGIProcessGroup ...

Just learned that you can run a single django app without defining them but when its two it produces a conflict.

Answered By: ip.

There will be many reason to causes to populate() isn't reentrant error. If you look at the registry.py in your in django application probably inside this directory
/python2.7/site-packages/django/apps

        # app_config should be pristine, otherwise the code below won't
        # guarantee that the order matches the order in INSTALLED_APPS.
        if self.app_configs:
            raise RuntimeError("populate() isn't reentrant")

As you see in the comment it says app_config should be pristine. Which means if one of configuration is not correct or required library missing it will rise this populate error. I got this error because I have missed sqlite installation. Even as you see there is no mentioning possible causes in the exception. I installed sqlite by this command on debian

pip install pysqlite

It solved my problem. My exception because of missing pysqlite.Your maybe having missing of another required packages or errors in your settings.py

Answered By: Harun ERGUL

Full disclosure – “populate() isn’t reentrant” errors can have multiple causes, and checking any recent config or program changes is a very good idea.

However, this error can also occur when Apache is updated, and a module is no longer valid/is corrupted/needs refreshing. This occurred to us on Webfaction after an Apache update (but may happen on any host).

Using the Apache restart script will NOT help this, because the modules remain loaded on the restart. Depending on your system, and whether the mods are cached even when Apache is shut down, this may help.

Fully stop Apache. On Webfaction, that is:

~/webapps/<YOUR WEB APP>/apache2/bin/stop

Wait a few seconds, and then…

~/webapps/<YOUR WEB APP>/apache2/bin/start

That should correct the issue. If your system caches the mods, you may have to flush the cache before starting.

Hope this helps!

Here is the link they gave me (I know the error is different, but we had this happen for the same reason with the populate error as well):

https://statusblog.webfaction.com/2018/05/16/regarding-glibc_private-errors-in-your-python-application/

Answered By: MontyThreeCard

For those using AWS Lambda (and optionally using zappa) this can happen when the size of code & dependencies zipped into a deployment package exceeds 250MB after decompression.

Typically the zip may only be 50 MB but may decompress to over 250MB so you may need to manually unzip the deployment package to check it isn’t too large when uncompressed.

https://docs.aws.amazon.com/lambda/latest/dg/limits.html

Answered By: pythonjsgeo

For me this error was caused, because I hadn’t correctly split my INSTALLED_APPS for local and production. Meaning in local I was using django-cors-headers and in production I was not. But I accidentally had left django-cors-headers even though I removed it from my production requirements.txt. After delete cors-headers from the installed apps in production the error went away.

Answered By: J. Hesters
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.