Skip to content

bpo-40094: Enhance fork and wait tests #19259

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Lib/test/fork_wait.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ def f(self, id):
except OSError:
pass

def wait_impl(self, cpid):
support.wait_process(cpid, exitcode=0)
def wait_impl(self, cpid, *, exitcode):
support.wait_process(cpid, exitcode=exitcode)

def test_wait(self):
for i in range(NUM_THREADS):
Expand Down Expand Up @@ -79,4 +79,4 @@ def test_wait(self):
os._exit(n)
else:
# Parent
self.wait_impl(cpid)
self.wait_impl(cpid, exitcode=0)
23 changes: 6 additions & 17 deletions Lib/test/test_fork1.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,6 @@
support.get_attribute(os, 'fork')

class ForkTest(ForkWait):
def wait_impl(self, cpid):
deadline = time.monotonic() + support.SHORT_TIMEOUT
while time.monotonic() <= deadline:
# waitpid() shouldn't hang, but some of the buildbots seem to hang
# in the forking tests. This is an attempt to fix the problem.
spid, status = os.waitpid(cpid, os.WNOHANG)
if spid == cpid:
break
time.sleep(0.1)

self.assertEqual(spid, cpid)
self.assertEqual(status, 0, "cause = %d, exit = %d" % (status&0xff, status>>8))

def test_threaded_import_lock_fork(self):
"""Check fork() in main thread works while a subthread is doing an import"""
import_started = threading.Event()
Expand All @@ -46,6 +33,7 @@ def importer():
t = threading.Thread(target=importer)
t.start()
import_started.wait()
exitcode = 42
pid = os.fork()
try:
# PyOS_BeforeFork should have waited for the import to complete
Expand All @@ -54,7 +42,7 @@ def importer():
if not pid:
m = __import__(fake_module_name)
if m == complete_module:
os._exit(0)
os._exit(exitcode)
else:
if support.verbose > 1:
print("Child encountered partial module")
Expand All @@ -64,7 +52,7 @@ def importer():
# Exitcode 1 means the child got a partial module (bad.) No
# exitcode (but a hang, which manifests as 'got pid 0')
# means the child deadlocked (also bad.)
self.wait_impl(pid)
self.wait_impl(pid, exitcode=exitcode)
finally:
try:
os.kill(pid, signal.SIGKILL)
Expand All @@ -74,6 +62,7 @@ def importer():

def test_nested_import_lock_fork(self):
"""Check fork() in main thread works while the main thread is doing an import"""
exitcode = 42
# Issue 9573: this used to trigger RuntimeError in the child process
def fork_with_import_lock(level):
release = 0
Expand All @@ -95,8 +84,8 @@ def fork_with_import_lock(level):
os._exit(1)
raise
if in_child:
os._exit(0)
self.wait_impl(pid)
os._exit(exitcode)
self.wait_impl(pid, exitcode=exitcode)

# Check this works with various levels of nested
# import in the main thread
Expand Down
5 changes: 3 additions & 2 deletions Lib/test/test_wait3.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
raise unittest.SkipTest("os.wait3 not defined")

class Wait3Test(ForkWait):
def wait_impl(self, cpid):
def wait_impl(self, cpid, *, exitcode):
# This many iterations can be required, since some previously run
# tests (e.g. test_ctypes) could have spawned a lot of children
# very quickly.
Expand All @@ -30,7 +30,8 @@ def wait_impl(self, cpid):
time.sleep(0.1)

self.assertEqual(spid, cpid)
self.assertEqual(status, 0, "cause = %d, exit = %d" % (status&0xff, status>>8))
self.assertEqual(status, exitcode << 8,
"cause = %d, exit = %d" % (status&0xff, status>>8))
self.assertTrue(rusage)

def test_wait3_rusage_initialized(self):
Expand Down
5 changes: 3 additions & 2 deletions Lib/test/test_wait4.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@


class Wait4Test(ForkWait):
def wait_impl(self, cpid):
def wait_impl(self, cpid, *, exitcode):
option = os.WNOHANG
if sys.platform.startswith('aix'):
# Issue #11185: wait4 is broken on AIX and will always return 0
Expand All @@ -29,7 +29,8 @@ def wait_impl(self, cpid):
break
time.sleep(0.1)
self.assertEqual(spid, cpid)
self.assertEqual(status, 0, "cause = %d, exit = %d" % (status&0xff, status>>8))
self.assertEqual(status, exitcode << 8,
"cause = %d, exit = %d" % (status&0xff, status>>8))
self.assertTrue(rusage)

def tearDownModule():
Expand Down