gh-83856: Honor atexit for all multiprocessing start methods (GH-114279)

Use atexit for all multiprocessing start methods to cleanup.
See the GH-114279 PR discussion and related issue for details as to why.
This commit is contained in:
Tian Gao 2024-05-03 11:45:46 -07:00 committed by GitHub
parent cb57a52a85
commit 998c3856c1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 34 additions and 5 deletions

View file

@ -1,3 +1,4 @@
import atexit
import errno
import os
import selectors
@ -271,6 +272,8 @@ def main(listener_fd, alive_r, preload, main_path=None, sys_path=None):
selector.close()
unused_fds = [alive_r, child_w, sig_r, sig_w]
unused_fds.extend(pid_to_fd.values())
atexit._clear()
atexit.register(util._exit_function)
code = _serve_one(child_r, fds,
unused_fds,
old_handlers)
@ -278,6 +281,7 @@ def main(listener_fd, alive_r, preload, main_path=None, sys_path=None):
sys.excepthook(*sys.exc_info())
sys.stderr.flush()
finally:
atexit._run_exitfuncs()
os._exit(code)
else:
# Send pid to client process

View file

@ -1,3 +1,4 @@
import atexit
import os
import signal
@ -66,10 +67,13 @@ class Popen(object):
self.pid = os.fork()
if self.pid == 0:
try:
atexit._clear()
atexit.register(util._exit_function)
os.close(parent_r)
os.close(parent_w)
code = process_obj._bootstrap(parent_sentinel=child_r)
finally:
atexit._run_exitfuncs()
os._exit(code)
else:
os.close(child_w)

View file

@ -310,11 +310,8 @@ class BaseProcess(object):
# _run_after_forkers() is executed
del old_process
util.info('child process calling self.run()')
try:
self.run()
exitcode = 0
finally:
util._exit_function()
self.run()
exitcode = 0
except SystemExit as e:
if e.code is None:
exitcode = 0