Skip to content

Use spawn pool on POSIX platforms - #7612

Closed
itamarst wants to merge 6 commits into
celery:masterfrom
itamarst:6036-always-spawn-in-multiprocessing
Closed

itamarst wants to merge 6 commits into
celery:masterfrom
itamarst:6036-always-spawn-in-multiprocessing

Conversation

@itamarst

@itamarst itamarst commented Jul 6, 2022 •

Copy link
Copy Markdown

Note: Before submitting this pull request, please review our contributing
guidelines
.

Description

Fixes #6036

Rather than making the pool strategy configurable, this always uses spawn, for the following reason:

So instead of an extra knob to disable a broken-by-default feature, this just uses a correct-by-default and consistent-across-platforms default, with no knob to turn it off. (The blocking issue on Windows is not related to spawn AFAICT, and Windows always spawn).

@codecov

codecov Bot commented Jul 7, 2022 •

Copy link
Copy Markdown

Codecov Report

Merging #7612 (8b5ea4e) into master (515f98b) will decrease coverage by 0.00%.
The diff coverage is n/a.

@@            Coverage Diff             @@
##           master    #7612      +/-   ##
==========================================
- Coverage   89.75%   89.75%   -0.01%     
==========================================
  Files         138      138              
  Lines       17000    16998       -2     
  Branches     2509     2509              
==========================================
- Hits        15259    15257       -2     
  Misses       1491     1491              
  Partials      250      250              
Flag Coverage Δ
unittests 89.74% <ø> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
celery/concurrency/prefork.py 97.72% <ø> (-0.06%) ⬇️
celery/worker/components.py 97.52% <ø> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 515f98b...8b5ea4e. Read the comment docs.

@auvipy auvipy left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for tackling this, would you please add some tests for this?

@itamarst

itamarst commented Jul 7, 2022

Copy link
Copy Markdown
Author

Will do!

@itamarst
itamarst requested a review from auvipy July 7, 2022 21:43
@itamarst

itamarst commented Jul 7, 2022

Copy link
Copy Markdown
Author

OK, I've updated with tests, and making it actually work 😁 (all existing tests used mock, to my surprise).

@auvipy auvipy left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

one failure
=========================== short test summary info ===========================
FAILED t/unit/concurrency/test_asynpool.py::test_AsynPool::test_subprocesses_are_spawned
= 1 failed,

forking_enable(True) is a no-op. False would set billiard's default context
to spawn, but we do that explicitly in asynpool.py, which is better than
global changes.
@itamarst itamarst changed the title Use spawn pool on all platforms, not just Windows Use spawn pool on POSIX platforms Jul 8, 2022
@itamarst

itamarst commented Jul 8, 2022

Copy link
Copy Markdown
Author

So... Windows appears to not support processes at the moment (https://github.com/celery/celery/blob/master/celery/worker/components.py#L140). So one of my points in original PR description above is not correct. However, Celery did used to support spawn-style process pools in the past, there's vestigial configuration for that which I removed.

To deal with Windows failures I just skipped the test, and updated code to explain why.

@itamarst
itamarst requested a review from auvipy July 8, 2022 14:11
@auvipy auvipy added this to the 5.3 milestone Jul 8, 2022
@auvipy

auvipy commented Jul 8, 2022

Copy link
Copy Markdown
Member

do we need to update some related documentation with versionadded 5.3 annotation?

@auvipy

auvipy commented Jul 8, 2022

Copy link
Copy Markdown
Member

also check the integration tests failures please

@itamarst

itamarst commented Jul 9, 2022

Copy link
Copy Markdown
Author

Will do.

@itamarst

itamarst commented Jul 9, 2022

Copy link
Copy Markdown
Author

Oops apparently I ran integration tests in previous testing but not at the right time after I got the right code path sorted. And indeed, it does not work. And since Windows was not actually using spawn, that validation of spawn working is not available... so perhaps it degraded since it was last supported.

In order for spawn to fail where fork succeeds, something must be wrong somewhere. Potential sites include:

  1. Code running in the child worker relies on class or module-level state set in the parent process before it was forked.
  2. There's a bug in Billiard's spawn codepaths. Seems less likely?
  3. Something involving file descriptor inheritance? And Python sets FD_CLOEXEC or Windows equivalent so that execve() (but not fork()!) closes file descriptors: https://peps.python.org/pep-0446/

Anything else?

@itamarst

itamarst commented Jul 9, 2022 •

Copy link
Copy Markdown
Author

Interestingly the first task sent to a worker (ping typically in tests) does succeed. So communication is happening.

@itamarst

itamarst commented Jul 9, 2022

Copy link
Copy Markdown
Author

So I noticed billiard used to have a C extension that was supposedly required for execve mode to work. Is the extension module no longer necessary, or was it just not maintained, or something else? It's disabled on Python 3: https://github.com/celery/billiard/blob/master/setup.py#L232

@itamarst

itamarst commented Jul 9, 2022

Copy link
Copy Markdown
Author

@auvipy

auvipy commented Jul 10, 2022

Copy link
Copy Markdown
Member

also upgrading/syncing the billiard codes is a long overdue

@itamarst

Copy link
Copy Markdown
Author

Here we can see some logs where the first task (ping) succeeds. But then there's a kombu error; the second task does not succeed:

INFO     celery.worker.consumer.connection:connection.py:22 Connected to amqp://guest:**@127.0.0.1:5672//
INFO     celery.worker.strategy:strategy.py:161 Task celery.ping[83f98219-6663-458c-bd01-437091802b4b] received
[2022-07-10 13:57:23,212: INFO/SpawnPoolWorker-2] child process 834713 calling self.run()
[2022-07-10 13:57:23,283: INFO/SpawnPoolWorker-2] Task celery.ping[83f98219-6663-458c-bd01-437091802b4b] succeeded in 0.025449884007684886s: 'pong'
ERROR    kombu.pidbox:pidbox.py:106 pidbox command error: AttributeError("'NoneType' object has no attribute 'groups'")
Traceback (most recent call last):
  File "/home/itamarst/devel/celery/.tox/3.7-integration-rabbitmq/lib/python3.7/site-packages/kombu/pidbox.py", line 102, in dispatch
    reply = handle(method, arguments)
  File "/home/itamarst/devel/celery/.tox/3.7-integration-rabbitmq/lib/python3.7/site-packages/kombu/pidbox.py", line 124, in handle_cast
    return self.handle(method, arguments)
  File "/home/itamarst/devel/celery/.tox/3.7-integration-rabbitmq/lib/python3.7/site-packages/kombu/pidbox.py", line 118, in handle
    return self.handlers[method](self.state, **arguments)
  File "/home/itamarst/devel/celery/celery/worker/control.py", line 275, in enable_events
    if dispatcher.groups and 'task' not in dispatcher.groups:
AttributeError: 'NoneType' object has no attribute 'groups'

Any idea what might cause that, and any sense if it's related?

@thedrow

thedrow commented Aug 18, 2022

Copy link
Copy Markdown
Contributor

So I noticed billiard used to have a C extension that was supposedly required for execve mode to work. Is the extension module no longer necessary, or was it just not maintained, or something else? It's disabled on Python 3: https://github.com/celery/billiard/blob/master/setup.py#L232

Billiard itself needs a maintainer who knows what he's doing. The C extension, like every other component in billiard receives very little attention.

@xboy2k11

Copy link
Copy Markdown

@itamarst Good day, sir. We have had this same issue with celery in several different applications over the years. I last solved it with the spawn fix, but that's no longer an option since airflow needs a newer version of celery. We would be happy to help test this at scale once you're confident the PR is complete.

@itamarst

Copy link
Copy Markdown
Author

Unfortunately I don't have time to work on this at the moment, so if someone else wants to take it up let me know.

@ShaheedHaque

Copy link
Copy Markdown
Contributor

also upgrading/syncing the billiard codes is a long overdue

Is there some information - or documentation - on what Billiard actually does? What was/is the rationale for replacing multiprocessing?

@sabetAI

sabetAI commented Dec 13, 2022

Copy link
Copy Markdown

@itamarst I get a weird crash, and the following warning when trying to run celery with your PR:

/home//miniconda3/lib/python3.9/multiprocessing/resource_tracker.py:216: UserWarning: resource_tracker: There appear to be 1 leaked semaphore objects to clean up at shutdown

Any ideas why?

@itamarst

Copy link
Copy Markdown
Author

@sabetAI I never got this to working state, sorry... Will close to prevent confusion. Still would be nice if someone could figure out how to do this.

@itamarst itamarst closed this Dec 13, 2022
@sabetAI

sabetAI commented Dec 13, 2022

Copy link
Copy Markdown

@itamarst no worries. Do you have a dockerfile I can use to reproduce?

@ShaheedHaque

Copy link
Copy Markdown
Contributor

FWIW, I would love to see this taken forward. It seems clear that this core fork-versus-spawn functionality of Celery is broken in multiple use cases and with a variety of hard-to-track down symptoms, but it also seems clear that no one person has the knowledge+willingness to drive this to conclusion. Might it be possible to draw together a small group to work on this in some collaborative fashion?

@auvipy auvipy modified the milestones: 5.3, 6.0.0 Dec 13, 2022
@sabetAI

sabetAI commented Dec 13, 2022

Copy link
Copy Markdown

@ShaheedHaque @itamarst yes! In particular, Celery multi-processing doesn't play well with CUDA. Know any fixes?

@auvipy

auvipy commented Dec 13, 2022

Copy link
Copy Markdown
Member

I am not sure celery multiprocessing was designed to work with CUDA. you should try Dask / Ray for that I think

@adampl

adampl commented Dec 15, 2022 •

Copy link
Copy Markdown

It does work with CUDA, you just have to make 100% sure that no CUDA code is executed by the root (parent) process :)

@sabetAI

sabetAI commented Dec 20, 2022

Copy link
Copy Markdown

@adampl interesting, can you share a link so I can read up further on that? Do I disable CUDA on parent by running torch code only if os.getpid != os.getppid()?

@adampl

adampl commented Dec 20, 2022

Copy link
Copy Markdown

I just import all the CUDA-related stuff inside the task functions. The trick with PIDs may work too. That way no CUDA context is ever initialized in the parent process. I also use the Celery Max Tasks Per Child=1 setting so that the child process is deleted when the task is finished, to ensure full cleanup.

@veonua

veonua commented Feb 15, 2023

Copy link
Copy Markdown

deleted when the task is finished, to ensure full cleanup.

some models are 10+ Gb, sometimes it can be very costly to do cleanup after every task

@adampl

adampl commented Feb 19, 2023

Copy link
Copy Markdown

sometimes it can be very costly to do cleanup after every task

Yes, but unfortunately many libraries don't properly manage GPU resources, causing subsequent tasks to fail.

@auvipy

auvipy commented Jul 9, 2025

Copy link
Copy Markdown
Member

hey good peoples! we got a new approach here to fix it #9810 . please check it out

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow celery to spawn rather than fork

9 participants