Fix OperationalError: kombu.exceptions.OperationalError: [Errno 111] Connection refused in Celery
This error occurs when a Celery worker or producer cannot connect to the message broker, usually Redis or RabbitMQ. The broker may not be running, the connection URL may be wrong, or a firewall is blocking the port. Fix it by verifying the broker is running, the CELERY_BROKER_URL is correct, and the broker port is accessible from your application.
Reading the Stack Trace
Here's what each line means:
- File "/app/venv/lib/python3.12/site-packages/kombu/connection.py", line 451, in ensure_connection: Kombu (Celery's messaging library) tries to establish a connection to the broker but fails after retries.
- File "/app/venv/lib/python3.12/site-packages/kombu/transport/pyamqp.py", line 201, in establish_connection: The AMQP transport layer attempts a TCP connection to the broker host and port.
- kombu.exceptions.OperationalError: [Errno 111] Connection refused: The operating system refused the TCP connection, meaning nothing is listening on the broker's port.
Common Causes
1. Broker service not running
Redis or RabbitMQ is not started, so there is nothing listening on the expected port.
# docker-compose.yml missing redis service
services:
web:
build: .
worker:
command: celery -A app.celery worker
# No redis service defined!
2. Wrong broker URL
The CELERY_BROKER_URL points to the wrong host, port, or protocol.
# config.py
CELERY_BROKER_URL = 'amqp://localhost:5672' # RabbitMQ not on this host in Docker
3. Network or firewall blocking connection
A firewall rule or Docker network configuration prevents the worker from reaching the broker.
# Docker services on different networks:
services:
redis:
networks: [backend]
worker:
networks: [frontend] # cannot reach redis
The Fix
Ensure the broker service is running and the CELERY_BROKER_URL correctly references the broker's hostname and port. In Docker, use the service name (redis) as the hostname rather than localhost. Add depends_on to ensure the broker starts before the worker.
# config.py
CELERY_BROKER_URL = 'amqp://localhost:5672'
# config.py
import os
CELERY_BROKER_URL = os.environ.get('CELERY_BROKER_URL', 'redis://localhost:6379/0')
# docker-compose.yml
# services:
# redis:
# image: redis:7-alpine
# ports:
# - "6379:6379"
# worker:
# build: .
# command: celery -A app.celery worker --loglevel=info
# depends_on:
# - redis
# environment:
# - CELERY_BROKER_URL=redis://redis:6379/0
Testing the Fix
import pytest
from unittest.mock import patch
from app import create_app
from app.tasks import add_numbers
@pytest.fixture
def app():
app = create_app()
app.config['TESTING'] = True
app.config['CELERY_BROKER_URL'] = 'memory://'
app.config['CELERY_RESULT_BACKEND'] = 'cache+memory://'
return app
def test_celery_task_runs_eagerly(app):
app.config['CELERY_ALWAYS_EAGER'] = True
result = add_numbers.delay(2, 3)
assert result.get() == 5
def test_broker_url_is_configured(app):
assert app.config['CELERY_BROKER_URL'] is not None
assert len(app.config['CELERY_BROKER_URL']) > 0
Run your tests:
pytest tests/ -v
Pushing Through CI/CD
git checkout -b fix/celery-broker-connection,git add config.py docker-compose.yml,git commit -m "fix: configure correct broker URL and add Redis service",git push origin fix/celery-broker-connection
Your CI config should look something like this:
name: CI
on:
pull_request:
branches: [main]
jobs:
test:
runs-on: ubuntu-latest
services:
redis:
image: redis:7-alpine
ports:
- 6379:6379
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: '3.12'
cache: 'pip'
- run: pip install -r requirements.txt
- run: pytest tests/ -v --tb=short
env:
CELERY_BROKER_URL: redis://localhost:6379/0
The Full Manual Process: 18 Steps
Here's every step you just went through to fix this one bug:
- Notice the error alert or see it in your monitoring tool
- Open the error dashboard and read the stack trace
- Identify the file and line number from the stack trace
- Open your IDE and navigate to the file
- Read the surrounding code to understand context
- Reproduce the error locally
- Identify the root cause
- Write the fix
- Run the test suite locally
- Fix any failing tests
- Write new tests covering the edge case
- Run the full test suite again
- Create a new git branch
- Commit and push your changes
- Open a pull request
- Wait for code review
- Merge and deploy to production
- Monitor production to confirm the error is resolved
Total time: 30-60 minutes. For one bug.
Or Let bugstack Fix It in Under 2 minutes
Every step above? bugstack does it automatically.
Step 1: Install the SDK
pip install bugstack
Step 2: Initialize
import bugstack
bugstack.init(api_key=os.environ["BUGSTACK_API_KEY"])
Step 3: There is no step 3.
bugstack handles everything from here:
- Captures the stack trace and request context
- Pulls the relevant source files from your GitHub repo
- Analyzes the error and understands the code context
- Generates a minimal, verified fix
- Runs your existing test suite
- Pushes through your CI/CD pipeline
- Deploys to production (or opens a PR for review)
Time from error to fix deployed: Under 2 minutes.
Human involvement: zero.
Try bugstack Free →No credit card. 5-minute setup. Cancel anytime.
Deploying the Fix (Manual Path)
- Verify the broker service is running and accessible.
- Open a pull request with the broker configuration fix.
- Wait for CI checks with Redis service to pass.
- Have a teammate review and approve the PR.
- Merge to main and verify the Celery worker connects to the broker in staging.
Frequently Asked Questions
BugStack verifies broker connectivity, tests task submission and execution, and runs your full test suite against a live Redis instance before marking it safe.
BugStack never pushes directly to production. Every fix goes through a pull request with full CI checks, so your team can review it before merging.
Redis is simpler to set up and works well for most use cases. RabbitMQ offers more advanced routing and better delivery guarantees for high-reliability requirements.
Set CELERY_ALWAYS_EAGER=True in your test config. This makes tasks run synchronously in the current process without needing a broker connection.