Skip to content
Latchkey

Python "Celery worker cannot connect to broker" in CI

A Celery worker that cannot reach its broker logs "consumer: Cannot connect to ..." and retries. In CI tests this stalls or fails when the broker (Redis or RabbitMQ) service is not running or the broker URL is wrong.

What this error means

CI shows "consumer: Cannot connect to redis://localhost:6379//: Error connecting to Redis." repeating, and Celery-backed tests time out or fail.

python
[ERROR/MainProcess] consumer: Cannot connect to redis://localhost:6379//: Error connecting to Redis.
Trying again in 2.00 seconds...

Common causes

The broker service is not running in CI

No Redis/RabbitMQ service was started, or it was not ready when the worker booted.

A broker URL that is correct locally but wrong in CI

localhost vs a service hostname mismatch in the CI network topology.

How to fix it

Run the broker as a service and use eager mode where possible

  1. Start the broker service in the job and set CELERY_BROKER_URL to match.
  2. For unit tests, set task_always_eager=True so tasks run inline without a broker.
  3. Gate integration tests on a broker readiness check.
Python
# unit tests: run tasks inline, no broker needed
app.conf.task_always_eager = True
app.conf.task_eager_propagates = True

How to prevent it

  • On Latchkey managed runners, transient broker connection failures during startup are auto-retried.
  • Use task_always_eager for unit tests.
  • Gate Celery integration tests on broker readiness.

Related guides

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