Skip to content
Latchkey

Cassandra "NoHostAvailable" from the Python driver in CI

The DataStax driver raises NoHostAvailable when it cannot establish a working connection to any node in its contact points. In CI this is almost always a node that is up on TCP but not yet ready, or a contact point pointing at the wrong host.

What this error means

Connecting raises "cassandra.cluster.NoHostAvailable: ('Unable to connect to any servers', ...)" with a per-host error map. The map shows whether each host was refused, timed out, or reset.

python
cassandra.cluster.NoHostAvailable: ('Unable to connect to any servers',
{'127.0.0.1:9042': ConnectionRefusedError(111, 'Connection refused')})

Common causes

Every contact point is still starting

The driver opened the control connection before the node finished bootstrapping, so all contact points failed and it gives up with NoHostAvailable.

The contact point host is wrong for the job context

Using 127.0.0.1 from inside a container network (or a container alias from the host) means the driver never reaches the live node.

How to fix it

Retry the cluster connect with backoff

  1. Wrap Cluster(...).connect() in a retry loop.
  2. Catch NoHostAvailable and sleep before retrying.
  3. Only proceed once a session is returned.
python
from cassandra.cluster import Cluster, NoHostAvailable
import time
for _ in range(30):
    try:
        session = Cluster(['127.0.0.1'], port=9042).connect()
        break
    except NoHostAvailable:
        time.sleep(3)

Use the correct contact point for the network

From a job step that runs inside the job container, use the service name; from the host, map the port and use 127.0.0.1.

.github/workflows/ci.yml
services:
  cassandra:
    image: cassandra:5.0
    ports: ['9042:9042']

How to prevent it

  • Retry the initial connect; the first node is slow to come up.
  • Match contact points to where the step actually runs.
  • Read the per-host error map to tell refused apart from timed out.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →