Opened 9 years ago

Closed 2 months ago

Last modified 5 weeks ago

#26214 closed Cleanup/optimization (needsinfo)

Better error message for RuntimeError: populate() isn't reentrant

Reported by: Thomas Güttler Owned by: nobody
Component: Core (Other) Version: dev
Severity: Normal Keywords:
Cc: Aymeric Augustin Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Thomas Güttler)

We were hit by an exception like this in production environment

Traceback (most recent call last):
  File "foobar-fm/foobar_fm/apache/django_wsgi.py", line 4, in <module>
    application = wsgi.get_handler()
  File "djangotools/djangotools/utils/wsgi.py", line 54, in get_handler
    return get_wsgi_application()
  File "lib/python2.7/site-packages/django/core/wsgi.py", line 14, in get_wsgi_application
    django.setup()
  File "lib/python2.7/site-packages/django/__init__.py", line 18, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "lib/python2.7/site-packages/django/apps/registry.py", line 78, in populate
    raise RuntimeError("populate() isn't reentrant")
RuntimeError: populate() isn't reentrant

To debug this, I am missing an very important part:

The Stacktrace of the other one trying to load the config

Here is the code part

            # 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")

My idea: the first one, who passes above check stores its current stracktrace to a variable.

The if the second fails, and RuntimeError("populate() isn't reentrant") gets executed, the exception can show the stacktrace of the first run.

With this you can debug it. Otherwise you are lost.

What do you think?

Change History (13)

comment:1 by Thomas Güttler, 9 years ago

Description: modified (diff)

comment:2 by Tim Graham, 9 years ago

Cc: Aymeric Augustin added
Component: Error reportingCore (Other)
Owner: set to nobody
Type: UncategorizedCleanup/optimization

I'm not sure. Aymeric, what's your opinion?

comment:3 by Aymeric Augustin, 9 years ago

I understand the reasoning behind this suggestion, but in my experience, trying to be smart with error handling often backfires, and my gut feeling is that this is too magical.

Besides Python 3's exception chaining solves the problem entirely... Let's say that my motivation for doing something is non-existent, given that I don't plan to ever use Python 2 again.

comment:4 by Tim Graham, 9 years ago

Resolution: wontfix
Status: newclosed

The rationale makes sense to me.

comment:5 by Thomas Güttler, 9 years ago

Python 3's exception chaining solves the problem entirely.

I only used Python2 up to now. I guess it is time to upgrade.

Thank you for your feedback.

comment:6 by Thomas Güttler, 4 years ago

5 years later, running Python 3.8 I come across the same error again. On the Python command line it works, there I see the real error: ModuleNotFoundError: No module named 'psycopg2'

But if I run this via PyCharm "execute file". I get the meaningless error message.

Here is an answer to see the real error message, if you don't see it: https://stackoverflow.com/a/55929118/633961 (replace the exection in registry.py with self.app_configs = {})

comment:7 by Adrian Torres, 2 years ago

Is there any way that we can reopen this ticket? It's still a constant issue and debugging the actual root cause of the issue can prove to be problematic in remote environments, the linked solution in StackOverflow has over 100k views which just goes to show how many people have run into this issue and still do.

Don't know why Aymeric mentions Python 3's exception chaining by default, it clearly does not happen here, at least not when executing commands such as python3 manage.py migrate.

Last edited 2 years ago by Adrian Torres (previous) (diff)

comment:8 by Claude Paroz, 2 years ago

If you can provide a patch with some concrete proposals that improves things without regressions, I guess this could be reconsidered.

comment:9 by Bruno Henrique de Paula, 2 years ago

Hey guys, is there a way to fix this? This is a well known issue in the Python community, it's not practical to keep editing the registry.py file and doing the workaround proposed here -> https://stackoverflow.com/a/55929118/633961 in order to see the real problem. How could we solve this?

comment:10 by Aymeric Augustin, 2 months ago

Resolution: wontfix
Status: closednew

I wasn't aware that this could hide errors in settings. That's a problem indeed.

comment:11 by Natalia Bidart, 2 months ago

Resolution: needsinfo
Status: newclosed
Version: 1.9dev

I have read the StackOverflow post, but I'm still unclear on how to reproduce this in a local development environment. I have tried running Django with Gunicorn and forcing errors such as:

  • An import error from the admin (no issues; I receive the traceback in the console).
  • An ImproperlyConfigured exception raised (no issues here either).

I will close this as needsinfo until someone can provide a small Django project with instructions to reproduce this issue. Ideally, providing a test case would also be helpful. In either case, please feel free to reopen for us to re-triage. Thank you!

comment:12 by Aymeric Augustin, 2 months ago

Based on the StackOverflow question, it seemed as simple as "add invalid code to settings", but indeed that's not enough. Thank you for double checking. Hopefully someone who hits the problem will report back here instead of on StackOverflow.

comment:13 by yzupnick, 5 weeks ago

I just hit this by installing django_allauth[socialaccount], and adding these apps to the INSTALLED_APPS list, but without adding pyjwt, cryptography, or requests to my python environment.

  1. "allauth",
  2. "allauth.account",
  3. "allauth.socialaccount",
  4. "allauth.socialaccount.providers.google"

(Also I am building using nix and dream2nix... not sure if that is causing the problem...)

Note: See TracTickets for help on using tickets.
Back to Top