Issue 1:
Checklist

I have included the output of celery -A proj report in the issue.

software -> celery:4.0.2 (latentcall) kombu:4.0.2 py:2.7.10
billiard:3.5.0.3 librabbitmq:1.6.1
platform -> system:Linux arch:64bit, ELF imp:CPython
loader -> celery.loaders.app.AppLoader
settings -> transport:librabbitmq results:mongodb://cyberivy:**@127.0.0.1/

I have verified that the issue exists against the master branch of Celery.

Steps to reproduce

It rarely happened. I can't reproduce

Expected behavior

Actual behavior

Traceback (most recent call last):
File "/opt/python2.7.10-customized/lib/python2.7/site-packages/celery/app/trace.py", line 362, in trace_task
request=task_request,
File "/opt/python2.7.10-customized/lib/python2.7/site-packages/celery/backends/base.py", line 309, in store_result
request=request, **kwargs)
File "/opt/python2.7.10-customized/lib/python2.7/site-packages/celery/backends/mongodb.py", line 175, in _store_result
self.collection.save(meta)
File "/opt/python2.7.10-customized/lib/python2.7/site-packages/pymongo/collection.py", line 2185, in save
check_keys, False, manipulate, write_concern)
File "/opt/python2.7.10-customized/lib/python2.7/site-packages/pymongo/collection.py", line 710, in _update
_check_write_command_response([(0, result)])
File "/opt/python2.7.10-customized/lib/python2.7/site-packages/pymongo/helpers.py", line 300, in _check_write_command_response
raise DuplicateKeyError(error.get("errmsg"), 11000, error)
DuplicateKeyError: E11000 duplicate key error collection: celery.celery_taskmeta index: _id_ dup key: { : "671ea5ba-9eff-447e-b6c5-329477da3cb1" }

Thanks

Issue 2:
Checklist

I have verified that the issue exists against the master branch of Celery.

This has already been asked to the discussions forum first.

I have read the relevant section in the

contribution guide

on reporting bugs.

I have checked the issues list

for similar or identical bug reports.

I have checked the pull requests list

for existing proposed fixes.

I have checked the commit log

to find out if the bug was already fixed in the master branch.

I have included all related issues and possible duplicate issues

in this issue (If there are none, check this box anyway).

Mandatory Debugging Information

I have included the output of celery -A proj report in the issue.

I have verified that the issue exists against the master branch of Celery.

I have included the contents of pip freeze in the issue.

I have included all the versions of all the external dependencies required

to reproduce this bug.

Optional Debugging Information

I have tried reproducing the issue on more than one Python version

and/or implementation.

I have tried reproducing the issue on more than one message broker and/or

result backend.

I have tried reproducing the issue on more than one version of the message

broker and/or result backend.

I have tried reproducing the issue on more than one operating system.

I have tried reproducing the issue on more than one workers pool.

I have tried reproducing the issue with autoscaling, retries,

ETA/Countdown & rate limits disabled.

I have tried reproducing the issue after downgrading

and/or upgrading Celery and its dependencies.

Related Issues and Possible Duplicates

Related Issues

None

Possible Duplicates

None

Environment & Settings

Celery version: 5.2.3 (dawn-chorus)

celery report Output:

software -> celery:5.2.3 (dawn-chorus) kombu:5.2.3 py:3.9.10
billiard:3.6.4.0 py-amqp:5.0.9
platform -> system:Darwin arch:64bit
kernel version:20.6.0 imp:CPython
loader -> celery.loaders.app.AppLoader
settings -> transport:amqp results:django-db

Steps to Reproduce

Required Dependencies

Minimal Python Version: N/A or Unknown

Minimal Celery Version: N/A or Unknown

Minimal Kombu Version: N/A or Unknown

Minimal Broker Version: N/A or Unknown

Minimal Result Backend Version: N/A or Unknown

Minimal OS and/or Kernel Version: N/A or Unknown

Minimal Broker Client Version: N/A or Unknown

Minimal Result Backend Client Version: N/A or Unknown

Python Packages

pip freeze Output:

Other Dependencies

N/A

Minimally Reproducible Test Case

Expected Behavior

Expected worker to exit with a non-zero code

Actual Behavior

Ran celery with the following settings

CELERY_BROKER_CONNECTION_RETRY = False
CELERY_BROKER_CONNECTION_TIMEOUT = 2 # seconds

because for kubernetes related reasons it's better for us if the workers just die immediately if the config is wrong.

The worker does exit fast enough, but the exit code is 0 despite the following backtrace

[2022-03-18 17:33:30,670: CRITICAL/MainProcess] Unrecoverable error: ConnectionRefusedError(61, 'Connection refused')
Traceback (most recent call last):
File "/Users/tomparker-shemilt/.virtualenvs/case-cards/lib/python3.9/site-packages/celery/worker/worker.py", line 203, in start
self.blueprint.start(self)
File "/Users/tomparker-shemilt/.virtualenvs/case-cards/lib/python3.9/site-packages/celery/bootsteps.py", line 116, in start
step.start(parent)
File "/Users/tomparker-shemilt/.virtualenvs/case-cards/lib/python3.9/site-packages/celery/bootsteps.py", line 365, in start
return self.obj.start()
File "/Users/tomparker-shemilt/.virtualenvs/case-cards/lib/python3.9/site-packages/celery/worker/consumer/consumer.py", line 326, in start
blueprint.start(self)
File "/Users/tomparker-shemilt/.virtualenvs/case-cards/lib/python3.9/site-packages/celery/bootsteps.py", line 116, in start
step.start(parent)
File "/Users/tomparker-shemilt/.virtualenvs/case-cards/lib/python3.9/site-packages/celery/worker/consumer/connection.py", line 21, in start
c.connection = c.connect()
File "/Users/tomparker-shemilt/.virtualenvs/case-cards/lib/python3.9/site-packages/celery/worker/consumer/consumer.py", line 422, in connect
conn = self.connection_for_read(heartbeat=self.amqheartbeat)
File "/Users/tomparker-shemilt/.virtualenvs/case-cards/lib/python3.9/site-packages/celery/worker/consumer/consumer.py", line 428, in connection_for_read
return self.ensure_connected(
File "/Users/tomparker-shemilt/.virtualenvs/case-cards/lib/python3.9/site-packages/celery/worker/consumer/consumer.py", line 451, in ensure_connected
conn.connect()
File "/Users/tomparker-shemilt/.virtualenvs/case-cards/lib/python3.9/site-packages/kombu/connection.py", line 275, in connect
return self._ensure_connection(
File "/Users/tomparker-shemilt/.virtualenvs/case-cards/lib/python3.9/site-packages/kombu/connection.py", line 434, in _ensure_connection
return retry_over_time(
File "/Users/tomparker-shemilt/.virtualenvs/case-cards/lib/python3.9/site-packages/kombu/utils/functional.py", line 312, in retry_over_time
return fun(*args, **kwargs)
File "/Users/tomparker-shemilt/.virtualenvs/case-cards/lib/python3.9/site-packages/kombu/connection.py", line 878, in _connection_factory
self._connection = self._establish_connection()
File "/Users/tomparker-shemilt/.virtualenvs/case-cards/lib/python3.9/site-packages/kombu/connection.py", line 813, in _establish_connection
conn = self.transport.establish_connection()
File "/Users/tomparker-shemilt/.virtualenvs/case-cards/lib/python3.9/site-packages/kombu/transport/pyamqp.py", line 201, in establish_connection
conn.connect()
File "/Users/tomparker-shemilt/.virtualenvs/case-cards/lib/python3.9/site-packages/amqp/connection.py", line 323, in connect
self.transport.connect()
File "/Users/tomparker-shemilt/.virtualenvs/case-cards/lib/python3.9/site-packages/amqp/transport.py", line 113, in connect
self._connect(self.host, self.port, self.connect_timeout)
File "/Users/tomparker-shemilt/.virtualenvs/case-cards/lib/python3.9/site-packages/amqp/transport.py", line 197, in _connect
self.sock.connect(sa)
ConnectionRefusedError: [Errno 61] Connection refused

I tried messing around with locally editing https://github.com/celery/celery/blob/master/celery/bin/worker.py and noted the

worker.start()
return worker.exitcode
except SecurityError as e:
ctx.obj.error(e.args[0])
ctx.exit(1)

block at the end. If I replaced return worker.exitcode with ctx.exit(worker.exitcode) it seems to work as I was expecting i.e. exit code of 1