Skip to content

Commit 43d4c03

Browse files
authored
bpo-30301: Fix AttributeError when using SimpleQueue.empty() (#1601) (#1628)
Under *spawn* and *forkserver* start methods, SimpleQueue.empty() could raise AttributeError due to not setting _poll in __setstate__.
1 parent b769c91 commit 43d4c03

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

Lib/multiprocessing/queues.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@ def __getstate__(self):
337337

338338
def __setstate__(self, state):
339339
(self._reader, self._writer, self._rlock, self._wlock) = state
340+
self._poll = self._reader.poll
340341

341342
def get(self):
342343
with self._rlock:

Lib/test/_test_multiprocessing.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3958,6 +3958,42 @@ def test_semaphore_tracker(self):
39583958
self.assertRegex(err, expected)
39593959
self.assertRegex(err, r'semaphore_tracker: %r: \[Errno' % name1)
39603960

3961+
class TestSimpleQueue(unittest.TestCase):
3962+
3963+
@classmethod
3964+
def _test_empty(cls, queue, child_can_start, parent_can_continue):
3965+
child_can_start.wait()
3966+
# issue 30301, could fail under spawn and forkserver
3967+
try:
3968+
queue.put(queue.empty())
3969+
queue.put(queue.empty())
3970+
finally:
3971+
parent_can_continue.set()
3972+
3973+
def test_empty(self):
3974+
queue = multiprocessing.SimpleQueue()
3975+
child_can_start = multiprocessing.Event()
3976+
parent_can_continue = multiprocessing.Event()
3977+
3978+
proc = multiprocessing.Process(
3979+
target=self._test_empty,
3980+
args=(queue, child_can_start, parent_can_continue)
3981+
)
3982+
proc.daemon = True
3983+
proc.start()
3984+
3985+
self.assertTrue(queue.empty())
3986+
3987+
child_can_start.set()
3988+
parent_can_continue.wait()
3989+
3990+
self.assertFalse(queue.empty())
3991+
self.assertEqual(queue.get(), True)
3992+
self.assertEqual(queue.get(), False)
3993+
self.assertTrue(queue.empty())
3994+
3995+
proc.join()
3996+
39613997
#
39623998
# Mixins
39633999
#

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ Core and Builtins
3636
Library
3737
-------
3838

39+
- bpo-30301: Fix AttributeError when using SimpleQueue.empty() under
40+
*spawn* and *forkserver* start methods.
41+
3942
- bpo-30329: imaplib and poplib now catch the Windows socket WSAEINVAL error
4043
(code 10022) on shutdown(SHUT_RDWR): An invalid operation was attempted.
4144
This error occurs sometimes on SSL connections.

0 commit comments

Comments
 (0)