cpython/Lib/test/mp_preload_flush.py
Miss Islington (bot) 9ac369cb73
[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 9877d191f4)

Co-authored-by: Duane Griffin <duaneg@dghda.com>
Co-authored-by: Mikhail Efimov <efimov.mikhail@gmail.com>
Co-authored-by: Victor Stinner <vstinner@python.org>
2025-06-18 12:42:18 +00:00

15 lines
486 B
Python

import multiprocessing
import sys
modname = 'preloaded_module'
if __name__ == '__main__':
if modname in sys.modules:
raise AssertionError(f'{modname!r} is not in sys.modules')
multiprocessing.set_start_method('forkserver')
multiprocessing.set_forkserver_preload([modname])
for _ in range(2):
p = multiprocessing.Process()
p.start()
p.join()
elif modname not in sys.modules:
raise AssertionError(f'{modname!r} is not in sys.modules')