From 13ed830ad7dcb3f9c2a071c267d5301d1280e632 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Lapeyre?= Date: Mon, 4 May 2020 17:19:56 +0200 Subject: [PATCH] Use threading.Event for the race condition --- Lib/test/_test_multiprocessing.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/Lib/test/_test_multiprocessing.py b/Lib/test/_test_multiprocessing.py index fe0443e9093..b60c6a0536d 100644 --- a/Lib/test/_test_multiprocessing.py +++ b/Lib/test/_test_multiprocessing.py @@ -4394,9 +4394,11 @@ class _TestPollEintr(BaseTestCase): # class SlowPicklable: + def __init__(self, event): + self.event = event + def __reduce__(self): - from time import sleep - sleep(1) + self.event.wait() return (SlowPicklable, ()) @@ -4417,9 +4419,9 @@ class TestInvalidHandle(unittest.TestCase): self.assertRaises((ValueError, OSError), multiprocessing.connection.Connection, -1) - def race_condition(self, parent): + def race_condition(self, parent, event): try: - parent.send(SlowPicklable()) + parent.send(SlowPicklable(event)) except Exception as e: # It's not possible to mark a test failed in a thread so we send # the exception back to the main thread. @@ -4430,9 +4432,11 @@ class TestInvalidHandle(unittest.TestCase): def test_closed_handled(self): parent, child = multiprocessing.Pipe() - t = threading.Thread(target=self.race_condition, args=(parent,)) + e = threading.Event() + t = threading.Thread(target=self.race_condition, args=(parent, e)) t.start() parent.close() + e.set() t.join() self.assertIsInstance(self.exc, OSError) self.assertEqual(str(self.exc), "handle is closed")