<h1 dir="auto">Checklist</h1>

<ul class="contains-task-list">
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I have verified that the issue exists against the <code class="notranslate">master</code> branch of Celery.</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> This has already been asked to the <a href="https://groups.google.com/forum/#!forum/celery-users" rel="nofollow">discussion group</a> first.</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I have read the relevant section in the<br>
<a href="http://docs.celeryproject.org/en/latest/contributing.html#other-bugs" rel="nofollow">contribution guide</a><br>
on reporting bugs.</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I have checked the <a href="https://github.com/celery/celery/issues?q=is%3Aissue+label%3A%22Issue+Type%3A+Bug+Report%22+-label%3A%22Category%3A+Documentation%22">issues list</a><br>
for similar or identical bug reports.</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I have checked the <a href="https://github.com/celery/celery/pulls?q=is%3Apr+label%3A%22PR+Type%3A+Bugfix%22+-label%3A%22Category%3A+Documentation%22">pull requests list</a><br>
for existing proposed fixes.</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I have checked the <a href="https://github.com/celery/celery/commits/master">commit log</a><br>
to find out if the bug was already fixed in the master branch.</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I have included all related issues and possible duplicate issues<br>
in this issue (If there are none, check this box anyway).</li>
</ul>
<h2 dir="auto">Mandatory Debugging Information</h2>
<ul class="contains-task-list">
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> I have included the output of <code class="notranslate">celery -A proj report</code> in the issue.<br>
(if you are not able to do this, then at least specify the Celery<br>
version affected).</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I have verified that the issue exists against the <code class="notranslate">master</code> branch of Celery.</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> I have included the contents of <code class="notranslate">pip freeze</code> in the issue.</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> I have included all the versions of all the external dependencies required<br>
to reproduce this bug.</li>
</ul>
<h2 dir="auto">Optional Debugging Information</h2>

<ul class="contains-task-list">
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> I have tried reproducing the issue on more than one Python version<br>
and/or implementation.</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> I have tried reproducing the issue on more than one message broker and/or<br>
result backend.</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> I have tried reproducing the issue on more than one version of the message<br>
broker and/or result backend.</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> I have tried reproducing the issue on more than one operating system.</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> I have tried reproducing the issue on more than one workers pool.</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> I have tried reproducing the issue with autoscaling, retries,<br>
ETA/Countdown &amp; rate limits disabled.</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> I have tried reproducing the issue after downgrading<br>
and/or upgrading Celery and its dependencies.</li>
</ul>
<h2 dir="auto">Related Issues and Possible Duplicates</h2>

<h4 dir="auto">Related Issues</h4>
<ul dir="auto">
<li>None</li>
</ul>
<h4 dir="auto">Possible Duplicates</h4>
<ul dir="auto">
<li>None</li>
</ul>
<h1 dir="auto">Expected Behavior</h1>

<p dir="auto">The <code class="notranslate">celery_worker</code> 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).</p>
<h1 dir="auto">Actual Behavior</h1>

<p dir="auto">If, when the cleanup code of the <code class="notranslate">celery_worker</code> 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.</p>
<h1 dir="auto">Additional information</h1>
<p dir="auto">The code around </p><div class="Box Box--condensed my-2">
  <div class="Box-header f6">
    <p class="mb-0 text-bold">
      <a href="https://github.com/celery/celery/blob/7288147d65a32b726869ed887d99e4bfd8c070e2/celery/contrib/testing/worker.py#L133">celery/celery/contrib/testing/worker.py</a>
    </p>
    <p class="mb-0 color-fg-muted">
         Line 133
      in
      <a data-pjax="true" class="commit-tease-sha" href="/celery/celery/commit/7288147d65a32b726869ed887d99e4bfd8c070e2">7288147</a>
    </p>
  </div>
  <div itemprop="text" class="Box-body p-0 blob-wrapper blob-wrapper-embedded data">
    <table class="highlight tab-size mb-0 js-file-line-container" data-tab-size="8" data-paste-markdown-skip="">

        <tbody><tr class="border-0">
          <td id="L133" class="blob-num border-0 px-3 py-0 color-bg-default" data-line-number="133"></td>
          <td id="LC133" class="blob-code border-0 px-3 py-0 color-bg-default blob-code-inner js-file-line"> <span class="pl-s1">t</span>.<span class="pl-en">join</span>(<span class="pl-c1">10</span>) </td>
        </tr>
    </tbody></table>
  </div>
</div>
 waits for the thread to exit with a timeout of 10 seconds, but the status of the thread once the threading API <code class="notranslate">.join</code> method returns is not checked, potentially leaving the thread running.<p></p>
<p dir="auto">Because of the thread status not being checked (and the <code class="notranslate">.join()</code> method not raising exceptions on timeout), the execution proceeds cleanly, by resetting the <code class="notranslate">should_terminate</code> state variable back to <code class="notranslate">None</code>, causing the worker thread to stay alive even after it completed all the enqueued tasks.</p>
<p dir="auto">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 <a href="https://github.com/celery/celery/blob/7288147d65a32b726869ed887d99e4bfd8c070e2/celery/contrib/pytest.py#L149">celery_worker_parameters</a> 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.</p>
<p dir="auto">I'm using celery to run tasks which are listening to some web connections and recording the messages. The environment information is as following:</p>
<blockquote>
<p dir="auto"><strong>software -&gt; celery:4.1.0 (latentcall) kombu:4.1.0 py:3.6.3<br>
billiard:3.5.0.3 py-amqp:2.2.2<br>
platform -&gt; system:Linux arch:64bit imp:CPython<br>
loader   -&gt; celery.loaders.app.AppLoader<br>
settings -&gt; transport:amqp results:disabled</strong></p>
</blockquote>
<p dir="auto">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.</p>
<blockquote>
<p dir="auto"><strong>PID                  STARTED     ELAPSED COMMAND<br>
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<br>
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<br>
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</strong></p>
</blockquote>
<p dir="auto">More related information in celery log file(all child processes):</p>
<blockquote>
<p dir="auto">[2018-04-24 08:04:14,623: ERROR/MainProcess] Process 'ForkPoolWorker-6' pid:15794 exited with 'exitcode 70'<br>
[2018-04-24 08:04:14,633: ERROR/MainProcess] Task handler raised error: WorkerLostError('Worker exited prematurely: exitcode 70.',)<br>
Traceback (most recent call last):<br>
File "/home/ubuntu/anaconda3/lib/python3.6/site-packages/celery/worker/worker.py", line 203, in start<br>
self.blueprint.start(self)<br>
File "/home/ubuntu/anaconda3/lib/python3.6/site-packages/celery/bootsteps.py", line 119, in start<br>
step.start(parent)<br>
File "/home/ubuntu/anaconda3/lib/python3.6/site-packages/celery/bootsteps.py", line 370, in start<br>
return self.obj.start()<br>
File "/home/ubuntu/anaconda3/lib/python3.6/site-packages/celery/worker/consumer/consumer.py", line 320, in start<br>
blueprint.start(self)<br>
File "/home/ubuntu/anaconda3/lib/python3.6/site-packages/celery/bootsteps.py", line 119, in start<br>
step.start(parent)<br>
File "/home/ubuntu/anaconda3/lib/python3.6/site-packages/celery/worker/consumer/consumer.py", line 596, in start<br>
c.loop(*c.loop_args())<br>
File "/home/ubuntu/anaconda3/lib/python3.6/site-packages/celery/worker/loops.py", line 77, in asynloop<br>
raise WorkerShutdown(should_stop)<br>
celery.exceptions.WorkerShutdown: 0</p>
<p dir="auto">During handling of the above exception, another exception occurred:</p>
<p dir="auto">Traceback (most recent call last):<br>
File "/home/ubuntu/anaconda3/lib/python3.6/site-packages/billiard/pool.py", line 1223, in mark_as_worker_lost<br>
human_status(exitcode)),<br>
billiard.exceptions.WorkerLostError: Worker exited prematurely: exitcode 70.</p>
</blockquote>
<p dir="auto">Why were the child processes shut down and restarted? (and the celery tasks were never queued again.)<br>
What's the best practice to keep such long-running tasks running forever?<br>
Any help would be appreciated!</p>