Skip to content

Commit 9081b36

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

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
@@ -3868,6 +3868,42 @@ def test_semaphore_tracker(self):
38683868
self.assertRegex(err, expected)
38693869
self.assertRegex(err, 'semaphore_tracker: %r: \[Errno' % name1)
38703870

3871+
class TestSimpleQueue(unittest.TestCase):
3872+
3873+
@classmethod
3874+
def _test_empty(cls, queue, child_can_start, parent_can_continue):
3875+
child_can_start.wait()
3876+
# issue 30301, could fail under spawn and forkserver
3877+
try:
3878+
queue.put(queue.empty())
3879+
queue.put(queue.empty())
3880+
finally:
3881+
parent_can_continue.set()
3882+
3883+
def test_empty(self):
3884+
queue = multiprocessing.SimpleQueue()
3885+
child_can_start = multiprocessing.Event()
3886+
parent_can_continue = multiprocessing.Event()
3887+
3888+
proc = multiprocessing.Process(
3889+
target=self._test_empty,
3890+
args=(queue, child_can_start, parent_can_continue)
3891+
)
3892+
proc.daemon = True
3893+
proc.start()
3894+
3895+
self.assertTrue(queue.empty())
3896+
3897+
child_can_start.set()
3898+
parent_can_continue.wait()
3899+
3900+
self.assertFalse(queue.empty())
3901+
self.assertEqual(queue.get(), True)
3902+
self.assertEqual(queue.get(), False)
3903+
self.assertTrue(queue.empty())
3904+
3905+
proc.join()
3906+
38713907
#
38723908
# Mixins
38733909
#

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ Extension Modules
4949
Library
5050
-------
5151

52+
- bpo-30301: Fix AttributeError when using SimpleQueue.empty() under
53+
*spawn* and *forkserver* start methods.
54+
5255
- bpo-30329: imaplib and poplib now catch the Windows socket WSAEINVAL error
5356
(code 10022) on shutdown(SHUT_RDWR): An invalid operation was attempted.
5457
This error occurs sometimes on SSL connections.

0 commit comments

Comments
 (0)