]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-30301: Fix AttributeError when using SimpleQueue.empty() (#1601) (#1628)
authorXiang Zhang <angwerzx@126.com>
Wed, 17 May 2017 14:03:35 +0000 (22:03 +0800)
committerGitHub <noreply@github.com>
Wed, 17 May 2017 14:03:35 +0000 (22:03 +0800)
Under *spawn* and *forkserver* start methods, SimpleQueue.empty() could
raise AttributeError due to not setting _poll in __setstate__.

Lib/multiprocessing/queues.py
Lib/test/_test_multiprocessing.py
Misc/NEWS

index dda03ddf5425ceebf93f9ff75ed362dc32c08a04..a4f4ef8b7b5334ea88efb97690de8abe59cd4b75 100644 (file)
@@ -337,6 +337,7 @@ class SimpleQueue(object):
 
     def __setstate__(self, state):
         (self._reader, self._writer, self._rlock, self._wlock) = state
+        self._poll = self._reader.poll
 
     def get(self):
         with self._rlock:
index 57ada127465d7589783cd46dde7f768f21a4efbf..3eb83c59ab17d70c62957cc623083728d55eeeb9 100644 (file)
@@ -3958,6 +3958,42 @@ class TestSemaphoreTracker(unittest.TestCase):
         self.assertRegex(err, expected)
         self.assertRegex(err, r'semaphore_tracker: %r: \[Errno' % name1)
 
+class TestSimpleQueue(unittest.TestCase):
+
+    @classmethod
+    def _test_empty(cls, queue, child_can_start, parent_can_continue):
+        child_can_start.wait()
+        # issue 30301, could fail under spawn and forkserver
+        try:
+            queue.put(queue.empty())
+            queue.put(queue.empty())
+        finally:
+            parent_can_continue.set()
+
+    def test_empty(self):
+        queue = multiprocessing.SimpleQueue()
+        child_can_start = multiprocessing.Event()
+        parent_can_continue = multiprocessing.Event()
+
+        proc = multiprocessing.Process(
+            target=self._test_empty,
+            args=(queue, child_can_start, parent_can_continue)
+        )
+        proc.daemon = True
+        proc.start()
+
+        self.assertTrue(queue.empty())
+
+        child_can_start.set()
+        parent_can_continue.wait()
+
+        self.assertFalse(queue.empty())
+        self.assertEqual(queue.get(), True)
+        self.assertEqual(queue.get(), False)
+        self.assertTrue(queue.empty())
+
+        proc.join()
+
 #
 # Mixins
 #
index 0459e8490048c9324d74f47b885b06de522336b9..6336e67ee48edbbbe6dd7be6b30d75bbc4eb220f 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -36,6 +36,9 @@ Core and Builtins
 Library
 -------
 
+- bpo-30301: Fix AttributeError when using SimpleQueue.empty() under
+  *spawn* and *forkserver* start methods.
+
 - bpo-30329: imaplib and poplib now catch the Windows socket WSAEINVAL error
   (code 10022) on shutdown(SHUT_RDWR): An invalid operation was attempted.
   This error occurs sometimes on SSL connections.