Skip to content

Allow celery to spawn rather than fork #6036

Closed

Description

@ibrewster

Checklist

  • I have checked the issues list
    for similar or identical feature requests.
  • I have checked the pull requests list
    for existing proposed implementations of this feature.
  • I have checked the commit log
    to find out if the if the same feature was already implemented 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).

Related Issues and Possible Duplicates

Related Issues

  • None

Possible Duplicates

Similar to issue #5005 and issue #5021

Brief Summary

When launching child processes, celery uses the fork method. Sometimes it is preferable to be able to spawn rather than fork, for instance: https://bugs.python.org/issue33725. Celery 3.x had this ability with the CELERYD_FORCE_EXECV option, however it was apparently removed in version 4.0 for some reason ( https://docs.celeryproject.org/en/stable/history/whatsnew-4.0.html). Please re-introduce this feature.

Design

Architectural Considerations

None

Proposed Behavior

This feature would behave exactly as it (presumably) did in version 3.x: using spawn to launch child workers rather than fork. It is likely this is already the behavior when run on windows anyway, since at least in the python multiprocessing module fork is not available on windows.

Proposed UI/UX

Same as in version 3.x: provide a CELERYD_FORCE_EXECV option (although I guess it would be lowercase now)

Diagrams

N/A

Alternatives

None

Activity

  1. added this to the 4.5 milestone on May 16, 2020
  2. georgexsh commented on May 24, 2020

    @georgexsh

    I think celery should defer init_worker call inside the worker process with the prefork pool, which will resolve this and many other issues together.

    (following message is copied from a celery-users mailing list thread: https://groups.google.com/d/msgid/celery-users/468ff21a-afd1-4887-ab9a-b7e548501e65%40googlegroups.com?utm_medium=email&utm_source=footer)

    currently, on platform supports fork(), and prefork pool is used, app.loader.init_worker(from AppLoader) is called in the master process, which is very annoying.
    init_worker will import app task modules, as app task modules are imported inside the master process, user application code is executed there then master forked worker, but not all the application code or library is prepared for a fork call, generally, they are multithreading safe, but not fork safe.

    take the Queue(queue in py3) stdlib(!!) for example, if the application created a Queue instance, then process forked, deadlock happened in the child process if the Queue instance gets used, as the mutex inside Queue is not fork-safe.
    https://groups.google.com/forum/#!topic/sqlalchemy/YX9NuBD75oY is a serious problem we have met also.
    #496 is another example.

    I think having the master process polluted with user application code before fork() is a bad design, take gunicorn as a good example, it will not touch the user WSGI app code before the worker forked by default, but give the user the option to do so.

  3. auvipy commented on May 24, 2020

    @auvipy
    Member

    I think celery should defer init_worker call inside the worker process with the prefork pool, which will resolve this and many other issues together.

    (following message is copied from a celery-users mailing list thread: https://groups.google.com/d/msgid/celery-users/468ff21a-afd1-4887-ab9a-b7e548501e65%40googlegroups.com?utm_medium=email&utm_source=footer)

    currently, on platform supports fork(), and prefork pool is used, app.loader.init_worker(from AppLoader) is called in the master process, which is very annoying.
    as app task modules are imported inside the master process, user application code is executed before fork, but not all the application code or library is prepared for the fork, generally, they are multithreading safe, but not fork safe.
    for example, the Queue stdlib, if the application created Queue instance, but process forked, deadlock happened as the mutex Queue used is not fork-safe.
    https://groups.google.com/forum/#!topic/sqlalchemy/YX9NuBD75oY is a serious problem we have met also.
    #496 is another example.
    I think having the master process polluted with user application code before fork() is a bad design, take gunicorn as a good example, it will not touch the user WSGI app code before the worker forked by default, but give the user the option to do so.

    thanks for chiming in. would you mind handling this?

  4. georgexsh commented on May 24, 2020

    @georgexsh

    for now, I am trying to find a workaround for our application by subclassing AppLoader, let init_worker do nothing in the master process if the prefork pool is used because process_initializer would trigger init_worker again in the worker process anyway.
    @auvipy do you think this could be polished as a formal solution?
    the problem is, so far I can't find a sound way to tell the difference between master and worker. environ variable like _MP_FORK_LOGFILE_ is not a reliable source I think. maybe I should go to check the parent process title?

  5. auvipy commented on May 24, 2020

    @auvipy
    Member

    please dig deeper. i will need some time to revisit this

  6. georgexsh commented on May 25, 2020

    @georgexsh

    a quick and dirty demo:

    from celery.loaders.app import AppLoader
    
    class ForkLazyAppLoader(AppLoader):
    
        def _is_in_child_process(self):
            # FIXME assert prefork pool
            from billiard.process import current_process
            p = current_process()
            if p._parent_pid is not None:
                return True
            return False
    
        def init_worker(self):
            if not self._is_in_child_process():
                return
            super(ForkSafeAppLoader, self).init_worker()
    
    app = Celery(..., loader=ForkLazyAppLoader)
  7. xirdneh commented on May 25, 2020

    @xirdneh
    Member

    @georgexsh I think this looks like a good solution to explore.
    Have you been able to test this locally?
    I'm curious to see if the master process brakes in some way.

  8. georgexsh commented on May 26, 2020

    @georgexsh
  9. auvipy commented on May 26, 2020

    @auvipy
    Member

    would you mind come with a PR?

  10. georgexsh commented on May 26, 2020

    @georgexsh

    @auvipy not yet, it misses worker pool class checking which I have no clue, and, it is unclear to me where should the patch goes, do you think it should be the default behavior of AppLoader, or as an option?

  11. georgexsh commented on May 26, 2020

    @georgexsh

    besides, dependent on a non-std API p._parent_pid is not a good idea to me, billiard should expose an API to indicate the current process is a worker.

  12. xirdneh commented on May 26, 2020

    @xirdneh
    Member

    @georgexsh I think it would be a good idea to open up a PR and work on those things. I can lend a hand if you want.
    Also, that is if you have time to keep an eye of this PR.
    Having said that let me ask a few questions:

    • what do you mean by

    it misses worker pool class checking which I have no clue

    • I think the best option is to expose this as an option and not the default behavior. Maybe in the future we can move this to be the default.
    • I agree that this should not use a non-standard API from billiard. We can also open up a PR on Billiard to standardize this if we don't find a better way to see if it's the master thread or not. Although, given the fact that this is a Unix specific problem it shouldn't be too difficult to come up with a solution.
  13. self-assigned this
    on May 26, 2020
  14. georgexsh commented on May 27, 2020

    @georgexsh
  15. 45 remaining items

  16. stuaxo commented on Aug 29, 2025

    @stuaxo
    Contributor

    When it comes to PsycoPg (as used in the Celery+Django+Postgres) combination, it was kerberos auth that was triggering this (most of the time), and a fix is being added there to disable that when it's not being used.

    This should help somewhat until sparn is available here.
    psycopg/psycopg#1136

  17. IngmarStein commented on Sep 27, 2025

    @IngmarStein

    I've started seeing paperless-ngx's Celery worker crash after updating to macOS 26.0 (still the same in 26.1 beta 1) which may trigger another fork-unsafe call. A workaround is to use any other pool type than the default prefork.

    Here's a stacktrace:

    Application Specific Information:
    *** multi-threaded process forked ***
    crashed on child side of fork pre-exec
    
    
    Thread 0 Crashed:
    0   libsystem_trace.dylib         	       0x180a690b4 _os_log_find + 64
    1   libsystem_trace.dylib         	       0x180a68dd4 os_log_create + 304
    2   libswiftos.dylib              	       0x19de85a7c Logger.init(subsystem:category:) + 92
    3   Network                       	       0x1892b7ed4 0x18903b000 + 2608852
    4   libdispatch.dylib             	       0x180b9aac4 _dispatch_client_callout + 16
    5   libdispatch.dylib             	       0x180b83a60 _dispatch_once_callout + 32
    6   Network                       	       0x18925a1b4 0x18903b000 + 2224564
    7   Network                       	       0x189119dd4 0x18903b000 + 912852
    8   Network                       	       0x18911d6ac 0x18903b000 + 927404
    9   Network                       	       0x18911d5e4 0x18903b000 + 927204
    10  Network                       	       0x189117018 0x18903b000 + 901144
    11  Network                       	       0x189e77318 nw_path_access_agent_cache + 188
    12  Network                       	       0x189113e44 0x18903b000 + 888388
    13  Network                       	       0x189113580 0x18903b000 + 886144
    14  Network                       	       0x1893d9a58 _nw_path_update_is_viableTm + 84
    15  Network                       	       0x189e50f20 nw_path_snapshot_path(NWConcrete_nw_path*) + 64
    16  Network                       	       0x189e50698 nw_path_evaluator_evaluate(NWConcrete_nw_path_evaluator*, int*) + 2308
    17  Network                       	       0x189e4f578 nw_path_create_evaluator_for_endpoint + 68
    18  Network                       	       0x189ca88a0 nw_nat64_v4_address_requires_synthesis + 240
    19  libsystem_info.dylib          	       0x180d6554c _gai_nat64_second_pass + 280
    20  libsystem_info.dylib          	       0x180d51900 si_addrinfo + 1388
    21  libsystem_info.dylib          	       0x180d512ec getaddrinfo + 168
    22  _socket.cpython-313-darwin.so 	       0x109ee9bc4 socket_getaddrinfo + 484
    23  Python                        	       0x104f5d51c cfunction_call + 76
    24  Python                        	       0x104effb3c _PyObject_MakeTpCall + 120
    25  Python                        	       0x1050295a4 _PyEval_EvalFrameDefault + 6876
    26  Python                        	       0x104eff9b4 _PyObject_VectorcallDictTstate + 200
    27  Python                        	       0x104f92e28 slot_tp_init + 196
    28  Python                        	       0x104f88de0 type_call + 148
    29  Python                        	       0x104effb3c _PyObject_MakeTpCall + 120
    30  Python                        	       0x10502f0e4 _PyEval_EvalFrameDefault + 30236
    31  Python                        	       0x104eff9b4 _PyObject_VectorcallDictTstate + 200
    32  Python                        	       0x104f92e28 slot_tp_init + 196
    33  Python                        	       0x104f88de0 type_call + 148
    34  Python                        	       0x104effb3c _PyObject_MakeTpCall + 120
    35  Python                        	       0x10502f0e4 _PyEval_EvalFrameDefault + 30236
    36  Python                        	       0x104eff948 _PyObject_VectorcallDictTstate + 92
    37  Python                        	       0x104f00f18 _PyObject_Call_Prepend + 136
    38  Python                        	       0x104f91d40 slot_tp_call + 128
    39  Python                        	       0x104f0081c _PyObject_Call + 128
    40  Python                        	       0x10502d1c0 _PyEval_EvalFrameDefault + 22264
    41  Python                        	       0x104eff948 _PyObject_VectorcallDictTstate + 92
    42  Python                        	       0x104f00f18 _PyObject_Call_Prepend + 136
    43  Python                        	       0x104f91d40 slot_tp_call + 128
    44  Python                        	       0x104f0081c _PyObject_Call + 128
    45  Python                        	       0x10502d1c0 _PyEval_EvalFrameDefault + 22264
    46  Python                        	       0x104f03124 method_vectorcall + 392
    47  Python                        	       0x10502d1c0 _PyEval_EvalFrameDefault + 22264
    48  Python                        	       0x104eff948 _PyObject_VectorcallDictTstate + 92
    49  Python                        	       0x104f00f18 _PyObject_Call_Prepend + 136
    50  Python                        	       0x104f91d40 slot_tp_call + 128
    51  Python                        	       0x104effb3c _PyObject_MakeTpCall + 120
    52  Python                        	       0x105029cc0 _PyEval_EvalFrameDefault + 8696
    53  Python                        	       0x104f1c428 gen_send_ex2 + 196
    54  Python                        	       0x104f1afe4 gen_iternext + 36
    55  Python                        	       0x105024328 builtin_next + 72
    56  Python                        	       0x10502c144 _PyEval_EvalFrameDefault + 18044
    57  Python                        	       0x104f030e0 method_vectorcall + 324
    58  Python                        	       0x104f00534 _PyVectorcall_Call + 152
    59  Python                        	       0x10502d1c0 _PyEval_EvalFrameDefault + 22264
    60  Python                        	       0x104f030e0 method_vectorcall + 324
    61  Python                        	       0x104f00534 _PyVectorcall_Call + 152
    62  Python                        	       0x10502d1c0 _PyEval_EvalFrameDefault + 22264
    63  Python                        	       0x104eff9b4 _PyObject_VectorcallDictTstate + 200
    64  Python                        	       0x104f00f18 _PyObject_Call_Prepend + 136
    65  Python                        	       0x104f91d40 slot_tp_call + 128
    66  Python                        	       0x104effb3c _PyObject_MakeTpCall + 120
    67  Python                        	       0x10502f0e4 _PyEval_EvalFrameDefault + 30236
    68  Python                        	       0x10502784c PyEval_EvalCode + 200
    69  Python                        	       0x1050981bc run_eval_code_obj + 104
    70  Python                        	       0x105097c04 run_mod + 168
    71  Python                        	       0x1050962ac pyrun_file + 164
    72  Python                        	       0x1050955e8 _PyRun_SimpleFileObject + 256
    73  Python                        	       0x105095220 _PyRun_AnyFileObject + 80
    74  Python                        	       0x1050bceb8 pymain_run_file_obj + 164
    75  Python                        	       0x1050bcb64 pymain_run_file + 76
    76  Python                        	       0x1050bbdd4 Py_RunMain + 968
    77  Python                        	       0x1050bc3d0 pymain_main + 304
    78  Python                        	       0x1050bc470 Py_BytesMain + 40
    79  dyld                          	       0x180975d54 start + 7184
    
  18. Nusnus commented on Feb 26, 2026

    @Nusnus
    Member
  19. modified the milestones: 6.0, 5.7.0 on Jul 5, 2026
  20. auvipy commented on Jul 5, 2026

    @auvipy
    Member

    This pr #10339 might be a potential fix for this, need people to check and try this

  21. modified the milestones: 5.7.0, 5.8.0 on Aug 31, 2026
  22. modified the milestones: 5.8.0, 5.7.0 on Sep 9, 2026
  23. auvipy commented on Sep 9, 2026

    @auvipy
    Member

    another relevant pr #10584

  24. ShaheedHaque commented on Sep 10, 2026

    @ShaheedHaque
    Contributor

    @auvipy Could you kindly summarise here, or with a pointer to the docs, how to enable this? (with or without async?)

  25. auvipy commented on Sep 10, 2026

    @auvipy
    Member

    please wait until the pre release of v5.7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions