Skip to content
Latchkey

Celery "consumer: Cannot connect to amqp://..." broker in CI

The Celery worker (or a task call) could not reach the AMQP broker named in broker_url. In CI this is a RabbitMQ service that is not up yet, or a broker URL that does not match the service host and port.

What this error means

Celery logs "consumer: Cannot connect to amqp://guest:**@127.0.0.1:5672//: [Errno 111] Connection refused. Trying again in 2.00 seconds..." and the worker never becomes ready.

python
[ERROR/MainProcess] consumer: Cannot connect to amqp://guest:**@127.0.0.1:5672//:
[Errno 111] Connection refused.
Trying again in 2.00 seconds... (1/100)

Common causes

RabbitMQ service is not ready

The worker started before the broker container finished initializing, so the connection is refused and retried.

The broker URL does not match the service

broker_url points at a host/port the runner cannot reach (wrong service name, unpublished port).

How to fix it

Add a RabbitMQ service and matching broker URL

  1. Run a RabbitMQ service with a readiness check.
  2. Set CELERY_BROKER_URL to the service host and port.
  3. Start the worker only after the broker is reachable.
.github/workflows/ci.yml
services:
  rabbitmq:
    image: rabbitmq:3
    ports: ['5672:5672']
    options: --health-cmd "rabbitmq-diagnostics -q ping" --health-interval 5s --health-retries 10
env:
  CELERY_BROKER_URL: amqp://guest:guest@127.0.0.1:5672//

Run tasks eagerly in tests

For unit tests that do not need a real broker, run tasks synchronously so no connection is required.

python
app.conf.task_always_eager = True
app.conf.task_eager_propagates = True

How to prevent it

  • Gate the worker on a broker health check.
  • Drive broker_url from env so CI matches the service.
  • Use task_always_eager for tests that do not need a broker.

Frequently asked questions

What causes ""Cannot connect to amqp://""?
The worker started before the broker container finished initializing, so the connection is refused and retried.
How do I fix "Cannot connect to amqp://"?
Add a RabbitMQ service and matching broker URL

Related guides

References

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