Debian Codegraph @

Lib/test/mp_preload_large_sysargv.py

raw
1 # gh-144503: Test that the forkserver can start when the parent process has
2 # a very large sys.argv. Prior to the fix, sys.argv was repr'd into the
3 # forkserver ``-c`` command string which could exceed the OS limit on the
4 # length of a single argv element (MAX_ARG_STRLEN on Linux, ~128 KiB),
5 # causing posix_spawn to fail and the parent to see a BrokenPipeError.
6
7 import multiprocessing
8 import sys
9
10 EXPECTED_LEN = 5002 # argv[0] + 5000 padding entries + sentinel
11
12
13 def fun():
14 print(f"worker:{len(sys.argv)}:{sys.argv[-1]}")
15
16
17 if __name__ == "__main__":
18 # Inflate sys.argv well past 128 KiB before the forkserver is started.
19 sys.argv[1:] = ["x" * 50] * 5000 + ["sentinel"]
20 assert len(sys.argv) == EXPECTED_LEN
21
22 ctx = multiprocessing.get_context("forkserver")
23 p = ctx.Process(target=fun)
24 p.start()
25 p.join()
26 sys.exit(p.exitcode)
27 else:
28 # This branch runs when the forkserver preloads this module as
29 # __mp_main__; confirm the large argv was propagated intact.
30 print(f"preload:{len(sys.argv)}:{sys.argv[-1]}")