pinterest/pymemcache

KeyError caused by wrong key type.

fabiocaccamo opened this issue · 2 comments

Hi, thank you for this great lib!

I'm using pymemcache in a Python 3.8 + Django 3.2 project and I experienced an unexpected KeyError (specifically using the django.views.decorators.cache.cache_page decorator).

Unfortunately I'm not able to replicate it, but checking the logs the problem is clear:
Key is of type bytes while it is expected to be of type str.

The error has been raised here:

Screenshot 2022-12-06 at 10 33 08

Here variable values at error time:

Screenshot 2022-12-06 at 10 34 23

This is the error raw stack trace:

KeyError: b':1:views.decorators.cache.cache_page..GET.17467d2ae029dd3e1ccc2200de93e2f6.d41d8cd98f00b204e9800998ecf8427e.en'
  File "django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "django/core/handlers/base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "sitebase/views.py", line 95, in router
    page_response = page_obj.get_response(
  File "sitebase/models.py", line 195, in get_response
    view_response = view_function(request)
  File "django/views/decorators/http.py", line 40, in inner
    return func(request, *args, **kwargs)
  File "django/utils/decorators.py", line 122, in _wrapped_view
    result = middleware.process_request(request)
  File "django/middleware/cache.py", line 145, in process_request
    cache_key = get_cache_key(request, self.key_prefix, 'GET', cache=self.cache)
  File "django/utils/cache.py", line 362, in get_cache_key
    headerlist = cache.get(cache_key)
  File "django/core/cache/backends/memcached.py", line 77, in get
    return self._cache.get(key, default)
  File "pymemcache/client/hash.py", line 347, in get
    return self._run_cmd("get", key, default, default=default, **kwargs)
  File "pymemcache/client/hash.py", line 322, in _run_cmd
    return self._safely_run_func(client, func, default_val, *args, **kwargs)
  File "pymemcache/client/hash.py", line 211, in _safely_run_func
    result = func(*args, **kwargs)
  File "pymemcache/client/base.py", line 687, in get
    return self._fetch_cmd(b"get", [key], False, key_prefix=self.key_prefix).get(
  File "pymemcache/client/base.py", line 1153, in _fetch_cmd
    key, value, buf = self._extract_value(
  File "pymemcache/client/base.py", line 1107, in _extract_value
    original_key = remapped_keys[key]
pgcd commented

I'm experiencing the same problem and I found something perhaps interesting: all the error messages I receive (using Django as well) show a Safari or mobile Safari user agent, except for a few bots.

jogo commented

if you turn on memcached verbose mode you might be able to get enough information to reproduce / isolate the error.

That way we can see what keys and calls are being used at the memcached layer etc.

example output of memcached -vv

<27 set a 0 0 1 noreply
>27 NOREPLY STORED```