Skip to content
Latchkey

pymemcache / python-memcached connection failure in CI

A Python memcached client (pymemcache or python-memcached) failed to connect to a configured server. With a single node it is a readiness or host problem; with a multi-server pooled client, consistent hashing may route a key to a node that is down in CI.

What this error means

Tests fail with "MemcacheUnexpectedCloseError", "Connection refused", or silent misses because a HashClient node is unreachable in CI.

python
pymemcache.exceptions.MemcacheServerError: (b'localhost:11211',
'Connection refused')

Common causes

Single node not ready or wrong host

The memcached service was not ready, or the host is localhost when the job runs in a container that needs the service name.

A dead node in a consistent-hashing pool

A HashClient distributes keys across servers; if one configured node is not running in CI, keys hashing to it fail.

How to fix it

Connect to a single ready node in CI

  1. Point the client at one memcached the workflow actually starts.
  2. Wait for the port to open before the first call.
  3. Read the host/port from environment so it is portable.
conftest.py
import os
from pymemcache.client.base import Client
c = Client((os.environ.get("MEMCACHED_HOST", "localhost"), 11211))
c.set(b"k", b"v")
assert c.get(b"k") == b"v"

Only configure nodes that exist in CI

If using HashClient, list only the servers the workflow starts so consistent hashing never routes to a missing node.

python
from pymemcache.client.hash import HashClient
c = HashClient([("localhost", 11211)])

How to prevent it

  • Wait for memcached readiness before the first client call.
  • Configure only the nodes the workflow actually starts.
  • Externalize host/port so tests run against the CI service.

Frequently asked questions

What causes "pymemcache connection failure"?
The memcached service was not ready, or the host is localhost when the job runs in a container that needs the service name.
How do I fix pymemcache connection failure?
Connect to a single ready node in CI

Related guides

References

Latchkey auto-heals failures like this one - detected, fixed, and retried without you. Start free → 30-day trial · No credit card