bpo-34651: Only allow the main interpreter to fork. (gh-9279)

When os.fork() is called (on platforms that support it) all threads but the current one are destroyed in the child process. Consequently we must ensure that all but the associated interpreter are likewise destroyed. The main interpreter is critical for runtime operation, so we must ensure that fork only happens in the main interpreter.

https://bugs.python.org/issue34651
This commit is contained in:
Eric Snow 2018-09-14 14:17:20 -07:00 committed by GitHub
parent 3faaa8857a
commit 5903296045
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 64 additions and 14 deletions

View file

@ -824,23 +824,12 @@ class RunStringTests(TestBase):
expected = 'spam spam spam spam spam'
script = dedent(f"""
# (inspired by Lib/test/test_fork.py)
import os
pid = os.fork()
if pid == 0: # child
try:
os.fork()
except RuntimeError:
with open('{file.name}', 'w') as out:
out.write('{expected}')
# Kill the unittest runner in the child process.
os._exit(1)
else:
SHORT_SLEEP = 0.1
import time
for _ in range(10):
spid, status = os.waitpid(pid, os.WNOHANG)
if spid == pid:
break
time.sleep(SHORT_SLEEP)
assert(spid == pid)
""")
interpreters.run_string(self.id, script)