Django stops working with RuntimeError: populate() isn't reentrant

Solution 1:

This is caused by a bug in your Django settings somewhere. Unfortunately, Django's hiding the bug behind this generic and un-useful error message.

To reveal the true problem, open django/apps/registry.py and around line 80, replace:

raise RuntimeError("populate() isn't reentrant")

with:

self.app_configs = {}

This will allow Django to continue loading, and reveal the actual error.

I've encountered this error for several different causes. Once was because I had a bad import in one of my app's admin.py.

Solution 2:

My server's administrator restarted Apache, and that magically fixed this problem. The exact same Python files loaded without causing populate() isn't reentrant. I even tried loading another file with a syntax error, then fixing it, and the server was able to load the new file and run correctly with no problems.

I still don't know what was going wrong, but I'm marking this as answered since the problem is gone. (Well, I'll mark it as answered as soon as StackOverflow allows me to accept my own answer.)

Update: After continuing to get this error when I accidentally upload Python with syntax errors, I figured out a workaround that's easier than restarting Apache. When WSGI starts throwing the populate() isn't reentrant error, I replace my Django project's wsgi.py with this simple function:

def application(environ, start_response):
    if environ['mod_wsgi.process_group'] != '': 
        import signal
        os.kill(os.getpid(), signal.SIGINT)
    return ["killed"]

Then I reload my website, and the WSGI daemon process restarts (which I can tell by looking at the Apache log, even though the website still displays the same 500 error).

If I then change wsgi.py back to normal and reload again, WSGI successfully picks up my code without throwing populate() isn't reentrant (assuming I have no syntax errors this time). So the entirety of Apache doesn't need to restart, just the WSGI process, and I can do that without root privileges.

Solution 3:

I know this is an old answer but I will contribute with my solution:

As a way to diagnose the source of the problem run manage.py checkand see if you find anything there

In my case an outdated requirement was the issue and django was failing to import a submodule

Make sure that your requirements are up to date

Solution 4:

It's not a response but a reflexion.

When you upgrade to django 1.7 and you have a 500 error and reload your page, Apache says "populate() isn't reentrant". I think it's when you load your page, Apache load all the modules you need for your app and when the error is handle it doesn't unload module. So, when you reload your page, apache load again theses modules but it's already loaded. So, apache says "populate() isn't reentrant".

I've two actions to correct this : Restart apache, or correct the error that handle the first 5OO error.

Try restarting apache with:

sudo service httpd restart

I hope it will help you.