#33252: CacheMiddleware is not thread safe
------------------------------------------+------------------------
               Reporter:  Iuri de Silvio  |          Owner:  nobody
                   Type:  Uncategorized   |         Status:  new
              Component:  Uncategorized   |        Version:  3.2
               Severity:  Normal          |       Keywords:
           Triage Stage:  Unreviewed      |      Has patch:  0
    Needs documentation:  0               |    Needs tests:  0
Patch needs improvement:  0               |  Easy pickings:  0
                  UI/UX:  0               |
------------------------------------------+------------------------
 CacheMiddleware persist `self.cache = caches[cache_alias]` on startup and
 it is not thread safe.
 https://github.com/django/django/blob/main/django/middleware/cache.py#L186

 I found that after some production errors with pylibmc and uwsgi threaded.
 Created a small project to reproduce it. Nothing fancy, just pylibmc cache
 and a `@cache_page` cached view. It fails even with development server,
 with concurrent requests.

 {{{
 Traceback (most recent call last):
   File "versions/pylibmcbug/lib/python3.9/site-
 packages/django/core/handlers/exception.py", line 47, in inner
     response = get_response(request)
   File "versions/pylibmcbug/lib/python3.9/site-
 packages/django/core/handlers/base.py", line 181, in _get_response
     response = wrapped_callback(request, *callback_args,
 **callback_kwargs)
   File "versions/pylibmcbug/lib/python3.9/site-
 packages/django/utils/decorators.py", line 122, in _wrapped_view
     result = middleware.process_request(request)
   File "versions/pylibmcbug/lib/python3.9/site-
 packages/django/middleware/cache.py", line 145, in process_request
     cache_key = get_cache_key(request, self.key_prefix, 'GET',
 cache=self.cache)
   File "versions/pylibmcbug/lib/python3.9/site-
 packages/django/utils/cache.py", line 362, in get_cache_key
     headerlist = cache.get(cache_key)
   File "versions/pylibmcbug/lib/python3.9/site-
 packages/django/core/cache/backends/memcached.py", line 77, in get
     return self._cache.get(key, default)
 pylibmc.ConnectionError: error 3 from
 memcached_get(:1:views.decorators.cache.cache_): (0x7f290400bd60) FAILURE,
 poll() returned a value that was not dealt with,  host: localhost:11211 ->
 libmemcached/io.cc:254
 }}}

 Looking for git history, it is this way since 2010.
 
https://github.com/django/django/commit/673e6fc7fb243ed44841b9969d26a161c25733b3

-- 
Ticket URL: <https://code.djangoproject.com/ticket/33252>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/053.14c0f9e3b3f7dc460c64c80f839ad632%40djangoproject.com.

Reply via email to