Skip to content

Conversation

moycat
Copy link

@moycat moycat commented Oct 3, 2025

This fixes issue #86354 that a child process does not reuse the forkserver process started by its parent. Currently, each child process creates its own forkserver for its own child processes:

import multiprocessing as mp
import os

mp.set_start_method("forkserver", force=True)


def child(remaining):
    print(f"PID: {os.getpid()}, PPID: {os.getppid()}")
    remaining -= 1
    if remaining > 0:
        proc = mp.Process(target=child, args=(remaining,))
        proc.start()
        proc.join()


def main():
    proc = mp.Process(target=child, args=(5,))
    proc.start()
    proc.join()


if __name__ == '__main__':
    main()
PID: 60016, PPID: 60015
PID: 60018, PPID: 60017
PID: 60020, PPID: 60019
PID: 60022, PPID: 60021
PID: 60024, PPID: 60023

With this patch, all the descendants reuse the forkserver spawned by the main process, reducing the overhead when the process tree is deep:

PID: 60054, PPID: 60053
PID: 60055, PPID: 60053
PID: 60056, PPID: 60053
PID: 60057, PPID: 60053
PID: 60058, PPID: 60053

The new behavior matches the comment in forkserver.py:

def ensure_running(self):
'''Make sure that a fork server is running.
This can be called from any process. Note that usually a child
process will just reuse the forkserver started by its parent, so
ensure_running() will do nothing.
'''

@moycat moycat requested a review from gpshead as a code owner October 3, 2025 06:21
@python-cla-bot
Copy link

python-cla-bot bot commented Oct 3, 2025

All commit authors signed the Contributor License Agreement.

CLA signed

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.

1 participant