Skip to content

Connection to broker lost. Trying to re-establish the connection... #3932

Closed

Description

@dharanpdarsana

We are using Django Rest Framework with MongoEngine, Redis, Celery and Kombu, and we are getting the following error in our logs:

`[2017-03-22 06:26:01,702: WARNING/MainProcess] consumer: Connection to broker lost. Trying to re-establish the connection...
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/celery/worker/consumer/consumer.py", line 318, in start
blueprint.start(self)
File "/usr/local/lib/python2.7/dist-packages/celery/bootsteps.py", line 119, in start
step.start(parent)
File "/usr/local/lib/python2.7/dist-packages/celery/worker/consumer/consumer.py", line 594, in start
c.loop(*c.loop_args())
File "/usr/local/lib/python2.7/dist-packages/celery/worker/loops.py", line 88, in asynloop
next(loop)
File "/usr/local/lib/python2.7/dist-packages/kombu/async/hub.py", line 345, in create_loop
cb(*cbargs)
File "/usr/local/lib/python2.7/dist-packages/kombu/transport/redis.py", line 1039, in on_readable
self.cycle.on_readable(fileno)
File "/usr/local/lib/python2.7/dist-packages/kombu/transport/redis.py", line 337, in on_readable
chan.handlerstype
File "/usr/local/lib/python2.7/dist-packages/kombu/transport/redis.py", line 667, in _receive
ret.append(self._receive_one(c))
File "/usr/local/lib/python2.7/dist-packages/kombu/transport/redis.py", line 678, in _receive_one
response = c.parse_response()
File "/usr/local/lib/python2.7/dist-packages/redis/client.py", line 2183, in parse_response
return self._execute(connection, connection.read_response)
File "/usr/local/lib/python2.7/dist-packages/redis/client.py", line 2176, in _execute
return command(*args)
File "/usr/local/lib/python2.7/dist-packages/redis/connection.py", line 577, in read_response
response = self._parser.read_response()
File "/usr/local/lib/python2.7/dist-packages/redis/connection.py", line 238, in read_response
response = self._buffer.readline()
File "/usr/local/lib/python2.7/dist-packages/redis/connection.py", line 168, in readline
self._read_from_socket()
File "/usr/local/lib/python2.7/dist-packages/redis/connection.py", line 143, in _read_from_socket
(e.args,))
ConnectionError: Error while reading from socket: ('Connection closed by server.',)

[2017-03-22 06:26:01,868: INFO/MainProcess] Connected to redis://:******************************/1

Versions used

python==2.7.12
redis==3.2.3
kombu==4.0.2
Django==1.10.3
celery==4.0.2
amqp==2.1.1
billiard==3.5.0.2
pytz==2016.7
Django==1.10.3o

We have HaProxy infront of Redis cluster and connections, everything else work without any issues. Could you please help us trouble shoot this error ?
Or guide on what to look for and where to look for , etc ?

We really appreciate your help,

Thanks,
Darsana

Activity

  1. jpatel3 commented on Mar 27, 2017

    @jpatel3

    @dharanpdarsana Do you have redis running? Here is how you can validate -

    $ redis-cli                                                                
    redis 127.0.0.1:6379> ping
    PONG
    redis 127.0.0.1:6379> set mykey somevalue
    OK
    redis 127.0.0.1:6379> get mykey
    "somevalue"
    
  2. dharanpdarsana commented on Mar 28, 2017

    @dharanpdarsana
    Author

    @jpatel3 redis is running and 90% of the connections are going through without any issue

  3. richmondwang commented on May 22, 2017

    @richmondwang

    I am also (suddenly) experiencing a broken pipe error.
    It was working perfectly fine before. I've forgotten what I have installed or did to trigger this.

    [2017-05-22 17:18:10,939: INFO/MainProcess] Connected to amqp://user123:**@192.168.99.100:5672//
    [2017-05-22 17:18:10,955: INFO/MainProcess] mingle: searching for neighbors
    [2017-05-22 17:18:10,965: WARNING/MainProcess] consumer: Connection to broker lost. Trying to re-establish the connection...
    Traceback (most recent call last):
      File "/Users/richmond/Projects/backend/env/lib/python3.6/site-packages/celery/worker/consumer/consumer.py", line 318, in start
        blueprint.start(self)
      File "/Users/richmond/Projects/backend/env/lib/python3.6/site-packages/celery/bootsteps.py", line 119, in start
        step.start(parent)
      File "/Users/richmond/Projects/backend/env/lib/python3.6/site-packages/celery/worker/consumer/mingle.py", line 38, in start
        self.sync(c)
      File "/Users/richmond/Projects/backend/env/lib/python3.6/site-packages/celery/worker/consumer/mingle.py", line 42, in sync
        replies = self.send_hello(c)
      File "/Users/richmond/Projects/backend/env/lib/python3.6/site-packages/celery/worker/consumer/mingle.py", line 55, in send_hello
        replies = inspect.hello(c.hostname, our_revoked._data) or {}
      File "/Users/richmond/Projects/backend/env/lib/python3.6/site-packages/celery/app/control.py", line 129, in hello
        return self._request('hello', from_node=from_node, revoked=revoked)
      File "/Users/richmond/Projects/backend/env/lib/python3.6/site-packages/celery/app/control.py", line 81, in _request
        timeout=self.timeout, reply=True,
      File "/Users/richmond/Projects/backend/env/lib/python3.6/site-packages/celery/app/control.py", line 436, in broadcast
        limit, callback, channel=channel,
      File "/Users/richmond/Projects/backend/env/lib/python3.6/site-packages/kombu/pidbox.py", line 315, in _broadcast
        serializer=serializer)
      File "/Users/richmond/Projects/backend/env/lib/python3.6/site-packages/kombu/pidbox.py", line 290, in _publish
        serializer=serializer,
      File "/Users/richmond/Projects/backend/env/lib/python3.6/site-packages/kombu/messaging.py", line 181, in publish
        exchange_name, declare,
      File "/Users/richmond/Projects/backend/env/lib/python3.6/site-packages/kombu/messaging.py", line 194, in _publish
        [maybe_declare(entity) for entity in declare]
      File "/Users/richmond/Projects/backend/env/lib/python3.6/site-packages/kombu/messaging.py", line 194, in <listcomp>
        [maybe_declare(entity) for entity in declare]
      File "/Users/richmond/Projects/backend/env/lib/python3.6/site-packages/kombu/messaging.py", line 102, in maybe_declare
        return maybe_declare(entity, self.channel, retry, **retry_policy)
      File "/Users/richmond/Projects/backend/env/lib/python3.6/site-packages/kombu/common.py", line 125, in maybe_declare
        return _maybe_declare(entity, declared, ident, channel, orig)
      File "/Users/richmond/Projects/backend/env/lib/python3.6/site-packages/kombu/common.py", line 131, in _maybe_declare
        entity.declare(channel=channel)
      File "/Users/richmond/Projects/backend/env/lib/python3.6/site-packages/kombu/entity.py", line 185, in declare
        nowait=nowait, passive=passive,
      File "/Users/richmond/Projects/backend/env/lib/python3.6/site-packages/amqp/channel.py", line 630, in exchange_declare
        wait=None if nowait else spec.Exchange.DeclareOk,
      File "/Users/richmond/Projects/backend/env/lib/python3.6/site-packages/amqp/abstract_channel.py", line 64, in send_method
        conn.frame_writer(1, self.channel_id, sig, args, content)
      File "/Users/richmond/Projects/backend/env/lib/python3.6/site-packages/amqp/method_framing.py", line 174, in write_frame
        write(view[:offset])
      File "/Users/richmond/Projects/backend/env/lib/python3.6/site-packages/amqp/transport.py", line 269, in write
        self._write(s)
      File "/Users/richmond/Projects/backend/env/lib/python3.6/site-packages/eventlet/greenio/base.py", line 397, in sendall
        tail = self.send(data, flags)
      File "/Users/richmond/Projects/backend/env/lib/python3.6/site-packages/eventlet/greenio/base.py", line 391, in send
        return self._send_loop(self.fd.send, data, flags)
      File "/Users/richmond/Projects/backend/env/lib/python3.6/site-packages/eventlet/greenio/base.py", line 378, in _send_loop
        return send_method(data, *args)
    BrokenPipeError: [Errno 32] Broken pipe
    [2017-05-22 17:18:11,032: INFO/MainProcess] Connected to amqp://user123:**@192.168.99.100:5672//
    [2017-05-22 17:18:11,046: INFO/MainProcess] mingle: searching for neighbors
    [2017-05-22 17:18:12,075: INFO/MainProcess] mingle: all alone
    [2017-05-22 17:18:12,104: INFO/MainProcess] [email protected] ready.
    

    $ pip freeze

    alabaster==0.7.10
    alembic==0.9.1
    amqp==2.1.4
    aniso8601==1.2.0
    appdirs==1.4.3
    argh==0.26.2
    Babel==2.4.0
    billiard==3.5.0.2
    blinker==1.4
    bumpversion==0.5.3
    cached-property==1.3.0
    celery==4.0.2
    cffi==1.10.0
    click==6.7
    colorama==0.3.8
    coverage==4.1
    cryptography==1.7
    docker==2.2.1
    docker-compose==1.11.2
    docker-pycreds==0.2.1
    dockerpty==0.4.1
    docopt==0.6.2
    docutils==0.13.1
    ecdsa==0.13
    enum-compat==0.0.2
    eventlet==0.21.0
    factory-boy==2.8.1
    Faker==0.7.11
    flake8==2.6.0
    Flask==0.12
    Flask-Babel==0.11.2
    Flask-Cors==3.0.2
    Flask-GraphQL==1.4.0
    Flask-Mail==0.9.1
    Flask-Migrate==2.0.3
    Flask-RESTful==0.3.5
    Flask-Script==2.0.5
    Flask-SocketIO==2.8.6
    Flask-SQLAlchemy==2.1
    future==0.16.0
    graphene==1.1.3
    graphene-sqlalchemy==1.1.1
    graphql-core==1.1
    graphql-relay==0.4.5
    greenlet==0.4.12
    gunicorn==19.7.1
    idna==2.5
    imagesize==0.7.1
    inflection==0.3.1
    iso8601==0.1.11
    itsdangerous==0.24
    Jinja2==2.9.6
    jsonschema==2.6.0
    kombu==4.0.2
    Mako==1.0.6
    MarkupSafe==1.0
    mccabe==0.5.3
    mod-wsgi==4.5.15
    ndg-httpsclient==0.4.2
    packaging==16.8
    pathtools==0.1.2
    pluggy==0.3.1
    promise==2.0
    py==1.4.33
    pyasn1==0.2.3
    pycodestyle==2.0.0
    pycparser==2.17
    pycrypto==2.6.1
    pyflakes==1.2.3
    Pygments==2.2.0
    PyMySQL==0.7.10
    pyOpenSSL==17.0.0
    pyparsing==2.2.0
    python-dateutil==2.6.0
    python-editor==1.0.3
    python-engineio==1.4.0
    python-jose==1.3.2
    python-socketio==1.7.4
    pytz==2017.2
    PyYAML==3.11
    raven==5.32.0
    requests==2.11.1
    singledispatch==3.4.0.3
    six==1.10.0
    snowballstemmer==1.2.1
    Sphinx==1.4.8
    SQLAlchemy==1.1.5
    texttable==0.8.8
    tox==2.3.1
    typing==3.6.1
    vine==1.1.3
    virtualenv==15.1.0
    voluptuous==0.9.3
    watchdog==0.8.3
    websocket-client==0.40.0
    Werkzeug==0.12.1
    
  4. Kurara commented on Jun 6, 2017

    @Kurara

    I have the same problem and I'm using the same versions and I can't find a solution :(

  5. pengye91 commented on Jun 19, 2017

    @pengye91

    I am using kombu and running into the same situation, I'm not sure but I think maybe it's an issue working with haproxy.

    When connected to haproxy which routes connections to several rabbitmq nodes, both producers and consumers have the same "connection to broker lost" issue, but when directly connected to a rabbitmq node, the issue never occurs.

  6. mistercrunch commented on Jun 21, 2017

    @mistercrunch
    Contributor

    Also seeing this, and also behind haproxy:

    kombu==4.0.2
    celery==4.0.2
    redis==2.10.5
    
  7. added a commit that references this issue on Jun 21, 2017
  8. Surgo commented on Jun 28, 2017

    @Surgo

    Maybe it's happened SQS brokers too:

    boto==2.47.0
    celery==4.0.2
    kombu==4.0.2
    
  9. miljevic commented on Aug 10, 2017

    @miljevic

    The same problem here, Celery with RabbitMQ and eventlet.

    amqp==2.2.1
    billiard==3.5.0.3
    celery==4.1.0
    Django==1.8.5
    eventlet==0.21.0
    kombu==4.1.0
    pytz==2017.2
    vine==1.1.4

    [2017-08-10 06:35:39,629: WARNING/MainProcess] consumer: Connection to broker lost. Trying to re-establish the connection...
    Traceback (most recent call last):
      File "/usr/lib/python2.7/site-packages/celery/worker/consumer/consumer.py", line 320, in start
        blueprint.start(self)
      File "/usr/lib/python2.7/site-packages/celery/bootsteps.py", line 119, in start
        step.start(parent)
      File "/usr/lib/python2.7/site-packages/celery/worker/consumer/consumer.py", line 596, in start
        c.loop(*c.loop_args())
      File "/usr/lib/python2.7/site-packages/celery/worker/loops.py", line 118, in synloop
        connection.drain_events(timeout=2.0)
      File "/usr/lib/python2.7/site-packages/kombu/connection.py", line 301, in drain_events
        return self.transport.drain_events(self.connection, **kwargs)
      File "/usr/lib/python2.7/site-packages/kombu/transport/pyamqp.py", line 103, in drain_events
        return connection.drain_events(**kwargs)
      File "/usr/lib/python2.7/site-packages/amqp/connection.py", line 485, in drain_events
        while not self.blocking_read(timeout):
      File "/usr/lib/python2.7/site-packages/amqp/connection.py", line 490, in blocking_read
        frame = self.transport.read_frame()
      File "/usr/lib/python2.7/site-packages/amqp/transport.py", line 240, in read_frame
        frame_header = read(7, True)
      File "/usr/lib/python2.7/site-packages/amqp/transport.py", line 415, in _read
        s = recv(n - len(rbuf))
      File "/usr/lib/python2.7/site-packages/eventlet/greenio/base.py", line 360, in recv
        return self._recv_loop(self.fd.recv, b'', bufsize, flags)
      File "/usr/lib/python2.7/site-packages/eventlet/greenio/base.py", line 354, in _recv_loop
        self._read_trampoline()
      File "/usr/lib/python2.7/site-packages/eventlet/greenio/base.py", line 325, in _read_trampoline
        timeout_exc=socket_timeout('timed out'))
      File "/usr/lib/python2.7/site-packages/eventlet/greenio/base.py", line 207, in _trampoline
        mark_as_closed=self._mark_as_closed)
      File "/usr/lib/python2.7/site-packages/eventlet/hubs/__init__.py", line 163, in trampoline
        return hub.switch()
      File "/usr/lib/python2.7/site-packages/eventlet/hubs/hub.py", line 295, in switch
        return self.greenlet.switch()
      File "/usr/lib/python2.7/site-packages/eventlet/hubs/hub.py", line 347, in run
        self.wait(sleep_time)
      File "/usr/lib/python2.7/site-packages/eventlet/hubs/poll.py", line 84, in wait
        presult = self.do_poll(seconds)
      File "/usr/lib/python2.7/site-packages/eventlet/hubs/epolls.py", line 61, in do_poll
        return self.poll.poll(seconds)
      File "/usr/lib/python2.7/site-packages/celery/apps/worker.py", line 333, in restart_worker_sig_handler
        safe_say('Restarting celery worker ({0})'.format(' '.join(sys.argv)))
      File "/usr/lib/python2.7/site-packages/celery/apps/worker.py", line 89, in safe_say
        print('\n{0}'.format(msg), file=sys.__stderr__)
    IOError: [Errno 32] Broken pipe
    [2017-08-10 06:35:39,820: INFO/MainProcess] Connected to amqp://guest:**@127.0.0.1:5672//
    [2017-08-10 06:35:39,940: INFO/MainProcess] mingle: searching for neighbors
    [2017-08-10 06:35:40,975: INFO/MainProcess] mingle: all alone
    [2017-08-10 06:35:41,115: INFO/MainProcess] pidbox: Connected to amqp://guest:**@127.0.0.1:5672//.
    
  10. chrisspen commented on Aug 10, 2017

    @chrisspen

    I'm having this same problem with RabbitMQ running on localhost. It's odd that local connections would time-out or lose connection. I do have some pretty intensive tasks, and my CPU is at 100%, but my memory usage is low. Would that cause dropped connections?

    celery==4.0.2
    kombu==4.0.2
    Django==1.8.6
    RabbitMQ==3.2.4
    
  11. Shuklapreetika commented on Oct 12, 2017

    @Shuklapreetika

    @chrisspen, were you able to resolve the issue? I am having a similar scenario, where while performing an CPU intensive task, the connection to redis gets dropped: the following is one of the many manifestation of exceptions which I receive:

    [2017-10-11 13:34:57,244: WARNING/MainProcess] consumer: Connection to broker lost. Trying to re-establish the connection...
    Traceback (most recent call last):
      File "/usr/local/python/lib/python2.7/site-packages/celery/worker/consumer/consumer.py", line 318, in start
        blueprint.start(self)
      File "/usr/local/python/lib/python2.7/site-packages/celery/bootsteps.py", line 119, in start
        step.start(parent)
      File "/usr/local/python/lib/python2.7/site-packages/celery/worker/consumer/consumer.py", line 594, in start
        c.loop(*c.loop_args())
      File "/usr/local/python/lib/python2.7/site-packages/celery/worker/loops.py", line 88, in asynloop
        next(loop)
      File "/usr/local/python/lib/python2.7/site-packages/kombu/async/hub.py", line 345, in create_loop
        cb(*cbargs)
      File "/usr/local/python/lib/python2.7/site-packages/kombu/transport/redis.py", line 1039, in on_readable
        self.cycle.on_readable(fileno)
      File "/usr/local/python/lib/python2.7/site-packages/kombu/transport/redis.py", line 337, in on_readable
        chan.handlers[type]()
      File "/usr/local/python/lib/python2.7/site-packages/kombu/transport/redis.py", line 714, in _brpop_read
        **options)
      File "/usr/local/python/lib/python2.7/site-packages/redis/client.py", line 585, in parse_response
        response = connection.read_response()
      File "/usr/local/python/lib/python2.7/site-packages/redis/connection.py", line 577, in read_response
        response = self._parser.read_response()
      File "/usr/local/python/lib/python2.7/site-packages/redis/connection.py", line 238, in read_response
        response = self._buffer.readline()
      File "/usr/local/python/lib/python2.7/site-packages/redis/connection.py", line 168, in readline
        self._read_from_socket()
      File "/usr/local/python/lib/python2.7/site-packages/redis/connection.py", line 143, in _read_from_socket
        (e.args,))
    ConnectionError: Error while reading from socket: ('Connection closed by server.',)
    
    
  12. imaia commented on Oct 31, 2017

    @imaia

    I also had a " Connection to broker lost" using rabbit + celery under high CPU usage; not sure is related.

  13. added this to the milestone on Dec 22, 2017
  14. auvipy commented on Jan 17, 2018

    @auvipy
    Member

    this might be an issue with HAProxy

  15. 42 remaining items

  16. atodniAr commented on Jun 9, 2021

    @atodniAr

    I had an application using celery with redis backend (haproxy) over k8s.

    Facts:

    • My haproxy deployment has a 37s timeout setting.
    • Celery prompt similar error message every 37s, then reconnect to redis.

    Thus, I think celery consumer lack a proper heartbeat with haproxy, which caused haproxy to break the connection on idle.

  17. thedrow commented on Aug 17, 2021

    @thedrow
    Contributor

    I had a similar issue using the HAproxy provided by redis-ha Helm chart. Turns out its default timeout client was 30s, and Redis' default TCP keepalive interval is "about 5 minutes". Raising the timeout client and timeout server both to 330s, the problem seems to have been fixed.

    Didn't this help?
    We just need to document this if that works.

  18. modified the milestones: 5.2, 5.2.x on Oct 30, 2021
  19. laczar commented on Nov 29, 2021

    @laczar

    is this solution applied or being planned on celery?

  20. hamid914 commented on Dec 23, 2021

    @hamid914

    I had this error with amqp==5.0.8 and it is fixed by upgrading to amqp==5.0.9

  21. cathhuo commented on Oct 14, 2022

    @cathhuo

    i have the same issue in 5.2.7

  22. vlad-stack commented on Nov 26, 2022

    @vlad-stack

    I have the same issue in 5.2.7, and 5.3.0b1

  23. DAAworld commented on Oct 25, 2023

    @DAAworld

    kombu==5.3.2
    celery==5.3.4
    redis==5.0.0

    my config
    BROKER_CONNECTION_MAX_RETRIES = 0
    CELERY_REDIS_RETRY_ON_TIMEOUT = True
    CELERY_REDIS_SOCKET_CONNECT_TIMEOUT = 5
    REDIS_BACKEND_HEALTH_CHECK_INTERVAL = 5

    I closed my internet about 1 minutes, and celery beat was retried successed, but worker raise error :
    [2023-10-25 17:22:12,954: WARNING/MainProcess] consumer: Connection to broker lost. Trying to re-establish the connection...
    Traceback (most recent call last):
    File "/home/ww/miniconda3/envs/sss/lib/python3.10/site-packages/redis/connection.py", line 435, in send_packed_command
    self._sock.sendall(item)
    ConnectionResetError: [Errno 104] Connection reset by peer

    During handling of the above exception, another exception occurred:

    Traceback (most recent call last):
    File "/home/ww/miniconda3/envs/sss/lib/python3.10/site-packages/celery/worker/consumer/consumer.py", line 340, in start
    blueprint.start(self)
    File "/home/ww/miniconda3/envs/sss/lib/python3.10/site-packages/celery/bootsteps.py", line 116, in start
    step.start(parent)
    File "/home/ww/miniconda3/envs/sss/lib/python3.10/site-packages/celery/worker/consumer/consumer.py", line 742, in start
    c.loop(*c.loop_args())
    File "/home/ww/miniconda3/envs/sss/lib/python3.10/site-packages/celery/worker/loops.py", line 97, in asynloop
    next(loop)
    File "/home/ww/miniconda3/envs/sss/lib/python3.10/site-packages/kombu/asynchronous/hub.py", line 373, in create_loop
    cb(*cbargs)
    File "/home/ww/miniconda3/envs/sss/lib/python3.10/site-packages/kombu/transport/redis.py", line 1341, in on_readable
    self.cycle.on_readable(fileno)
    File "/home/ww/miniconda3/envs/sss/lib/python3.10/site-packages/kombu/transport/redis.py", line 566, in on_readable
    chan.handlerstype
    File "/home/ww/miniconda3/envs/sss/lib/python3.10/site-packages/kombu/transport/redis.py", line 910, in _receive
    ret.append(self._receive_one(c))
    File "/home/ww/miniconda3/envs/sss/lib/python3.10/site-packages/kombu/transport/redis.py", line 920, in _receive_one
    response = c.parse_response()
    File "/home/ww/miniconda3/envs/sss/lib/python3.10/site-packages/redis/client.py", line 786, in parse_response
    self.check_health()
    File "/home/ww/miniconda3/envs/sss/lib/python3.10/site-packages/redis/client.py", line 824, in check_health
    conn.send_command("PING", self.HEALTH_CHECK_MESSAGE, check_health=False)
    File "/home/ww/miniconda3/envs/sss/lib/python3.10/site-packages/redis/connection.py", line 457, in send_command
    self.send_packed_command(
    File "/home/ww/miniconda3/envs/sss/lib/python3.10/site-packages/redis/connection.py", line 446, in send_packed_command
    raise ConnectionError(f"Error {errno} while writing to socket. {errmsg}.")
    redis.exceptions.ConnectionError: Error 104 while writing to socket. Connection reset by peer.

  24. added a commit that references this issue on May 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions