Skip to content

Deadlock in worker process handling #496

Closed

Description

@harmv

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

Activity

  1. harmv commented on Oct 7, 2011

    @harmv
    ContributorAuthor

    I found that the Worker gets stuck in execute_and_trace.

  2. reopened this on Oct 7, 2011
  3. harmv commented on Oct 11, 2011

    @harmv
    ContributorAuthor

    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....

    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

    --- 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()
    

    harmv@14c23c4

  4. ask commented on Oct 11, 2011

    @ask
    Contributor

    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?

  5. harmv commented on Oct 11, 2011

    @harmv
    ContributorAuthor

    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?

    Reply to this email directly or view it on GitHub:
    https://github.com/ask/celery/issues/496#issuecomment-2360489

  6. harmv commented on Oct 11, 2011

    @harmv
    ContributorAuthor

    verified in tests that issue is not solved 100% yet.
    Cause analysis is correct, solution not (yet) complete.

  7. harmv commented on Oct 13, 2011

    @harmv
    ContributorAuthor

    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.

    • The previous fix (14c23c4) did
      not solve the problem.
    • now really call the re-init of the logging system every fork.
      (moved init code to worker function)
    • Add initialisation of 2 more locks. (root logger and logging
      global lock)

    this patch survived 30 hours of stress testing. (before it would deadlock in 2-10 hours)

  8. added a commit that references this issue on Oct 14, 2011
  9. haizaar commented on Oct 17, 2011

    @haizaar

    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

  10. lsemel commented on Dec 21, 2011

    @lsemel

    Was this ever solved?

  11. ask commented on Jan 23, 2012

    @ask
    Contributor

    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

    python -m celery.bin.celeryd -l info
    

    instead of just calling celeryd, hopefully we'll find the solution for this too
    (this has been a problem on windows for along time)

  12. harmv commented on Jan 23, 2012

    @harmv
    ContributorAuthor

    Note: since the fix in bf997c1, I was never able to reproduce this problem anymore.

  13. lsemel commented on Jan 23, 2012

    @lsemel

    Does that bug only apply to Windows?

    python -m celery.bin.celeryd -l info

  14. ask commented on Feb 1, 2012

    @ask
    Contributor

    @lsemel it used to apply only to windows, but after the patch mentioned it also happens on unix.

  15. ask commented on Feb 7, 2012

    @ask
    Contributor

    I fixed the 'celeryd' invocation error on Windows and for this branch in 4ea9e5d

  16. added a commit that references this issue on Feb 7, 2012
    363a56c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions