Skip to content

'NoneType' object has no attribute 'recv' #612

Description

@BertrandBordage

This error happens occasionally at random in my application.
The application:

  • uses gunicorn --workers 1 --threads 8, so with gthread
  • connects to memcached through a UNIX socket
  • the application and memcached run in separate Docker Compose services, with the UNIX socket being shared through a named volume

Cache configuration in Django:

CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.memcached.PyMemcacheCache',
        'LOCATION': 'unix:/var/run/memcached/memcached.sock',
        'OPTIONS': {
            'use_pooling': True,  # Keeps connections alive, more reliable with multiple threads per worker.
            'default_noreply': True,  # Do not wait for a reply when we do not need it.
            'connect_timeout': 10.0,  # seconds
            'timeout': 10.0,  # seconds
        },
    },
}

'ignore_exc': True could be specified to consider this error a cache miss (if this type of error is correctly caught), but this means we would not know whether 1% or 100% of the cache calls are failing.

Versions:
pymemcache 4.0.0
memcached 1.6.31 (from the memcached:1.6.31-alpine Docker image)
gunicorn 23.0.0
Python 3.12.4
Django 5.0.7

Relevant traceback:

  File "/srv/.local/lib/python3.12/site-packages/django/core/cache/backends/memcached.py", line 75, in get
    return self._cache.get(key, default)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/srv/.local/lib/python3.12/site-packages/pymemcache/client/hash.py", line 347, in get
    return self._run_cmd("get", key, default, default=default, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/srv/.local/lib/python3.12/site-packages/pymemcache/client/hash.py", line 322, in _run_cmd
    return self._safely_run_func(client, func, default_val, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/srv/.local/lib/python3.12/site-packages/pymemcache/client/hash.py", line 211, in _safely_run_func
    result = func(*args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^
  File "/srv/.local/lib/python3.12/site-packages/pymemcache/client/base.py", line 1494, in get
    return client.get(key, default)
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/srv/.local/lib/python3.12/site-packages/pymemcache/client/base.py", line 687, in get
    return self._fetch_cmd(b"get", [key], False, key_prefix=self.key_prefix).get(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/srv/.local/lib/python3.12/site-packages/pymemcache/client/base.py", line 1145, in _fetch_cmd
    buf, line = _readline(self.sock, buf)
                ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/srv/.local/lib/python3.12/site-packages/pymemcache/client/base.py", line 1658, in _readline
    buf = _recv(sock, RECV_SIZE)
          ^^^^^^^^^^^^^^^^^^^^^^
  File "/srv/.local/lib/python3.12/site-packages/pymemcache/client/base.py", line 1750, in _recv
    return sock.recv(size)
           ^^^^^^^^^

EDIT: The memcached server was up and running at that time. docker compose ps considered it healthy for the entire day this issue happened.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions