Skip to content

gh-86354: Fix child processes not reusing forkserver created by parent - #139537

Open
moycat wants to merge 1 commit into
python:mainfrom
moycat:fix-issue-86354
Open

moycat wants to merge 1 commit into
python:mainfrom
moycat:fix-issue-86354

Conversation

@moycat

@moycat moycat commented Oct 3, 2025 •

Copy link
Copy Markdown

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

python-cla-bot Bot commented Oct 3, 2025 •

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

@github-actions

Copy link
Copy Markdown

This PR is stale because it has been open for 30 days with no activity.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label Apr 30, 2026
@doublex

doublex commented Apr 30, 2026

Copy link
Copy Markdown

relevant

@github-actions github-actions Bot removed the stale Stale PR or inactive for long period of time. label May 16, 2026
@github-actions

Copy link
Copy Markdown

This PR is stale because it has been open for 90 days with no activity.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label Aug 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting review stale Stale PR or inactive for long period of time.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants