Commit c99d41f9 authored by Victor Stinner's avatar Victor Stinner Committed by GitHub

bpo-31234: fork_wait tests now join threads (#3139)

fork_wait.py tests now joins threads, to not leak running threads in
the background.
parent d73a960c
...@@ -11,7 +11,8 @@ active threads survive in the child after a fork(); this is an error. ...@@ -11,7 +11,8 @@ active threads survive in the child after a fork(); this is an error.
import os, sys, time, unittest import os, sys, time, unittest
import test.support as support import test.support as support
_thread = support.import_module('_thread')
threading = support.import_module('threading')
LONGSLEEP = 2 LONGSLEEP = 2
SHORTSLEEP = 0.5 SHORTSLEEP = 0.5
...@@ -20,8 +21,19 @@ NUM_THREADS = 4 ...@@ -20,8 +21,19 @@ NUM_THREADS = 4
class ForkWait(unittest.TestCase): class ForkWait(unittest.TestCase):
def setUp(self): def setUp(self):
self._threading_key = support.threading_setup()
self.alive = {} self.alive = {}
self.stop = 0 self.stop = 0
self.threads = []
def tearDown(self):
# Stop threads
self.stop = 1
for thread in self.threads:
thread.join()
thread = None
self.threads.clear()
support.threading_cleanup(*self._threading_key)
def f(self, id): def f(self, id):
while not self.stop: while not self.stop:
...@@ -43,10 +55,11 @@ class ForkWait(unittest.TestCase): ...@@ -43,10 +55,11 @@ class ForkWait(unittest.TestCase):
self.assertEqual(spid, cpid) self.assertEqual(spid, cpid)
self.assertEqual(status, 0, "cause = %d, exit = %d" % (status&0xff, status>>8)) self.assertEqual(status, 0, "cause = %d, exit = %d" % (status&0xff, status>>8))
@support.reap_threads
def test_wait(self): def test_wait(self):
for i in range(NUM_THREADS): for i in range(NUM_THREADS):
_thread.start_new(self.f, (i,)) thread = threading.Thread(target=self.f, args=(i,))
thread.start()
self.threads.append(thread)
# busy-loop to wait for threads # busy-loop to wait for threads
deadline = time.monotonic() + 10.0 deadline = time.monotonic() + 10.0
...@@ -75,8 +88,4 @@ class ForkWait(unittest.TestCase): ...@@ -75,8 +88,4 @@ class ForkWait(unittest.TestCase):
os._exit(n) os._exit(n)
else: else:
# Parent # Parent
try: self.wait_impl(cpid)
self.wait_impl(cpid)
finally:
# Tell threads to die
self.stop = 1
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment