Repository navigation
Description
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 same feature was already implemented in the
main 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
preforkis not safe anymore #10339 and Allow worker_pool_start_method='spawn' with the asynchronous prefork pool #10584 addedworker_pool_start_methodoptions of eitherforkorspawn- Is there any way to set multiprocess start method #5005 and Celery can not select start_method of multiprocessing #5021 asked for a way to choose the start method, including forkserver
- Consider porting python 3.4 forkserver into billiard billiard#127 from 2014 asked for
forkserversupport and was completed when billiard addedforkserversupport - forkserver start method not available billiard#200 is open but is specific to Python 2
Possible Duplicates
- None found
Brief Summary
This enhancement adds forkserver as an opt-in start method for Celery’s prefork pool, alongside fork and spawn.
fork offers fast startup and memory sharing but has the risk of inheriting fork-unsafe state. spawn provides cleaner initialization at the cost of higher startup latency and memory overhead. Users must therefore choose one approach and accept its trade-offs.
forkserver keeps the benefits of fork while making it safer:
- Like
fork, workers start quickly and share memory, because they are copied from a fork server that has already preloaded the application. - Unlike
fork, workers are copied from that server rather than the running parent, so they never inherit what the parent or its libraries set up later, such as gRPC clients, which gRPC Python "doesn't support" acrossfork()(see grpc/grpc#31885 and Allow celery to spawn rather than fork #6036).
Further motivation for adding Celery forkserver support is that Python 3.14 made forkserver the default start method on Linux, reinforcing the case for making it a supported option in Celery.
Design
Proposed UI/UX
Users can select forkserver through worker_pool_start_method or the existing --pool-start-method CLI option. No new hooks are proposed. The existing worker_process_init hook can be used to defer resource setup until after forking, and worker_process_shutdown for best-effort cleanup.
A new optional setting lets users list modules to load into the fork server before it creates workers. An empty list means no application preloading.
| Configuration setting | CLI option | Default | Example | Constraints |
|---|---|---|---|---|
worker_forkserver_preload |
--forkserver-preload |
[] |
['myapp.preload'] |
Only valid with forkserver. |
Architectural Considerations
Proposed billiard changes
- Billiard already provides
forkserver. The proposal is to extendbilliardto enable Celery support. - File descriptor handling
- For
forkserverto work reliably, the Celery parent worker needs to know the correct file descriptor numbers to acknowledge tasks and mark the right worker as available. Underforkservertoday, they can differ between parent and child, as was observed inpreforkis not safe anymore #10339, "With forkserver they get renumbered, and I can reproduce the breakage there". - They differ because
fdnumbers are per process, and underforkserverthe child receives the pipe over a Unix socket rather than inheriting it, so the kernel assigns it the child's lowest free number, which can differ from the parent's. - The proposal is for billiard to preserve identifiers that Celery can recognize on the parent side.
- For
- Fork server recovery
- Billiard currently does not detect or replace a dead server. Once it dies, attempts to create replacement workers fail, because nothing is listening on the dead server's socket.
- The proposal is for billiard to detect server death and restart the server.
Keeping existing users unaffected
- Current
forkandspawnbehavior will be preserved to reduce disruption. - The new mode is proposed as opt-in, with no change to existing multiprocessing defaults.
Unsupported platforms
- Support will only be added for Linux and macOS. Windows will be left unsupported since it only supports
spawnas of today.
Proposed Behavior
Matching worker identifiers in parent and child
- Today, the child works out its worker identifiers from its own file descriptor table, which under
forkservergives different numbers from the parent's. - The proposal is for billiard to send the parent's numbers to the child in
Worker.__reduce__, so the child does not infer them. The child then reliably reports the numbers the parent expects:inqW_fdinREADYmessages andsynqW_fdinACKmessages.
Child creation, once per child
sequenceDiagram
participant CE as Parent (Celery)
participant BP as Parent (Billiard)
participant BC as Child (Billiard)
Note over BP: creates inq pipe, write end is fd 4
BP->>CE: new worker, identifier 4
BP->>BC: pipe sent over Unix socket
Note over BC: same pipe arrives as fd 10
BP->>BC: inqW_fd = 4 sent with the worker
Running a task, repeated for every task
sequenceDiagram
participant CE as Parent (Celery)
participant BP as Parent (Billiard)
participant BC as Child (Billiard)
Note over BC: holds inqW_fd = 4 from creation
CE->>BC: task A, Celery marks 4 busy
BC->>BP: READY with inqW_fd = 4
BP->>CE: worker 4 is ready
Note over CE: 4 removed from busy set, child is free
Recovering from fork server death
- The proposal is to port CPython’s existing on-demand recovery into billiard, rather than introduce a separate monitoring mechanism.
- Before creating a child, billiard's
ForkServer.ensure_runningwill check whether theforkserverprocess is still alive. - If it is, the existing server will be reused. If the server has exited, billiard will reap it, close stale descriptors and clear the cached server state before launching a replacement.
- If fork server recovery fails, the parent will continue serving tasks through any surviving healthy children. Failed child creation will be cleaned up and retried during subsequent pool maintenance.
sequenceDiagram
participant CE as Parent (Celery)
participant BP as Parent (Billiard)
participant FS as Fork server (Billiard)
Note over FS: fork server dies
Note over CE: surviving children keep serving tasks
Note over CE,FS: Next time a child is needed
CE->>BP: maintain the pool
Note over BP: checks the server, finds it dead
Note over BP: reaps it, closes stale descriptors, clears cached state
BP->>FS: launches a replacement server
alt launch succeeds
BP->>FS: requests a child
else launch fails
Note over BP: cleans up, retries at next pool maintenance
end
Preloading modules into the fork server
- Today, Celery has no way to tell the fork server which modules to load, so each child imports the application itself, which is about as slow as
spawn. - The proposal is for Celery to pass the modules listed in
worker_forkserver_preloadto billiard's existingset_forkserver_preload, fromTaskPool.on_start, where the start method is set today. The fork server imports them once, before it creates any children, and every child inherits them.
Reloading code with pool restarts
- With
forkandspawn, a pool restart reloads task modules in the parent and replaces the children, so new children run the new code. Pool restarts are opt-in throughworker_pool_restarts. - The proposal is for a pool restart, in billiard's
Pool.restart, to also replace the fork server.
sequenceDiagram
participant CE as Parent (Celery)
participant BP as Parent (Billiard)
participant FS as Fork server (Billiard)
participant CH as Child (Billiard)
Note over CE,CH: User sends pool_restart with reload
Note over CE: reloads task modules, parent has new code
Note over FS: still has old code from startup
CE->>BP: restart the pool
BP->>FS: stops the old fork server
BP->>FS: starts a new fork server
Note over FS: imports preload modules again, now new code
BP->>FS: request a replacement child
FS->>CH: fork
Note over CH: inherits the new code
Alternatives
Fixing the worker identifiers in Celery instead of billiard
- Celery could override billiard's worker packing to send the parent's numbers itself. This works, but the packing logic lives in billiard, so a Celery override would depend on billiard's internals and could silently break whenever billiard changes them.
Monitoring the fork server continuously
- A background thread or event-loop timer could watch the fork server and restart it as soon as it dies. Not proposed, because checking on demand is simpler, needs no extra thread or timer, and follows the approach CPython already uses.
Shutting the worker down when fork server recovery fails
- The worker could stop with a clear error and rely on a process manager to restart it.
- Not proposed, because the surviving children are still healthy and can keep serving tasks. The trade-off is that if recovery keeps failing, the pool shrinks each time a child is recycled.
Activity
auvipy commentedon Nov 14, 2026
here is already a open issue for this celery/billiard#200 may be you should start contributing
tzifudzi commentedon Nov 14, 2026
@auvipy On Python 3, billiard already has
forkserversupport. celery/billiard#200 is for Python 2.Good idea thanks, I read contribution guidelines and will get started on this and propose a design and prototype for review.
tzifudzi commentedon Nov 22, 2026
I tested the proposed billiard fix locally by applying it as an override in a reproduction script, so it can be tried without changing billiard.
Worker.__reduce__is how billiard sends the worker to the child. Returning the parent's identifiers as state makes the child use them, instead of the numbers it works out from its own fd table:forkserverforkserver,synack=Trueforkspawnforkandspawnare unaffected.Tested with Celery 5.7.0 and unmodified billiard 4.3.2 on macOS arm64, Python 3.14.6.
Full script
Run with
METHOD=forkserver python repro.py, then addFIX=1and optionallySYNACK=1.tzifudzi commentedon Nov 22, 2026
@auvipy Following up on this, I've updated the description with the proposed design, and posted local test results in above comment. Could you take a look and let me know if you agree with the direction before I start implementing?
auvipy commentedon Nov 22, 2026
come with a pr and mention me
Restart the fork server if it dies
Restart the fork server if it dies
Restart the fork server if it dies
Restart the fork server if it dies
Restart the fork server if it dies