Repository: django-cms/django-cms

Issue:
Plugin pool initialises incorrectly if database is down during first request
We just had a failure on a production site because the MySQL server was down when Apache served the first request after a restart:

```
[Tue Jul 22 21:45:22 2014] [notice] caught SIGTERM, shutting down
[Tue Jul 22 21:45:23 2014] [notice] suEXEC mechanism enabled (wrapper: /usr/sbin/suexec)
[Tue Jul 22 21:45:23 2014] [warn] module wsgi_module is already loaded, skipping
[Tue Jul 22 21:45:23 2014] [notice] Digest: generating secret for digest authentication ...
[Tue Jul 22 21:45:23 2014] [notice] Digest: done
[Tue Jul 22 21:45:23 2014] [warn] mod_wsgi: Compiled for Python/2.7.6.
[Tue Jul 22 21:45:23 2014] [warn] mod_wsgi: Runtime using Python/2.7.7.
[Tue Jul 22 21:45:23 2014] [notice] Apache/2.2.15 (Unix) DAV/2 mod_ssl/2.2.15 OpenSSL/1.0.1e-fips mod_wsgi/3.2 Python/2.7.7 configured -- resuming normal operations
[Tue Jul 22 21:45:23 2014] [notice] Graceful restart requested, doing restart
[Tue Jul 22 21:45:23 2014] [warn] module wsgi_module is already loaded, skipping
[Tue Jul 22 21:45:23 2014] [warn] NameVirtualHost *:443 has no VirtualHosts
[Tue Jul 22 21:45:23 2014] [notice] Digest: generating secret for digest authentication ...
[Tue Jul 22 21:45:23 2014] [notice] Digest: done
[Tue Jul 22 21:45:23 2014] [warn] mod_wsgi: Compiled for Python/2.7.6.
[Tue Jul 22 21:45:23 2014] [warn] mod_wsgi: Runtime using Python/2.7.7.
[Tue Jul 22 21:45:23 2014] [notice] Apache/2.2.15 (Unix) DAV/2 mod_ssl/2.2.15 OpenSSL/1.0.1e-fips mod_wsgi/3.2 Python/2.7.7 configured -- resuming normal operations
[Tue Jul 22 21:45:46 2014] [error] /var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/django/conf/urls/defaults.py:3: DeprecationWarning: django.conf.urls.defaults is deprecated; use django.conf.urls instead
[Tue Jul 22 21:45:46 2014] [error]   DeprecationWarning)
[Tue Jul 22 21:45:46 2014] [error] 
[Tue Jul 22 21:45:47 2014] [error] Failed to patch Django signals for haystack: No module named aptivate_monkeypatch.monkeypatch
[Tue Jul 22 21:45:47 2014] [error] /var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/mptt/models.py:305: DeprecationWarning: Implicit manager CMSPlugin.tree will be removed in django-mptt 0.6.  Explicitly define a TreeManager() on your model to remove this warning.
[Tue Jul 22 21:45:47 2014] [error]   DeprecationWarning
[Tue Jul 22 21:45:47 2014] [error] 
[Tue Jul 22 21:45:48 2014] [error] /var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/django/conf/urls/defaults.py:3: DeprecationWarning: django.conf.urls.defaults is deprecated; use django.conf.urls instead
[Tue Jul 22 21:45:48 2014] [error]   DeprecationWarning)
[Tue Jul 22 21:45:48 2014] [error] 
```

...
    [Tue Jul 22 21:45:49 2014] [error] Internal Server Error: /en/
    [Tue Jul 22 21:45:49 2014] [error] Traceback (most recent call last):
    [Tue Jul 22 21:45:49 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/django/core/handlers/base.py", line 92, in get_response
    [Tue Jul 22 21:45:49 2014] [error]     response = middleware_method(request)
    [Tue Jul 22 21:45:49 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/django/middleware/locale.py", line 21, in process_request
    [Tue Jul 22 21:45:49 2014] [error]     check_path = self.is_language_prefix_patterns_used()
    [Tue Jul 22 21:45:49 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/django/middleware/locale.py", line 56, in is_language_prefix_patterns_used
    [Tue Jul 22 21:45:49 2014] [error]     for url_pattern in get_resolver(None).url_patterns:
    [Tue Jul 22 21:45:49 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/django/core/urlresolvers.py", line 347, in url_patterns
    [Tue Jul 22 21:45:49 2014] [error]     patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
    [Tue Jul 22 21:45:49 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/django/core/urlresolvers.py", line 342, in urlconf_module
    [Tue Jul 22 21:45:49 2014] [error]     self._urlconf_module = import_module(self.urlconf_name)
    [Tue Jul 22 21:45:49 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
    [Tue Jul 22 21:45:49 2014] [error]     __import__(name)
    [Tue Jul 22 21:45:49 2014] [error]   File "/var/django/washwatch/current/django/website/urls.py", line 22, in <module>
    [Tue Jul 22 21:45:49 2014] [error]     admin.autodiscover()
    [Tue Jul 22 21:45:49 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/django/contrib/admin/**init**.py", line 29, in autodiscover
    [Tue Jul 22 21:45:49 2014] [error]     import_module('%s.admin' % app)
    [Tue Jul 22 21:45:49 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
    [Tue Jul 22 21:45:49 2014] [error]     **import**(name)
    [Tue Jul 22 21:45:49 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/cms/admin/**init**.py", line 9, in <module>
    [Tue Jul 22 21:45:49 2014] [error]     plugin_pool.plugin_pool.discover_plugins()
    [Tue Jul 22 21:45:49 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/cms/plugin_pool.py", line 20, in discover_plugins
    [Tue Jul 22 21:45:49 2014] [error]     load('cms_plugins')
    [Tue Jul 22 21:45:49 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/cms/utils/django_load.py", line 42, in load
    [Tue Jul 22 21:45:49 2014] [error]     get_module(app, modname, verbose, failfast)
    [Tue Jul 22 21:45:49 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/cms/utils/django_load.py", line 21, in get_module
    [Tue Jul 22 21:45:49 2014] [error]     module = import_module(module_name)
    [Tue Jul 22 21:45:49 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
    [Tue Jul 22 21:45:49 2014] [error]     **import**(name)
    [Tue Jul 22 21:45:49 2014] [error]   File "/var/django/washwatch/current/django/website/main/cms_plugins.py", line 9, in <module>
    [Tue Jul 22 21:45:49 2014] [error]     from .forms import CountryForm
    [Tue Jul 22 21:45:49 2014] [error]   File "/var/django/washwatch/current/django/website/main/forms.py", line 15, in <module>
    [Tue Jul 22 21:45:49 2014] [error]     class CountryForm(forms.Form):
    [Tue Jul 22 21:45:49 2014] [error]   File "/var/django/washwatch/current/django/website/main/forms.py", line 19, in CountryForm
    [Tue Jul 22 21:45:49 2014] [error]     country = CountryField(empty_label=_("Select a country"))
    [Tue Jul 22 21:45:49 2014] [error]   File "/var/django/washwatch/current/django/website/main/country_field.py", line 42, in __init__
    [Tue Jul 22 21:45:49 2014] [error]     super(CountryField, self).**init**(queryset, _args, *_kwargs)
    [Tue Jul 22 21:45:49 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/django/forms/models.py", line 950, in **init**
    [Tue Jul 22 21:45:49 2014] [error]     self.queryset = queryset
    [Tue Jul 22 21:45:49 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/django/forms/models.py", line 965, in _set_queryset
    [Tue Jul 22 21:45:49 2014] [error]     self.widget.choices = self.choices
    [Tue Jul 22 21:45:49 2014] [error]   File "/var/django/washwatch/current/django/website/main/country_field.py", line 59, in choices
    [Tue Jul 22 21:45:49 2014] [error]     return items + [self.choice(o) for o in self._choices_list()]
    [Tue Jul 22 21:45:49 2014] [error]   File "/var/django/washwatch/2014-07-15_12-06-22/django/website/.ve/src/django-harness/django_harness/translation.py", line 254, in get_all_ordered_by_unicode
    [Tue Jul 22 21:45:49 2014] [error]     return sorted(self.model.objects.all(), key=self.model.__unicode__)
    [Tue Jul 22 21:45:49 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/django/db/models/query.py", line 108, in **len**
    [Tue Jul 22 21:45:49 2014] [error]     self._result_cache.extend(self._iter)
    [Tue Jul 22 21:45:49 2014] [error]   File "/var/django/washwatch/2014-07-15_12-06-22/django/website/.ve/src/django-hvad/hvad/manager.py", line 725, in iterator
    [Tue Jul 22 21:45:49 2014] [error]     instance = next(base_iter)
    [Tue Jul 22 21:45:49 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/django/db/models/query.py", line 317, in iterator
    [Tue Jul 22 21:45:49 2014] [error]     for row in compiler.results_iter():
    [Tue Jul 22 21:45:49 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 775, in results_iter
    [Tue Jul 22 21:45:49 2014] [error]     for rows in self.execute_sql(MULTI):
    [Tue Jul 22 21:45:49 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 845, in execute_sql
    [Tue Jul 22 21:45:49 2014] [error]     cursor = self.connection.cursor()
    [Tue Jul 22 21:45:49 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/django/db/backends/__init__.py", line 326, in cursor
    [Tue Jul 22 21:45:49 2014] [error]     cursor = util.CursorWrapper(self._cursor(), self)
    [Tue Jul 22 21:45:49 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 406, in _cursor
    [Tue Jul 22 21:45:49 2014] [error]     self.connection = Database.connect(**kwargs)
    [Tue Jul 22 21:45:49 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/MySQLdb/__init__.py", line 81, in Connect
    [Tue Jul 22 21:45:49 2014] [error]     return Connection(_args, *_kwargs)
    [Tue Jul 22 21:45:49 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/MySQLdb/connections.py", line 193, in **init**
    [Tue Jul 22 21:45:49 2014] [error]     super(Connection, self).**init**(_args, *_kwargs2)
    [Tue Jul 22 21:45:49 2014] [error] OperationalError: (2002, "Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)")

After this, every subsequent request failed with an error:

```
[Tue Jul 22 21:45:58 2014] [error] Internal Server Error: /
[Tue Jul 22 21:45:58 2014] [error] Traceback (most recent call last):
[Tue Jul 22 21:45:58 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/django/core/handlers/base.py", line 153, in get_response
[Tue Jul 22 21:45:58 2014] [error]     response = callback(request, **param_dict)
[Tue Jul 22 21:45:58 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/django/utils/decorators.py", line 91, in _wrapped_view
[Tue Jul 22 21:45:58 2014] [error]     response = view_func(request, *args, **kwargs)
[Tue Jul 22 21:45:58 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/django/views/defaults.py", line 26, in page_not_found
[Tue Jul 22 21:45:58 2014] [error]     return http.HttpResponseNotFound(template.render(RequestContext(request, {'request_path': request.path})))
[Tue Jul 22 21:45:58 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/django/template/base.py", line 140, in render
[Tue Jul 22 21:45:58 2014] [error]     return self._render(context)
[Tue Jul 22 21:45:58 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/django/template/base.py", line 134, in _render
[Tue Jul 22 21:45:58 2014] [error]     return self.nodelist.render(context)
[Tue Jul 22 21:45:58 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/django/template/base.py", line 830, in render
[Tue Jul 22 21:45:58 2014] [error]     bit = self.render_node(node, context)
[Tue Jul 22 21:45:58 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/django/template/base.py", line 844, in render_node
[Tue Jul 22 21:45:58 2014] [error]     return node.render(context)
[Tue Jul 22 21:45:58 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/django/template/loader_tags.py", line 124, in render
[Tue Jul 22 21:45:58 2014] [error]     return compiled_parent._render(context)
[Tue Jul 22 21:45:58 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/django/template/base.py", line 134, in _render
[Tue Jul 22 21:45:58 2014] [error]     return self.nodelist.render(context)
[Tue Jul 22 21:45:58 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/django/template/base.py", line 830, in render
[Tue Jul 22 21:45:58 2014] [error]     bit = self.render_node(node, context)
[Tue Jul 22 21:45:58 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/django/template/base.py", line 844, in render_node
[Tue Jul 22 21:45:58 2014] [error]     return node.render(context)
[Tue Jul 22 21:45:58 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/django/template/loader_tags.py", line 124, in render
[Tue Jul 22 21:45:58 2014] [error]     return compiled_parent._render(context)
[Tue Jul 22 21:45:58 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/django/template/base.py", line 134, in _render
[Tue Jul 22 21:45:58 2014] [error]     return self.nodelist.render(context)
[Tue Jul 22 21:45:58 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/django/template/base.py", line 830, in render
[Tue Jul 22 21:45:58 2014] [error]     bit = self.render_node(node, context)
[Tue Jul 22 21:45:58 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/django/template/base.py", line 844, in render_node
[Tue Jul 22 21:45:58 2014] [error]     return node.render(context)
[Tue Jul 22 21:45:58 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/django/template/loader_tags.py", line 124, in render
[Tue Jul 22 21:45:58 2014] [error]     return compiled_parent._render(context)
[Tue Jul 22 21:45:58 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/django/template/base.py", line 134, in _render
[Tue Jul 22 21:45:58 2014] [error]     return self.nodelist.render(context)
[Tue Jul 22 21:45:58 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/django/template/base.py", line 830, in render
[Tue Jul 22 21:45:58 2014] [error]     bit = self.render_node(node, context)
[Tue Jul 22 21:45:58 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/django/template/base.py", line 844, in render_node
[Tue Jul 22 21:45:58 2014] [error]     return node.render(context)
[Tue Jul 22 21:45:58 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/classytags/core.py", line 106, in render
[Tue Jul 22 21:45:58 2014] [error]     return self.render_tag(context, **kwargs)
[Tue Jul 22 21:45:58 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/sekizai/templatetags/sekizai_tags.py", line 76, in render_tag
[Tue Jul 22 21:45:58 2014] [error]     rendered_contents = nodelist.render(context)
[Tue Jul 22 21:45:58 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/django/template/base.py", line 830, in render
[Tue Jul 22 21:45:58 2014] [error]     bit = self.render_node(node, context)
[Tue Jul 22 21:45:58 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/django/template/base.py", line 844, in render_node
[Tue Jul 22 21:45:58 2014] [error]     return node.render(context)
[Tue Jul 22 21:45:58 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/django/template/loader_tags.py", line 63, in render
[Tue Jul 22 21:45:58 2014] [error]     result = block.nodelist.render(context)
[Tue Jul 22 21:45:58 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/django/template/base.py", line 830, in render
[Tue Jul 22 21:45:58 2014] [error]     bit = self.render_node(node, context)
[Tue Jul 22 21:45:58 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/django/template/base.py", line 844, in render_node
[Tue Jul 22 21:45:58 2014] [error]     return node.render(context)
[Tue Jul 22 21:45:58 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/classytags/core.py", line 106, in render
[Tue Jul 22 21:45:58 2014] [error]     return self.render_tag(context, **kwargs)
[Tue Jul 22 21:45:58 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/classytags/helpers.py", line 78, in render_tag
[Tue Jul 22 21:45:58 2014] [error]     data = self.get_context(context, **kwargs)
[Tue Jul 22 21:45:58 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/cms/templatetags/cms_tags.py", line 468, in get_context
[Tue Jul 22 21:45:58 2014] [error]     return _show_placeholder_for_page(**self.get_kwargs(*args, **kwargs))
[Tue Jul 22 21:45:58 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/cms/templatetags/cms_tags.py", line 446, in _show_placeholder_for_page
[Tue Jul 22 21:45:58 2014] [error]     content = render_placeholder(placeholder, context, placeholder_name)
[Tue Jul 22 21:45:58 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/cms/plugin_rendering.py", line 94, in render_placeholder
[Tue Jul 22 21:45:58 2014] [error]     plugins = [plugin for plugin in get_plugins(request, placeholder, lang=lang)]
[Tue Jul 22 21:45:58 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/cms/plugins/utils.py", line 20, in get_plugins
[Tue Jul 22 21:45:58 2014] [error]     assign_plugins(request, [placeholder], lang)
[Tue Jul 22 21:45:58 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/cms/plugins/utils.py", line 46, in assign_plugins
[Tue Jul 22 21:45:58 2014] [error]     plugin_list = downcast_plugins(qs)
[Tue Jul 22 21:45:58 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/cms/plugins/utils.py", line 83, in downcast_plugins
[Tue Jul 22 21:45:58 2014] [error]     cls = plugin_pool.get_plugin(plugin_type)
[Tue Jul 22 21:45:58 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/cms/plugin_pool.py", line 114, in get_plugin
[Tue Jul 22 21:45:58 2014] [error]     return self.plugins[name]
[Tue Jul 22 21:45:58 2014] [error] KeyError: u'SnippetPlugin'
```

I think the problem comes from this code in `cms/plugin_pool.py`:

```
class PluginPool(object):
    def discover_plugins(self):
        if self.discovered:
            return
        self.discovered = True
        load('cms_plugins')
```

If `load('cms_plugins')` throws an exception then the PluginPool will think that it's initialised, but it's not.

Since `load` is idempotent (can be safely repeated), I propose that we move `self.discovered = True` to after `load`, so that it doesn't happen if `load` throws an exception, and the next attempt to retrieve the plugin pool will try again to discover all plugins, hopefully successfully this time.

Plugin pool initialises incorrectly if database is down during first request
We just had a failure on a production site because the MySQL server was down when Apache served the first request after a restart:

```
[Tue Jul 22 21:45:22 2014] [notice] caught SIGTERM, shutting down
[Tue Jul 22 21:45:23 2014] [notice] suEXEC mechanism enabled (wrapper: /usr/sbin/suexec)
[Tue Jul 22 21:45:23 2014] [warn] module wsgi_module is already loaded, skipping
[Tue Jul 22 21:45:23 2014] [notice] Digest: generating secret for digest authentication ...
[Tue Jul 22 21:45:23 2014] [notice] Digest: done
[Tue Jul 22 21:45:23 2014] [warn] mod_wsgi: Compiled for Python/2.7.6.
[Tue Jul 22 21:45:23 2014] [warn] mod_wsgi: Runtime using Python/2.7.7.
[Tue Jul 22 21:45:23 2014] [notice] Apache/2.2.15 (Unix) DAV/2 mod_ssl/2.2.15 OpenSSL/1.0.1e-fips mod_wsgi/3.2 Python/2.7.7 configured -- resuming normal operations
[Tue Jul 22 21:45:23 2014] [notice] Graceful restart requested, doing restart
[Tue Jul 22 21:45:23 2014] [warn] module wsgi_module is already loaded, skipping
[Tue Jul 22 21:45:23 2014] [warn] NameVirtualHost *:443 has no VirtualHosts
[Tue Jul 22 21:45:23 2014] [notice] Digest: generating secret for digest authentication ...
[Tue Jul 22 21:45:23 2014] [notice] Digest: done
[Tue Jul 22 21:45:23 2014] [warn] mod_wsgi: Compiled for Python/2.7.6.
[Tue Jul 22 21:45:23 2014] [warn] mod_wsgi: Runtime using Python/2.7.7.
[Tue Jul 22 21:45:23 2014] [notice] Apache/2.2.15 (Unix) DAV/2 mod_ssl/2.2.15 OpenSSL/1.0.1e-fips mod_wsgi/3.2 Python/2.7.7 configured -- resuming normal operations
[Tue Jul 22 21:45:46 2014] [error] /var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/django/conf/urls/defaults.py:3: DeprecationWarning: django.conf.urls.defaults is deprecated; use django.conf.urls instead
[Tue Jul 22 21:45:46 2014] [error]   DeprecationWarning)
[Tue Jul 22 21:45:46 2014] [error] 
[Tue Jul 22 21:45:47 2014] [error] Failed to patch Django signals for haystack: No module named aptivate_monkeypatch.monkeypatch
[Tue Jul 22 21:45:47 2014] [error] /var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/mptt/models.py:305: DeprecationWarning: Implicit manager CMSPlugin.tree will be removed in django-mptt 0.6.  Explicitly define a TreeManager() on your model to remove this warning.
[Tue Jul 22 21:45:47 2014] [error]   DeprecationWarning
[Tue Jul 22 21:45:47 2014] [error] 
[Tue Jul 22 21:45:48 2014] [error] /var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/django/conf/urls/defaults.py:3: DeprecationWarning: django.conf.urls.defaults is deprecated; use django.conf.urls instead
[Tue Jul 22 21:45:48 2014] [error]   DeprecationWarning)
[Tue Jul 22 21:45:48 2014] [error] 
```

...
    [Tue Jul 22 21:45:49 2014] [error] Internal Server Error: /en/
    [Tue Jul 22 21:45:49 2014] [error] Traceback (most recent call last):
    [Tue Jul 22 21:45:49 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/django/core/handlers/base.py", line 92, in get_response
    [Tue Jul 22 21:45:49 2014] [error]     response = middleware_method(request)
    [Tue Jul 22 21:45:49 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/django/middleware/locale.py", line 21, in process_request
    [Tue Jul 22 21:45:49 2014] [error]     check_path = self.is_language_prefix_patterns_used()
    [Tue Jul 22 21:45:49 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/django/middleware/locale.py", line 56, in is_language_prefix_patterns_used
    [Tue Jul 22 21:45:49 2014] [error]     for url_pattern in get_resolver(None).url_patterns:
    [Tue Jul 22 21:45:49 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/django/core/urlresolvers.py", line 347, in url_patterns
    [Tue Jul 22 21:45:49 2014] [error]     patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
    [Tue Jul 22 21:45:49 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/django/core/urlresolvers.py", line 342, in urlconf_module
    [Tue Jul 22 21:45:49 2014] [error]     self._urlconf_module = import_module(self.urlconf_name)
    [Tue Jul 22 21:45:49 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
    [Tue Jul 22 21:45:49 2014] [error]     __import__(name)
    [Tue Jul 22 21:45:49 2014] [error]   File "/var/django/washwatch/current/django/website/urls.py", line 22, in <module>
    [Tue Jul 22 21:45:49 2014] [error]     admin.autodiscover()
    [Tue Jul 22 21:45:49 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/django/contrib/admin/**init**.py", line 29, in autodiscover
    [Tue Jul 22 21:45:49 2014] [error]     import_module('%s.admin' % app)
    [Tue Jul 22 21:45:49 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
    [Tue Jul 22 21:45:49 2014] [error]     **import**(name)
    [Tue Jul 22 21:45:49 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/cms/admin/**init**.py", line 9, in <module>
    [Tue Jul 22 21:45:49 2014] [error]     plugin_pool.plugin_pool.discover_plugins()
    [Tue Jul 22 21:45:49 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/cms/plugin_pool.py", line 20, in discover_plugins
    [Tue Jul 22 21:45:49 2014] [error]     load('cms_plugins')
    [Tue Jul 22 21:45:49 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/cms/utils/django_load.py", line 42, in load
    [Tue Jul 22 21:45:49 2014] [error]     get_module(app, modname, verbose, failfast)
    [Tue Jul 22 21:45:49 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/cms/utils/django_load.py", line 21, in get_module
    [Tue Jul 22 21:45:49 2014] [error]     module = import_module(module_name)
    [Tue Jul 22 21:45:49 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
    [Tue Jul 22 21:45:49 2014] [error]     **import**(name)
    [Tue Jul 22 21:45:49 2014] [error]   File "/var/django/washwatch/current/django/website/main/cms_plugins.py", line 9, in <module>
    [Tue Jul 22 21:45:49 2014] [error]     from .forms import CountryForm
    [Tue Jul 22 21:45:49 2014] [error]   File "/var/django/washwatch/current/django/website/main/forms.py", line 15, in <module>
    [Tue Jul 22 21:45:49 2014] [error]     class CountryForm(forms.Form):
    [Tue Jul 22 21:45:49 2014] [error]   File "/var/django/washwatch/current/django/website/main/forms.py", line 19, in CountryForm
    [Tue Jul 22 21:45:49 2014] [error]     country = CountryField(empty_label=_("Select a country"))
    [Tue Jul 22 21:45:49 2014] [error]   File "/var/django/washwatch/current/django/website/main/country_field.py", line 42, in __init__
    [Tue Jul 22 21:45:49 2014] [error]     super(CountryField, self).**init**(queryset, _args, *_kwargs)
    [Tue Jul 22 21:45:49 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/django/forms/models.py", line 950, in **init**
    [Tue Jul 22 21:45:49 2014] [error]     self.queryset = queryset
    [Tue Jul 22 21:45:49 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/django/forms/models.py", line 965, in _set_queryset
    [Tue Jul 22 21:45:49 2014] [error]     self.widget.choices = self.choices
    [Tue Jul 22 21:45:49 2014] [error]   File "/var/django/washwatch/current/django/website/main/country_field.py", line 59, in choices
    [Tue Jul 22 21:45:49 2014] [error]     return items + [self.choice(o) for o in self._choices_list()]
    [Tue Jul 22 21:45:49 2014] [error]   File "/var/django/washwatch/2014-07-15_12-06-22/django/website/.ve/src/django-harness/django_harness/translation.py", line 254, in get_all_ordered_by_unicode
    [Tue Jul 22 21:45:49 2014] [error]     return sorted(self.model.objects.all(), key=self.model.__unicode__)
    [Tue Jul 22 21:45:49 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/django/db/models/query.py", line 108, in **len**
    [Tue Jul 22 21:45:49 2014] [error]     self._result_cache.extend(self._iter)
    [Tue Jul 22 21:45:49 2014] [error]   File "/var/django/washwatch/2014-07-15_12-06-22/django/website/.ve/src/django-hvad/hvad/manager.py", line 725, in iterator
    [Tue Jul 22 21:45:49 2014] [error]     instance = next(base_iter)
    [Tue Jul 22 21:45:49 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/django/db/models/query.py", line 317, in iterator
    [Tue Jul 22 21:45:49 2014] [error]     for row in compiler.results_iter():
    [Tue Jul 22 21:45:49 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 775, in results_iter
    [Tue Jul 22 21:45:49 2014] [error]     for rows in self.execute_sql(MULTI):
    [Tue Jul 22 21:45:49 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 845, in execute_sql
    [Tue Jul 22 21:45:49 2014] [error]     cursor = self.connection.cursor()
    [Tue Jul 22 21:45:49 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/django/db/backends/__init__.py", line 326, in cursor
    [Tue Jul 22 21:45:49 2014] [error]     cursor = util.CursorWrapper(self._cursor(), self)
    [Tue Jul 22 21:45:49 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 406, in _cursor
    [Tue Jul 22 21:45:49 2014] [error]     self.connection = Database.connect(**kwargs)
    [Tue Jul 22 21:45:49 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/MySQLdb/__init__.py", line 81, in Connect
    [Tue Jul 22 21:45:49 2014] [error]     return Connection(_args, *_kwargs)
    [Tue Jul 22 21:45:49 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/MySQLdb/connections.py", line 193, in **init**
    [Tue Jul 22 21:45:49 2014] [error]     super(Connection, self).**init**(_args, *_kwargs2)
    [Tue Jul 22 21:45:49 2014] [error] OperationalError: (2002, "Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)")

After this, every subsequent request failed with an error:

```
[Tue Jul 22 21:45:58 2014] [error] Internal Server Error: /
[Tue Jul 22 21:45:58 2014] [error] Traceback (most recent call last):
[Tue Jul 22 21:45:58 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/django/core/handlers/base.py", line 153, in get_response
[Tue Jul 22 21:45:58 2014] [error]     response = callback(request, **param_dict)
[Tue Jul 22 21:45:58 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/django/utils/decorators.py", line 91, in _wrapped_view
[Tue Jul 22 21:45:58 2014] [error]     response = view_func(request, *args, **kwargs)
[Tue Jul 22 21:45:58 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/django/views/defaults.py", line 26, in page_not_found
[Tue Jul 22 21:45:58 2014] [error]     return http.HttpResponseNotFound(template.render(RequestContext(request, {'request_path': request.path})))
[Tue Jul 22 21:45:58 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/django/template/base.py", line 140, in render
[Tue Jul 22 21:45:58 2014] [error]     return self._render(context)
[Tue Jul 22 21:45:58 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/django/template/base.py", line 134, in _render
[Tue Jul 22 21:45:58 2014] [error]     return self.nodelist.render(context)
[Tue Jul 22 21:45:58 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/django/template/base.py", line 830, in render
[Tue Jul 22 21:45:58 2014] [error]     bit = self.render_node(node, context)
[Tue Jul 22 21:45:58 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/django/template/base.py", line 844, in render_node
[Tue Jul 22 21:45:58 2014] [error]     return node.render(context)
[Tue Jul 22 21:45:58 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/django/template/loader_tags.py", line 124, in render
[Tue Jul 22 21:45:58 2014] [error]     return compiled_parent._render(context)
[Tue Jul 22 21:45:58 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/django/template/base.py", line 134, in _render
[Tue Jul 22 21:45:58 2014] [error]     return self.nodelist.render(context)
[Tue Jul 22 21:45:58 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/django/template/base.py", line 830, in render
[Tue Jul 22 21:45:58 2014] [error]     bit = self.render_node(node, context)
[Tue Jul 22 21:45:58 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/django/template/base.py", line 844, in render_node
[Tue Jul 22 21:45:58 2014] [error]     return node.render(context)
[Tue Jul 22 21:45:58 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/django/template/loader_tags.py", line 124, in render
[Tue Jul 22 21:45:58 2014] [error]     return compiled_parent._render(context)
[Tue Jul 22 21:45:58 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/django/template/base.py", line 134, in _render
[Tue Jul 22 21:45:58 2014] [error]     return self.nodelist.render(context)
[Tue Jul 22 21:45:58 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/django/template/base.py", line 830, in render
[Tue Jul 22 21:45:58 2014] [error]     bit = self.render_node(node, context)
[Tue Jul 22 21:45:58 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/django/template/base.py", line 844, in render_node
[Tue Jul 22 21:45:58 2014] [error]     return node.render(context)
[Tue Jul 22 21:45:58 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/django/template/loader_tags.py", line 124, in render
[Tue Jul 22 21:45:58 2014] [error]     return compiled_parent._render(context)
[Tue Jul 22 21:45:58 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/django/template/base.py", line 134, in _render
[Tue Jul 22 21:45:58 2014] [error]     return self.nodelist.render(context)
[Tue Jul 22 21:45:58 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/django/template/base.py", line 830, in render
[Tue Jul 22 21:45:58 2014] [error]     bit = self.render_node(node, context)
[Tue Jul 22 21:45:58 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/django/template/base.py", line 844, in render_node
[Tue Jul 22 21:45:58 2014] [error]     return node.render(context)
[Tue Jul 22 21:45:58 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/classytags/core.py", line 106, in render
[Tue Jul 22 21:45:58 2014] [error]     return self.render_tag(context, **kwargs)
[Tue Jul 22 21:45:58 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/sekizai/templatetags/sekizai_tags.py", line 76, in render_tag
[Tue Jul 22 21:45:58 2014] [error]     rendered_contents = nodelist.render(context)
[Tue Jul 22 21:45:58 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/django/template/base.py", line 830, in render
[Tue Jul 22 21:45:58 2014] [error]     bit = self.render_node(node, context)
[Tue Jul 22 21:45:58 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/django/template/base.py", line 844, in render_node
[Tue Jul 22 21:45:58 2014] [error]     return node.render(context)
[Tue Jul 22 21:45:58 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/django/template/loader_tags.py", line 63, in render
[Tue Jul 22 21:45:58 2014] [error]     result = block.nodelist.render(context)
[Tue Jul 22 21:45:58 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/django/template/base.py", line 830, in render
[Tue Jul 22 21:45:58 2014] [error]     bit = self.render_node(node, context)
[Tue Jul 22 21:45:58 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/django/template/base.py", line 844, in render_node
[Tue Jul 22 21:45:58 2014] [error]     return node.render(context)
[Tue Jul 22 21:45:58 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/classytags/core.py", line 106, in render
[Tue Jul 22 21:45:58 2014] [error]     return self.render_tag(context, **kwargs)
[Tue Jul 22 21:45:58 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/classytags/helpers.py", line 78, in render_tag
[Tue Jul 22 21:45:58 2014] [error]     data = self.get_context(context, **kwargs)
[Tue Jul 22 21:45:58 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/cms/templatetags/cms_tags.py", line 468, in get_context
[Tue Jul 22 21:45:58 2014] [error]     return _show_placeholder_for_page(**self.get_kwargs(*args, **kwargs))
[Tue Jul 22 21:45:58 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/cms/templatetags/cms_tags.py", line 446, in _show_placeholder_for_page
[Tue Jul 22 21:45:58 2014] [error]     content = render_placeholder(placeholder, context, placeholder_name)
[Tue Jul 22 21:45:58 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/cms/plugin_rendering.py", line 94, in render_placeholder
[Tue Jul 22 21:45:58 2014] [error]     plugins = [plugin for plugin in get_plugins(request, placeholder, lang=lang)]
[Tue Jul 22 21:45:58 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/cms/plugins/utils.py", line 20, in get_plugins
[Tue Jul 22 21:45:58 2014] [error]     assign_plugins(request, [placeholder], lang)
[Tue Jul 22 21:45:58 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/cms/plugins/utils.py", line 46, in assign_plugins
[Tue Jul 22 21:45:58 2014] [error]     plugin_list = downcast_plugins(qs)
[Tue Jul 22 21:45:58 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/cms/plugins/utils.py", line 83, in downcast_plugins
[Tue Jul 22 21:45:58 2014] [error]     cls = plugin_pool.get_plugin(plugin_type)
[Tue Jul 22 21:45:58 2014] [error]   File "/var/django/washwatch/current/django/website/.ve/lib/python2.7/site-packages/cms/plugin_pool.py", line 114, in get_plugin
[Tue Jul 22 21:45:58 2014] [error]     return self.plugins[name]
[Tue Jul 22 21:45:58 2014] [error] KeyError: u'SnippetPlugin'
```

I think the problem comes from this code in `cms/plugin_pool.py`:

```
class PluginPool(object):
    def discover_plugins(self):
        if self.discovered:
            return
        self.discovered = True
        load('cms_plugins')
```

If `load('cms_plugins')` throws an exception then the PluginPool will think that it's initialised, but it's not.

Since `load` is idempotent (can be safely repeated), I propose that we move `self.discovered = True` to after `load`, so that it doesn't happen if `load` throws an exception, and the next attempt to retrieve the plugin pool will try again to discover all plugins, hopefully successfully this time.



Discussion:
Sounds reasonable.

Sounds reasonable.


Resolving patch:
diff --git a/cms/plugin_pool.py b/cms/plugin_pool.py
--- a/cms/plugin_pool.py
+++ b/cms/plugin_pool.py
@@ -31,10 +31,10 @@ def __init__(self):
     def discover_plugins(self):
         if self.discovered:
             return
-        self.discovered = True
         from cms.views import invalidate_cms_page_cache
         invalidate_cms_page_cache()
         load('cms_plugins')
+        self.discovered = True
 
     def clear(self):
         self.discovered = False


Test patch: