Opened 8 years ago

Closed 8 years ago

Last modified 8 years ago

#25555 closed Cleanup/optimization (duplicate)

runserver swallows exceptions

Reported by: Anssi Kääriäinen Owned by: nobody
Component: Core (Management commands) Version: 1.8
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

With Django 1.8.5 and after following steps in a fresh project:

  1. Add settings.LOGGING = {'invalid'}
  2. python manage.py runserver

You'll get an exception mentioning that apps aren't ready yet. If you call django.setup() in a manual script you'll get a nice exception by which it is possible to see that your logging config is invalid.

The problem lies somewhere in the management command setup, or in the way get_app_configs() reports errors.

Stacktrace:

(tehku2_env)securejava@debian:~/projects/testapp$ python manage.py runserver
Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/home/securejava/projects/tehku2_env/lib/python3.4/site-packages/django/core/management/__init__.py", line 351, in execute_from_command_line
    utility.execute()
  File "/home/securejava/projects/tehku2_env/lib/python3.4/site-packages/django/core/management/__init__.py", line 343, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/securejava/projects/tehku2_env/lib/python3.4/site-packages/django/core/management/__init__.py", line 177, in fetch_command
    commands = get_commands()
  File "/home/securejava/projects/tehku2_env/lib/python3.4/functools.py", line 434, in wrapper
    result = user_function(*args, **kwds)
  File "/home/securejava/projects/tehku2_env/lib/python3.4/site-packages/django/core/management/__init__.py", line 72, in get_commands
    for app_config in reversed(list(apps.get_app_configs())):
  File "/home/securejava/projects/tehku2_env/lib/python3.4/site-packages/django/apps/registry.py", line 137, in get_app_configs
    self.check_apps_ready()
  File "/home/securejava/projects/tehku2_env/lib/python3.4/site-packages/django/apps/registry.py", line 124, in check_apps_ready
    raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.

Interestingly if you run instead manage.py --version, the exception isn't swallowed.

Change History (5)

comment:1 by Claude Paroz, 8 years ago

Summary: Django swallows exceptions when running management commandsrunserver swallows exceptions
Triage Stage: UnreviewedAccepted

This is due to runserver being special-cased from cc14d51ee8325c82665cb98af4dfe49aab565d52 / #24704 (sorry Aymeric!)

comment:2 by Aymeric Augustin, 8 years ago

This could be a duplicate of #25510. Anssi, can you test if https://github.com/django/django/pull/5402 improves the situation?

comment:3 by Anssi Kääriäinen, 8 years ago

Yes, that one fixes the issue. Instead of getting apps not ready exception, I can see the original exception on command line. If I fix the error in the settings, the project is correctly reloaded.

If I start the runserver with an exception caused by a mistake in models.py of some installed app, then I get the following exception:

(tehku2_env)securejava@debian:~/projects/tehku2$ python manage.py runserver
Traceback (most recent call last):
  File "/home/securejava/projects/tehku2_env/lib/python3.4/site-packages/django/core/management/__init__.py", line 317, in execute
    autoreload.check_errors(django.setup)()
  File "/home/securejava/projects/tehku2_env/lib/python3.4/site-packages/django/utils/autoreload.py", line 229, in wrapper
    fn(*args, **kwargs)
  File "/home/securejava/projects/tehku2_env/lib/python3.4/site-packages/django/__init__.py", line 18, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/home/securejava/projects/tehku2_env/lib/python3.4/site-packages/django/apps/registry.py", line 108, in populate
    app_config.import_models(all_models)
  File "/home/securejava/projects/tehku2_env/lib/python3.4/site-packages/django/apps/config.py", line 198, in import_models
    self.models_module = import_module(models_module_name)
  File "/home/securejava/projects/tehku2_env/lib/python3.4/importlib/__init__.py", line 109, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 2254, in _gcd_import
  File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
  File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 1129, in _exec
  File "<frozen importlib._bootstrap>", line 1471, in exec_module
  File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
  File "/home/securejava/projects/tehku2/tehku/models.py", line 114, in <module>
    raise Exception("hara")
Exception: hara

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "manage.py", line 9, in <module>
    execute_from_command_line(sys.argv)
  File "/home/securejava/projects/tehku2_env/lib/python3.4/site-packages/django/core/management/__init__.py", line 351, in execute_from_command_line
    utility.execute()
  File "/home/securejava/projects/tehku2_env/lib/python3.4/site-packages/django/core/management/__init__.py", line 321, in execute
    apps.populate([])
  File "/home/securejava/projects/tehku2_env/lib/python3.4/site-packages/django/apps/registry.py", line 78, in populate
    raise RuntimeError("populate() isn't reentrant")
RuntimeError: populate() isn't reentrant

I guess we want to avoid that.

comment:4 by Tim Graham, 8 years ago

Resolution: duplicate
Status: newclosed

I left the same comment about avoiding RuntimeError: populate() isn't reentrant on the pull request.

comment:5 by Aymeric Augustin, 8 years ago

I'm closing this ticket since it appears identical to #25510. FYI I updated the PR.

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