Skip to content

Commit 5f20539

Browse files
authored
bpo-31234: fork_wait tests now join threads (#3139) (#3187)
fork_wait.py tests now joins threads, to not leak running threads in the background. (cherry picked from commit c99d41f)
1 parent 29d007b commit 5f20539

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

Lib/test/fork_wait.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111

1212
import os, sys, time, unittest
1313
import test.support as support
14-
_thread = support.import_module('_thread')
14+
15+
threading = support.import_module('threading')
1516

1617
LONGSLEEP = 2
1718
SHORTSLEEP = 0.5
@@ -20,8 +21,19 @@
2021
class ForkWait(unittest.TestCase):
2122

2223
def setUp(self):
24+
self._threading_key = support.threading_setup()
2325
self.alive = {}
2426
self.stop = 0
27+
self.threads = []
28+
29+
def tearDown(self):
30+
# Stop threads
31+
self.stop = 1
32+
for thread in self.threads:
33+
thread.join()
34+
thread = None
35+
self.threads.clear()
36+
support.threading_cleanup(*self._threading_key)
2537

2638
def f(self, id):
2739
while not self.stop:
@@ -43,10 +55,11 @@ def wait_impl(self, cpid):
4355
self.assertEqual(spid, cpid)
4456
self.assertEqual(status, 0, "cause = %d, exit = %d" % (status&0xff, status>>8))
4557

46-
@support.reap_threads
4758
def test_wait(self):
4859
for i in range(NUM_THREADS):
49-
_thread.start_new(self.f, (i,))
60+
thread = threading.Thread(target=self.f, args=(i,))
61+
thread.start()
62+
self.threads.append(thread)
5063

5164
# busy-loop to wait for threads
5265
deadline = time.monotonic() + 10.0
@@ -75,8 +88,4 @@ def test_wait(self):
7588
os._exit(n)
7689
else:
7790
# Parent
78-
try:
79-
self.wait_impl(cpid)
80-
finally:
81-
# Tell threads to die
82-
self.stop = 1
91+
self.wait_impl(cpid)

0 commit comments

Comments
 (0)