Skip to content

forked process in multiprocessing does not honour atexit #83856

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
gaborbernat mannequin opened this issue Feb 18, 2020 · 3 comments
Closed

forked process in multiprocessing does not honour atexit #83856

gaborbernat mannequin opened this issue Feb 18, 2020 · 3 comments
Assignees
Labels
stdlib Python modules in the Lib dir topic-multiprocessing type-bug An unexpected behavior, bug, or error

Comments

@gaborbernat
Copy link
Mannequin

gaborbernat mannequin commented Feb 18, 2020

BPO 39675
Nosy @pitrou, @applio, @pablogsal, @gaborbernat

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = 'https://github.com/pablogsal'
closed_at = None
created_at = <Date 2020-02-18.11:35:55.954>
labels = ['type-bug', 'library', '3.9']
title = 'forked process in multiprocessing does not honour atexit'
updated_at = <Date 2020-02-18.12:27:31.963>
user = 'https://github.com/gaborbernat'

bugs.python.org fields:

activity = <Date 2020-02-18.12:27:31.963>
actor = 'pablogsal'
assignee = 'pablogsal'
closed = False
closed_date = None
closer = None
components = ['Library (Lib)']
creation = <Date 2020-02-18.11:35:55.954>
creator = 'gaborjbernat'
dependencies = []
files = []
hgrepos = []
issue_num = 39675
keywords = []
message_count = 1.0
messages = ['362197']
nosy_count = 4.0
nosy_names = ['pitrou', 'davin', 'pablogsal', 'gaborjbernat']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'open'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue39675'
versions = ['Python 3.9']

Linked PRs

@gaborbernat
Copy link
Mannequin Author

gaborbernat mannequin commented Feb 18, 2020

I've talked with Pablo about this in person, and as advised opening the issue here now.

I've discovered that forked processes do not honour atexit registrations. See the following example code:

from multiprocessing import Process, set_start_method
import time
import os
import atexit


def cleanup():
    print(f"cleanup {os.getpid()}")


atexit.register(cleanup)


def run():
    time.sleep(0.1)
    print(f"process done {os.getpid()}")
    # atexit._run_exitfuncs()


if __name__ == "__main__":
    set_start_method("fork")
    process = Process(target=run)
    process.start()
    process.join()
    print("app finished")

In case of a forked process childs the atexit is never executed (note it works if I ran them manually at the end of the child process; so they're registered correctly). Switching to spawn method makes it work as expected. The behaviour is the same even if you call register within the child process (as opposed to being inherited during forking). Also found this StackOverflow question that mentions this https://stackoverflow.com/a/26476585. At the very least the documentation should explain this; though I'd expect atexit to be called before finalization of the fork processes (assuming the child process exits with 0 exit code). d

@pablogsal pablogsal added stdlib Python modules in the Lib dir 3.9 only security fixes type-bug An unexpected behavior, bug, or error labels Feb 18, 2020
@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
@pablogsal
Copy link
Member

CC: @gpshead @pitrou @ambv

See the PR and this comment for more context

@gpshead gpshead self-assigned this Jan 19, 2024
@gpshead gpshead moved this to In Progress in Multiprocessing issues Jan 19, 2024
gpshead pushed a commit that referenced this issue May 3, 2024
Use atexit for all multiprocessing start methods to cleanup.
See the GH-114279 PR discussion and related issue for details as to why.
@gpshead gpshead removed the 3.9 only security fixes label May 3, 2024
@gpshead
Copy link
Member

gpshead commented May 3, 2024

The discussion on the PR was useful. This is merged in time for 3.13beta1. It is a bit of a behavior change and a corner case that has existed for a long time and most code does not hit so I do not believe we should be backporting it as a bugfix despite it also being reasonably to call a bug. Thanks for the discussion and PR!

@gpshead gpshead closed this as completed May 3, 2024
@github-project-automation github-project-automation bot moved this from In Progress to Done in Multiprocessing issues May 3, 2024
SonicField pushed a commit to SonicField/cpython that referenced this issue May 8, 2024
…ythonGH-114279)

Use atexit for all multiprocessing start methods to cleanup.
See the pythonGH-114279 PR discussion and related issue for details as to why.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir topic-multiprocessing type-bug An unexpected behavior, bug, or error
Projects
Development

No branches or pull requests

3 participants