Skip to content

Commit 6f75bc0

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

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
@@ -323,6 +323,9 @@ Extension Modules
323323
Library
324324
-------
325325

326+
- bpo-30301: Fix AttributeError when using SimpleQueue.empty() under
327+
*spawn* and *forkserver* start methods.
328+
326329
- bpo-30375: Warnings emitted when compile a regular expression now always
327330
point to the line in the user code. Previously they could point into inners
328331
of the re module if emitted from inside of groups or conditionals.

0 commit comments

Comments
 (0)