Skip to content

WIP: Replace the fork server on pool restart - #485

Draft
tzifudzi wants to merge 2 commits into
celery:mainfrom
tzifudzi:forkserver-pool-restart
Draft

tzifudzi wants to merge 2 commits into
celery:mainfrom
tzifudzi:forkserver-pool-restart

Conversation

@tzifudzi

@tzifudzi tzifudzi commented Nov 28, 2026 •

Copy link
Copy Markdown

PR is still in draft and not yet ready for review, awaiting dependant changes to be reviewed and merge first.

Part of enhancement Support forkserver as a worker_pool_start_method celery/celery#11096.

Problem

Pool.restart() recycles workers, but under forkserver their replacements inherit any preloaded modules from the same server. Those modules still contain the code loaded when the server started.

Fix

Start a fresh fork server before signaling the existing workers to exit. Route new worker requests to the new server.

Related PRs

Report the parent's fd numbers from forkserver pool workers celery/billiard #483
Restart the fork server if it dies celery/billiard #484
Support forkserver as a prefork start method celery/celery #11177
Support preloading modules in the fork server celery/celery #11178

The fork server is started once and never checked again. If it dies,
every later attempt to start a process fails with ConnectionRefusedError,
because nothing listens on its socket any more, so a pool can no longer
replace its workers.

Before asking for a new process, check whether the fork server has
exited, as CPython does (python/cpython#75489). If it has, forget it and
start a new one. If that start fails, the next call tries again.

Only the process that started the fork server can check on it. A process
the server started, or a forked copy of the one that started it, keeps
using it without checking, as billiard does today. CPython instead starts
a second fork server in the first case and raises ChildProcessError in
the second.

A pool worker that failed to start stayed in the pool, and cleaning it up
at the next maintenance raised KeyError because it was never registered
under a pid. Clean it up without raising, so the pool goes on to start a
replacement.

Part of celery/celery#11096
Route new workers to a fresh fork server before recycling the pool so they inherit the current preloaded code. Let the retired server exit on its own: stopping it early can kill workers on Linux through their parent-death signal.

Serialize the switch with worker startup and reap the retired server after its last client exits.

Part of celery/celery#11096
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant