Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9ac369c

Browse files
miss-islingtonduanegefimov-mikhailvstinner
authored
[3.14] gh-135335: flush stdout/stderr in forkserver after preloading modules (GH-135338) (#135670)
gh-135335: flush stdout/stderr in forkserver after preloading modules (GH-135338) If a preloaded module writes to stdout or stderr, and the stream is buffered, child processes will inherit the buffered data after forking. Attempt to prevent this by flushing the streams after preload. (cherry picked from commit 9877d19) Co-authored-by: Duane Griffin <[email protected]> Co-authored-by: Mikhail Efimov <[email protected]> Co-authored-by: Victor Stinner <[email protected]>
1 parent 91914fd commit 9ac369c

File tree

4 files changed

+50
-0
lines changed

4 files changed

+50
-0
lines changed

Lib/multiprocessing/forkserver.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,10 @@ def main(listener_fd, alive_r, preload, main_path=None, sys_path=None,
222222
except ImportError:
223223
pass
224224

225+
# gh-135335: flush stdout/stderr in case any of the preloaded modules
226+
# wrote to them, otherwise children might inherit buffered data
227+
util._flush_std_streams()
228+
225229
util._close_stdin()
226230

227231
sig_r, sig_w = os.pipe()

Lib/test/_test_multiprocessing.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6789,6 +6789,35 @@ def test_child_sys_path(self):
67896789
self.assertEqual(child_sys_path[1:], sys.path[1:])
67906790
self.assertIsNone(import_error, msg=f"child could not import {self._mod_name}")
67916791

6792+
def test_std_streams_flushed_after_preload(self):
6793+
# gh-135335: Check fork server flushes standard streams after
6794+
# preloading modules
6795+
if multiprocessing.get_start_method() != "forkserver":
6796+
self.skipTest("forkserver specific test")
6797+
6798+
# Create a test module in the temporary directory on the child's path
6799+
# TODO: This can all be simplified once gh-126631 is fixed and we can
6800+
# use __main__ instead of a module.
6801+
dirname = os.path.join(self._temp_dir, 'preloaded_module')
6802+
init_name = os.path.join(dirname, '__init__.py')
6803+
os.mkdir(dirname)
6804+
with open(init_name, "w") as f:
6805+
cmd = '''if 1:
6806+
import sys
6807+
print('stderr', end='', file=sys.stderr)
6808+
print('stdout', end='', file=sys.stdout)
6809+
'''
6810+
f.write(cmd)
6811+
6812+
name = os.path.join(os.path.dirname(__file__), 'mp_preload_flush.py')
6813+
env = {'PYTHONPATH': self._temp_dir}
6814+
_, out, err = test.support.script_helper.assert_python_ok(name, **env)
6815+
6816+
# Check stderr first, as it is more likely to be useful to see in the
6817+
# event of a failure.
6818+
self.assertEqual(err.decode().rstrip(), 'stderr')
6819+
self.assertEqual(out.decode().rstrip(), 'stdout')
6820+
67926821

67936822
class MiscTestCase(unittest.TestCase):
67946823
def test__all__(self):

Lib/test/mp_preload_flush.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import multiprocessing
2+
import sys
3+
4+
modname = 'preloaded_module'
5+
if __name__ == '__main__':
6+
if modname in sys.modules:
7+
raise AssertionError(f'{modname!r} is not in sys.modules')
8+
multiprocessing.set_start_method('forkserver')
9+
multiprocessing.set_forkserver_preload([modname])
10+
for _ in range(2):
11+
p = multiprocessing.Process()
12+
p.start()
13+
p.join()
14+
elif modname not in sys.modules:
15+
raise AssertionError(f'{modname!r} is not in sys.modules')
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
:mod:`multiprocessing`: Flush ``stdout`` and ``stderr`` after preloading
2+
modules in the ``forkserver``.

0 commit comments

Comments
 (0)