[New-bugs-announce] [issue39675] forked process in multiprocessing does not honour atexit

gaborbernat report at bugs.python.org
Tue Feb 18 06:35:55 EST 2020

New submission from gaborbernat <gaborjbernat at gmail.com>:

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()}")


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

if __name__ == "__main__":
    process = Process(target=run)
    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

messages: 362197
nosy: davin, gaborbernat, pablogsal, pitrou
priority: normal
severity: normal
status: open
title: forked process in multiprocessing does not honour atexit

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list