[3.13] gh-135335: flush stdout/stderr in forkserver after preloading modules (GH-135338) (#135671)

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>
This commit is contained in:
Miss Islington (bot) 2025-06-18 14:40:21 +02:00 committed by GitHub
parent 28a70a59e4
commit a0a1aa3125
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 50 additions and 0 deletions

View file

@ -182,6 +182,10 @@ def main(listener_fd, alive_r, preload, main_path=None, sys_path=None):
except ImportError:
pass
# gh-135335: flush stdout/stderr in case any of the preloaded modules
# wrote to them, otherwise children might inherit buffered data
util._flush_std_streams()
util._close_stdin()
sig_r, sig_w = os.pipe()