Skip to content

Commit 61c948e

Browse files
committed
[3.15] gh-158975: Replace fixed sleeps in multiprocessing tests (gh-158977)
Replace fixed sleeps used for synchronization in multiprocessing tests with bounded waits to avoid failures on slow or overloaded systems. Ensure pool cleanup runs even when assertions fail in the imap buffer tests. (cherry picked from commit 75d96f8) Co-authored-by: Neil Schemenauer <nas-github@arctrix.com>
1 parent e62d391 commit 61c948e

2 files changed

Lines changed: 22 additions & 14 deletions

File tree

‎Lib/test/_test_multiprocessing.py‎

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1221,8 +1221,10 @@ def test_put(self):
12211221
queue.put(5, False, None)
12221222
queue.put_nowait(6)
12231223

1224-
# the values may be in buffer but not yet in pipe so sleep a bit
1225-
time.sleep(DELTA)
1224+
# The values may be in the buffer but not yet in the pipe.
1225+
for _ in support.sleeping_retry(support.SHORT_TIMEOUT):
1226+
if not queue_empty(queue):
1227+
break
12261228

12271229
self.assertEqual(queue_empty(queue), False)
12281230
self.assertEqual(queue_full(queue, MAXSIZE), True)
@@ -1343,8 +1345,10 @@ def test_fork(self):
13431345
for i in range(10):
13441346
queue.put(i)
13451347

1346-
# wait to make sure thread starts before we fork a new process
1347-
time.sleep(DELTA)
1348+
# Wait for the feeder thread to write to the pipe before forking.
1349+
for _ in support.sleeping_retry(support.SHORT_TIMEOUT):
1350+
if not queue_empty(queue):
1351+
break
13481352

13491353
# fork process
13501354
p = self.Process(target=self._test_fork, args=(queue,))
@@ -1567,15 +1571,15 @@ def test_repr_lock(self):
15671571
args=(lock, l),
15681572
name=tname)
15691573
t.start()
1570-
time.sleep(0.1)
1574+
threading_helper.join_thread(t)
15711575
self.assertEqual(f'<Lock(owner=MainProcess|{tname})>', l[0])
15721576
lock.release()
15731577

15741578
t = threading.Thread(target=self._acquire,
15751579
args=(lock,),
15761580
name=tname)
15771581
t.start()
1578-
time.sleep(0.1)
1582+
threading_helper.join_thread(t)
15791583
self.assertEqual('<Lock(owner=SomeOtherThread)>', repr(lock))
15801584
lock.release()
15811585

@@ -3251,8 +3255,11 @@ def test_release_task_refs(self):
32513255
self.pool.map(identity, objs)
32523256

32533257
del objs
3254-
time.sleep(DELTA) # let threaded cleanup code run
3255-
support.gc_collect() # For PyPy or other GCs.
3258+
for _ in support.sleeping_retry(support.SHORT_TIMEOUT, error=False):
3259+
support.gc_collect() # For PyPy or other GCs.
3260+
if (all(wr() is None for wr in refs)
3261+
and CountedObject.n_instances == 0):
3262+
break
32563263
self.assertEqual(set(wr() for wr in refs), {None})
32573264
# With a process pool, copies of the objects are returned, check
32583265
# they were released too.
@@ -3744,7 +3751,7 @@ def test_connection(self):
37443751
self.assertTimingAlmostEqual(poll.elapsed, TIMEOUT1)
37453752

37463753
conn.send(None)
3747-
time.sleep(.1)
3754+
self.assertTrue(conn.poll(support.SHORT_TIMEOUT))
37483755

37493756
self.assertEqual(poll(TIMEOUT1), True)
37503757
self.assertTimingAlmostEqual(poll.elapsed, 0)
@@ -4045,11 +4052,10 @@ def test_issue14725(self):
40454052
p = self.Process(target=self._test, args=(l.address,))
40464053
p.daemon = True
40474054
p.start()
4048-
time.sleep(1)
4049-
# On Windows the client process should by now have connected,
4050-
# written data and closed the pipe handle by now. This causes
4051-
# ConnectNamdedPipe() to fail with ERROR_NO_DATA. See Issue
4052-
# 14725.
4055+
join_process(p)
4056+
# On Windows the client process has now connected, written data
4057+
# and closed the pipe handle. This causes ConnectNamedPipe()
4058+
# to fail with ERROR_NO_DATA. See Issue 14725.
40534059
conn = l.accept()
40544060
self.assertEqual(conn.recv(), 'hello')
40554061
conn.close()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Replace fixed sleeps used for synchronization in multiprocessing tests with
2+
bounded waits to avoid failures on slow or overloaded systems.

0 commit comments

Comments
 (0)