Issue 1:
Checklist

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

(if you are not able to do this, then at least specify the Celery

version affected).

software -> celery:4.2.1 (windowlicker) kombu:4.2.1 py:3.6.6
billiard:3.5.0.4 py-amqp:2.3.2
platform -> system:Linux arch:64bit imp:CPython
loader -> celery.loaders.app.AppLoader
settings -> transport:amqp results:redis://XXXXXXXSNIPXXXXXXX

broker_url: 'amqp://XXXXXXXSNIPXXXXXXX'
task_ignore_result: False
worker_hijack_root_logger: False
result_backend: 'redis://XXXXXXXSNIPXXXXXXX'
task_acks_late: True
task_track_started: True
worker_prefetch_multiplier: 1
worker_pool_restarts: True
task_create_missing_queues: True

Worker info:

-------------- celery@app v4.2.1 (windowlicker)
---- **** -----
--- * *** * -- Linux-4.9.93-linuxkit-aufs-x86_64-with-debian-9.5 2018-08-24 15:19:01
-- * - **** ---
- ** ---------- [config]
- ** ---------- .> app: XXXXXXXSNIPXXXXXXX
- ** ---------- .> transport: amqp://XXXXXXXSNIPXXXXXXX
- ** ---------- .> results: redis://XXXXXXXSNIPXXXXXXX
- *** --- * --- .> concurrency: 2 (prefork)
-- ******* ---- .> task events: OFF (enable -E to monitor tasks in this worker)
--- ***** -----
-------------- [queues]
.> celery exchange=celery(direct) key=celery

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

I have also verified that this issue exists as described on 4.1.1

Steps to reproduce

Consider the following definition of a simple task, that waits for delay time, and then logs:

import logging
from time import sleep
import celery

log = logging.getLogger(__name__)

@celery.shared_task(bind=True)
def test_task(self, *args, **kwargs):
delay = kwargs.get('delay', 0)
arg = args[-1]
sleep(delay)
log.info('Test Task called with %s', arg)

Defer a task chain as follows, and then perform a warm shutdown of the workers

sigs = [test_task.s('A', delay=30), test_task.s('B'), test_task.s('C')]
celery.chain(sigs).apply_async()
sleep(20)
celery.current_app.control.shutdown() # this should occur during the execution of task A

Finally, start the workers back up.

Expected behavior

The worker gets a shutdown while running task A, and shuts down after completion of task A.

Then, upon starting back up, the worker executes task B and finally task C.

Actual behavior

The worker gets a shutdown while running task A, and shuts down after completion of task A.

Upon starting back up, however, the worker executes both task B and task A again. Furthermore, upon completion of task A, it then executes task B a second time, and so on with task C. Essentially, from the point of restarting the worker, the entire task chain is duplicated. Interestingly, the duplicate tasks throughout the entire chain appear to have duplicate ids (eg. task B runs with the same id both the first and second times). Following are the worker logs demonstrating this behavior:

The initial worker run

[15:29:02] DEBUG 7d841ca8-c2a6-40e1-8022-a4e370e79861 foobar_project.celery[106]:150 task_prerun: {'task_id': '7d841ca8-c2a6-40e1-8022-a4e370e79861', 'task': <@task: foobar.tasks.test.test_task of foobar_project at 0x7f66b74036d8>, 'args': ['A'], 'kwargs': {'delay': 30}}
[15:29:22] WARNING celery.worker.control[100]:519 Got shutdown from remote
[15:29:22] INFO foobar_project.celery[105]:150 worker_process_shutdown: {'pid': 105, 'exitcode': 0}
[15:29:32] INFO 7d841ca8-c2a6-40e1-8022-a4e370e79861 foobar.tasks.test[106]:13 Test Task called with A
[15:29:32] DEBUG 7d841ca8-c2a6-40e1-8022-a4e370e79861 foobar_project.celery[106]:150 task_postrun: {'task_id': '7d841ca8-c2a6-40e1-8022-a4e370e79861', 'task': <@task: foobar.tasks.test.test_task of foobar_project at 0x7f66b74036d8>, 'args': ['A'], 'kwargs': {'delay': 30}, 'retval': None, 'state': 'SUCCESS'}
[15:29:32] INFO 7d841ca8-c2a6-40e1-8022-a4e370e79861 foobar_project.celery[106]:150 worker_process_shutdown: {'pid': 106, 'exitcode': 0}
[15:29:33] INFO foobar_project.celery[100]:152 worker_shutdown

The worker logs after the worker is restarted

[15:30:07] DEBUG 2922b5cf-6f52-415d-b2b9-4713b92388ab foobar_project.celery[122]:150 task_prerun: {'task_id': '2922b5cf-6f52-415d-b2b9-4713b92388ab', 'task': <@task: foobar.tasks.test.test_task of foobar_project at 0x7f31212fadd8>, 'args': [None, 'B'], 'kwargs': {}}
[15:30:07] DEBUG 7d841ca8-c2a6-40e1-8022-a4e370e79861 foobar_project.celery[123]:150 task_prerun: {'task_id': '7d841ca8-c2a6-40e1-8022-a4e370e79861', 'task': <@task: foobar.tasks.test.test_task of foobar_project at 0x7f31212fadd8>, 'args': ['A'], 'kwargs': {'delay': 30}}
[15:30:07] INFO 2922b5cf-6f52-415d-b2b9-4713b92388ab foobar.tasks.test[122]:13 Test Task called with B
[15:30:07] DEBUG 2922b5cf-6f52-415d-b2b9-4713b92388ab foobar_project.celery[122]:150 task_postrun: {'task_id': '2922b5cf-6f52-415d-b2b9-4713b92388ab', 'task': <@task: foobar.tasks.test.test_task of foobar_project at 0x7f31212fadd8>, 'args': [None, 'B'], 'kwargs': {}, 'retval': None, 'state': 'SUCCESS'}
[15:30:07] DEBUG e5a39e30-0345-43f0-857f-ad05e2111abc foobar_project.celery[122]:150 task_prerun: {'task_id': 'e5a39e30-0345-43f0-857f-ad05e2111abc', 'task': <@task: foobar.tasks.test.test_task of foobar_project at 0x7f31212fadd8>, 'args': [None, 'C'], 'kwargs': {}}
[15:30:07] INFO e5a39e30-0345-43f0-857f-ad05e2111abc foobar.tasks.test[122]:13 Test Task called with C
[15:30:07] DEBUG e5a39e30-0345-43f0-857f-ad05e2111abc foobar_project.celery[122]:150 task_postrun: {'task_id': 'e5a39e30-0345-43f0-857f-ad05e2111abc', 'task': <@task: foobar.tasks.test.test_task of foobar_project at 0x7f31212fadd8>, 'args': [None, 'C'], 'kwargs': {}, 'retval': None, 'state': 'SUCCESS'}
[15:30:37] INFO 7d841ca8-c2a6-40e1-8022-a4e370e79861 foobar.tasks.test[123]:13 Test Task called with A
[15:30:37] DEBUG 7d841ca8-c2a6-40e1-8022-a4e370e79861 foobar_project.celery[123]:150 task_postrun: {'task_id': '7d841ca8-c2a6-40e1-8022-a4e370e79861', 'task': <@task: foobar.tasks.test.test_task of foobar_project at 0x7f31212fadd8>, 'args': ['A'], 'kwargs': {'delay': 30}, 'retval': None, 'state': 'SUCCESS'}
[15:30:37] DEBUG 2922b5cf-6f52-415d-b2b9-4713b92388ab foobar_project.celery[123]:150 task_prerun: {'task_id': '2922b5cf-6f52-415d-b2b9-4713b92388ab', 'task': <@task: foobar.tasks.test.test_task of foobar_project at 0x7f31212fadd8>, 'args': [None, 'B'], 'kwargs': {}}
[15:30:37] INFO 2922b5cf-6f52-415d-b2b9-4713b92388ab foobar.tasks.test[123]:13 Test Task called with B
[15:30:37] DEBUG 2922b5cf-6f52-415d-b2b9-4713b92388ab foobar_project.celery[123]:150 task_postrun: {'task_id': '2922b5cf-6f52-415d-b2b9-4713b92388ab', 'task': <@task: foobar.tasks.test.test_task of foobar_project at 0x7f31212fadd8>, 'args': [None, 'B'], 'kwargs': {}, 'retval': None, 'state': 'SUCCESS'}
[15:30:37] DEBUG e5a39e30-0345-43f0-857f-ad05e2111abc foobar_project.celery[123]:150 task_prerun: {'task_id': 'e5a39e30-0345-43f0-857f-ad05e2111abc', 'task': <@task: foobar.tasks.test.test_task of foobar_project at 0x7f31212fadd8>, 'args': [None, 'C'], 'kwargs': {}}
[15:30:37] INFO e5a39e30-0345-43f0-857f-ad05e2111abc foobar.tasks.test[123]:13 Test Task called with C
[15:30:37] DEBUG e5a39e30-0345-43f0-857f-ad05e2111abc foobar_project.celery[123]:150 task_postrun: {'task_id': 'e5a39e30-0345-43f0-857f-ad05e2111abc', 'task': <@task: foobar.tasks.test.test_task of foobar_project at 0x7f31212fadd8>, 'args': [None, 'C'], 'kwargs': {}, 'retval': None, 'state': 'SUCCESS'}

As one might imagine, this behavior makes running chains of tasks that require a particular ordering difficult.

Other notes

I originally observed this behavior with a task that was retrying (via self.retry), so the behavior does not appear to be related to the task state being SUCCESS (or at least is also present with state=RETRY). I provided this non-retrying case as it is a simpler example.

This issue is very likely related to both/either #3796 and #3802 ; I have submitted this issue separately to highlight the behavior of the task chains with duplicate ids, as well as the behavior with RETRY rather than SUCCESS. Only workaround I've found thus far is to use a lock on task_id to detect the duplication, and then kill the "second" task (whichever failed to get the lock) by raising an Exception. This still has the unfortunate drawback of calling into any link_error handlers on the chain.

Issue 2:
Checklist

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

This has already been asked to the discussion group 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.

(if you are not able to do this, then at least specify the Celery

version affected).

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

#6134

#4116

Environment & Settings

Celery version:

today master: 39209f2

celery report Output:

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

celery/celery/fixups/django.py

Line 180
in
39209f2

self._close_database()

Here maybe it should be _close_database(force=True), following #6134: when celery db max reuse is reached we probably do want to really close everything and not ask django to maybe close them according to django settings.

Actual Behavior