You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
There seems to be something wrong in the process handling in celery 2.3.3
Workers can dead-lock when using CELERYD_MAX_TASKS_PER_CHILD
symptoms:
I'm using CELERYD_MAX_TASKS_PER_CHILD 10, so that celery exits and restart processes every 10 jobs.
However every now and then one worker does not restart. The last lines in the logfile are:
2011-10-03 18:53:04,029: INFO/MainProcess] Got task from broker: XXX[315d2d90-26e2-4df9-8874-bfdf5c41f5fa] expires:[2011-10-03 18:53:10.186636].
[2011-10-03 18:53:04,620: INFO/PoolWorker-92] process shutting down
[2011-10-03 18:53:04,622: INFO/MainProcess] Got task from broker: XXX[8166a098-25d5-4294-b8d0-25907b95a6e0] expires:[2011-10-03 18:53:10.197982]
[2011-10-03 18:53:04,622: INFO/PoolWorker-92] process exiting with exitcode 0
[2011-10-03 18:53:04,622: INFO/MainProcess] Task XXXX[eba55059-163a-465d-b871-be0211533ba1] succeeded in 0.59160900116s: None
[2011-10-03 18:53:04,956: INFO/MainProcess] Got task from broker: XXXX [0d90ed30-2daa-46c1-9ce1-f2b524e7f5eb] expires:[2011-10-03 18:53:10.209910]
[2011-10-03 18:53:04,960: INFO/PoolWorker-93] child process calling self.run()
... after this ... nothing.
Sometimes it works fine for some hours then it just stops here.
I'm starting celery with django-celery celeryd_multi. I have 3 queues, and 3 groups of workers.
Of the example above there is a single worker that handles that particular queue.
It reproduces within a few to 20 hours. every time.
This seems to be a regression in the 2.3 branch.
I think I saw this too with 2.2.4 but less often
I found the deadlock.
Workers occasionally get stuck in python logging.
See stack-trace below.
...
...
return amqp_method(self, args)
File "/home/ep/releases/CURRENT/projects/platform/django/virtualenv/lib/python2.6/site-packages/amqplib/client_0_8/connection.py", line 661, in _start
str(self.server_properties), self.mechanisms, self.locales))
File "/usr/lib/python2.6/logging/__init__.py", line 1036, in debug
self._log(DEBUG, msg, args, **kwargs)
File "/usr/lib/python2.6/logging/__init__.py", line 1165, in _log
self.handle(record)
File "/usr/lib/python2.6/logging/__init__.py", line 1175, in handle
self.callHandlers(record)
File "/usr/lib/python2.6/logging/__init__.py", line 1212, in callHandlers
hdlr.handle(record)
File "/usr/lib/python2.6/logging/__init__.py", line 671, in handle
self.acquire()
File "/usr/lib/python2.6/logging/__init__.py", line 622, in acquire
self.lock.acquire()
File "/usr/lib/python2.6/threading.py", line 132, in acquire
rc = self.__block.acquire(blocking)
I debugged the python.treading.RLock() implementation and found....
python logging, uses RLock() locks, and those are broken after a fork.
Looking the long history of this python bug, the conclusion that this bug is not going to be solved in python.
deadlock is seen when using CELERYD_MAX_TASKS_PER_CHILD, that cause worker process to be forked over an over again.
I made a patch in celery that works around this problem
--- a/celery/apps/worker.py
+++ b/celery/apps/worker.py
@@ -178,6 +178,17 @@ class Worker(object):
print("discard: Erased %d %s from the queue.\n" % (count, what))
def worker_init(self):
+
+ # re-init the logging system
+ # Work around for http://bugs.python.org/issue6721#msg140215
+ # in short: python logging module uses RLock() objects. These are broken after
+ # a fork.
+ # this causes your workers to hang, https://github.com/ask/celery/issues/496
+ logger_names = logging.Logger.manager.loggerDict.keys()
+ for logger_name in logger_names:
+ for handler in logging.getLogger(logger_name).handlers:
+ handler.createLock()
+
# Run the worker init handler.
# (Usually imports task modules and such.)
self.loader.init_worker()
Hmz. Good catch. It was meant to run every fork.
But it doesn't in my current patch.
What location do you suggest to put this logging initialisation code in ?
I put it at that location, as I thought djcelery.loaders.on_worker_init() is
also runs every fork...
I found that djcelery.loaders.on_worker_init() also does not run every
fork. Is that intended ? (I thought those calls to close_database() are also
required every fork, No ?)
On Tue, Oct 11, 2011 at 1:57 PM, Ask Solem Hoel < [email protected]>wrote:
Thanks! This is very good! What is the reason for adding it to
apps/worker.py? Is it not meant to be run after fork?
Activity
harmv commentedon Oct 7, 2011
I found that the Worker gets stuck in execute_and_trace.
let worker_init() re-init the logging system. Fixes deadlock celery#496
harmv commentedon Oct 11, 2011
I found the deadlock.
Workers occasionally get stuck in python logging.
See stack-trace below.
I debugged the python.treading.RLock() implementation and found....
This is caused by a long standing known python issue:
http://bugs.python.org/issue6721
informative comments at: http://bugs.python.org/issue6721#msg140215
python logging, uses RLock() locks, and those are broken after a fork.
Looking the long history of this python bug, the conclusion that this bug is not going to be solved in python.
deadlock is seen when using CELERYD_MAX_TASKS_PER_CHILD, that cause worker process to be forked over an over again.
I made a patch in celery that works around this problem
harmv@14c23c4
ask commentedon Oct 11, 2011
Thanks! This is very good! What is the reason for adding it to apps/worker.py? Is it not meant to be run after fork?
harmv commentedon Oct 11, 2011
Hmz. Good catch. It was meant to run every fork.
But it doesn't in my current patch.
What location do you suggest to put this logging initialisation code in ?
I put it at that location, as I thought djcelery.loaders.on_worker_init() is
also runs every fork...
I found that djcelery.loaders.on_worker_init() also does not run every
fork. Is that intended ? (I thought those calls to close_database() are also
required every fork, No ?)
On Tue, Oct 11, 2011 at 1:57 PM, Ask Solem Hoel <
[email protected]>wrote:
harmv commentedon Oct 11, 2011
verified in tests that issue is not solved 100% yet.
Cause analysis is correct, solution not (yet) complete.
Subject: [PATCH 1/1] Renewed fix for deadlock (ticket celery#496)
harmv commentedon Oct 13, 2011
Renewed fix for deadlock (ticket #496) in harmv@bf997c1
https://github.com/ask/celery/issues/496
Work around for http://bugs.python.org/issue6721#msg140215
in short: python logging module uses RLock() objects. These are broken after a fork.
not solve the problem.
(moved init code to worker function)
global lock)
this patch survived 30 hours of stress testing. (before it would deadlock in 2-10 hours)
Better fix for #496.
haizaar commentedon Oct 17, 2011
I think I'm experiencing this issue with workers autoscaling in celery 2.2.7.
Is there a chance to see this fix merged to 2.2.x and pushed to pypi?
Thank you,
Zaar
lsemel commentedon Dec 21, 2011
Was this ever solved?
ask commentedon Jan 23, 2012
This branch: https://github.com/ask/celery/tree/using_execv
Uses fork+execv instead of just fork so it should solve this issue.
Could you guys please try it out?
There's one bug where you have to start celeryd with
instead of just calling celeryd, hopefully we'll find the solution for this too
(this has been a problem on windows for along time)
harmv commentedon Jan 23, 2012
Note: since the fix in bf997c1, I was never able to reproduce this problem anymore.
lsemel commentedon Jan 23, 2012
Does that bug only apply to Windows?
python -m celery.bin.celeryd -l infoask commentedon Feb 1, 2012
@lsemel it used to apply only to windows, but after the patch mentioned it also happens on unix.
ask commentedon Feb 7, 2012
I fixed the 'celeryd' invocation error on Windows and for this branch in 4ea9e5d
Adds CELERYD_FORCE_EXECV option. Closes celery#496