Skip to content

gh-83856: Honor atexit for all multiprocessing start methods #114279

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

Merged
merged 6 commits into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Lib/multiprocessing/forkserver.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import atexit
import errno
import os
import selectors
Expand Down Expand Up @@ -271,13 +272,16 @@ def sigchld_handler(*_unused):
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)
except Exception:
sys.excepthook(*sys.exc_info())
sys.stderr.flush()
finally:
atexit._run_exitfuncs()
os._exit(code)
else:
# Send pid to client process
Expand Down
4 changes: 4 additions & 0 deletions Lib/multiprocessing/popen_fork.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import atexit
import os
import signal

Expand Down Expand Up @@ -66,10 +67,13 @@ def _launch(self, process_obj):
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)
Expand Down
7 changes: 2 additions & 5 deletions Lib/multiprocessing/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,11 +310,8 @@ def _bootstrap(self, parent_sentinel=None):
# _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
Expand Down
23 changes: 23 additions & 0 deletions Lib/test/_test_multiprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -6083,6 +6083,29 @@ def submain(): pass
self.assertFalse(err, msg=err.decode('utf-8'))


class _TestAtExit(BaseTestCase):

ALLOWED_TYPES = ('processes',)

@classmethod
def _write_file_at_exit(self, output_path):
import atexit
def exit_handler():
with open(output_path, 'w') as f:
f.write("deadbeef")
atexit.register(exit_handler)

def test_atexit(self):
# gh-83856
with os_helper.temp_dir() as temp_dir:
output_path = os.path.join(temp_dir, 'output.txt')
p = self.Process(target=self._write_file_at_exit, args=(output_path,))
p.start()
p.join()
with open(output_path) as f:
self.assertEqual(f.read(), 'deadbeef')


class MiscTestCase(unittest.TestCase):
def test__all__(self):
# Just make sure names in not_exported are excluded
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Honor :mod:`atexit` for all :mod:`multiprocessing` start methods