Issue 1:
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

None

Possible Duplicates

None

Expected Behavior

The celery_worker pytest fixture should either wait until all enqueued tasks are processed before stopping the worker or, alternatively, it should raise an exception if the worker fails to terminate within the defined timeout (currently hardcoded to 10s).

Actual Behavior

If, when the cleanup code of the celery_worker pytest fixture is called, the worker is still executing tasks, and those tasks take longer than 10s to terminate, the worker thread is left in running state and the worker will never be stopped, causing the pytest process to never exit.

Additional information

The code around

celery/celery/contrib/testing/worker.py

Line 133
in
7288147

t.join(10)

waits for the thread to exit with a timeout of 10 seconds, but the status of the thread once the threading API .join method returns is not checked, potentially leaving the thread running.

Because of the thread status not being checked (and the .join() method not raising exceptions on timeout), the execution proceeds cleanly, by resetting the should_terminate state variable back to None, causing the worker thread to stay alive even after it completed all the enqueued tasks.

At a minimum, the fixture should raise an exception if the worker is still alive once the timeout is expired, letting the developer know about the issue and letting him make sure that all tasks have been processed before exiting the test and letting pytest invoke the teardown code. An additional improvement would be to let the timeout be overridable e.g. via another fixture as done for celery_worker_parameters or to let the developer indicate that the worker should wait until the current/all pending tasks have been completed before exiting via a more robust method.

Issue 2:
I'm using celery to run tasks which are listening to some web connections and recording the messages. The environment information is as following:

software -> celery:4.1.0 (latentcall) kombu:4.1.0 py:3.6.3

billiard:3.5.0.3 py-amqp:2.2.2

platform -> system:Linux arch:64bit imp:CPython

loader -> celery.loaders.app.AppLoader

settings -> transport:amqp results:disabled

These tasks should always be running as I used while true loop in async function to keep recording messages sent by web server. However, after several hours, all celery tasks stopped. And I found the main process and the child processes of my celery worker were still running but the start time of child processes' was much newer than the main process.

PID STARTED ELAPSED COMMAND

15785 Mon Apr 23 03:20:01 2018 2-04:53:38 /home/ubuntu/anaconda3/bin/python /home/ubuntu/anaconda3/bin/celery worker -A data_fetcher --loglevel=info --logfile=log.txt --concurrency=10

18587 Tue Apr 24 08:04:20 2018 1-00:09:19 /home/ubuntu/anaconda3/bin/python /home/ubuntu/anaconda3/bin/celery worker -A data_fetcher --loglevel=info --logfile=log.txt --concurrency=10

18588 Tue Apr 24 08:04:20 2018 1-00:09:19 /home/ubuntu/anaconda3/bin/python /home/ubuntu/anaconda3/bin/celery worker -A data_fetcher --loglevel=info --logfile=log.txt --concurrency=10

More related information in celery log file(all child processes):

[2018-04-24 08:04:14,623: ERROR/MainProcess] Process 'ForkPoolWorker-6' pid:15794 exited with 'exitcode 70'

[2018-04-24 08:04:14,633: ERROR/MainProcess] Task handler raised error: WorkerLostError('Worker exited prematurely: exitcode 70.',)

Traceback (most recent call last):

File "/home/ubuntu/anaconda3/lib/python3.6/site-packages/celery/worker/worker.py", line 203, in start

self.blueprint.start(self)

File "/home/ubuntu/anaconda3/lib/python3.6/site-packages/celery/bootsteps.py", line 119, in start

step.start(parent)

File "/home/ubuntu/anaconda3/lib/python3.6/site-packages/celery/bootsteps.py", line 370, in start

return self.obj.start()

File "/home/ubuntu/anaconda3/lib/python3.6/site-packages/celery/worker/consumer/consumer.py", line 320, in start

blueprint.start(self)

File "/home/ubuntu/anaconda3/lib/python3.6/site-packages/celery/bootsteps.py", line 119, in start

step.start(parent)

File "/home/ubuntu/anaconda3/lib/python3.6/site-packages/celery/worker/consumer/consumer.py", line 596, in start

c.loop(*c.loop_args())

File "/home/ubuntu/anaconda3/lib/python3.6/site-packages/celery/worker/loops.py", line 77, in asynloop

raise WorkerShutdown(should_stop)

celery.exceptions.WorkerShutdown: 0

During handling of the above exception, another exception occurred:

Traceback (most recent call last):

File "/home/ubuntu/anaconda3/lib/python3.6/site-packages/billiard/pool.py", line 1223, in mark_as_worker_lost

human_status(exitcode)),

billiard.exceptions.WorkerLostError: Worker exited prematurely: exitcode 70.

Why were the child processes shut down and restarted? (and the celery tasks were never queued again.)

What's the best practice to keep such long-running tasks running forever?

Any help would be appreciated!