Skip to content

Commit 4256c86

Browse files
committed
bpo-22872: multiprocessing.Queue's put() and get() now raise ValueError if the queue is closed
Previously, put() and get() would raise AssertionError and OSError, respectively.
1 parent 1b5f9c9 commit 4256c86

File tree

4 files changed

+24
-2
lines changed

4 files changed

+24
-2
lines changed

Doc/library/multiprocessing.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -786,6 +786,10 @@ For an example of the usage of queues for interprocess communication see
786786
available, else raise the :exc:`queue.Full` exception (*timeout* is
787787
ignored in that case).
788788

789+
.. versionchanged:: 3.8
790+
If the queue is closed, :exc:`ValueError` is raised instead of
791+
:exc:`AssertionError`.
792+
789793
.. method:: put_nowait(obj)
790794

791795
Equivalent to ``put(obj, False)``.
@@ -800,6 +804,10 @@ For an example of the usage of queues for interprocess communication see
800804
``False``), return an item if one is immediately available, else raise the
801805
:exc:`queue.Empty` exception (*timeout* is ignored in that case).
802806

807+
.. versionchanged:: 3.8
808+
If the queue is closed, :exc:`ValueError` is raised instead of
809+
:exc:`OSError`.
810+
803811
.. method:: get_nowait()
804812

805813
Equivalent to ``get(False)``.

Lib/multiprocessing/queues.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ def _after_fork(self):
7878
self._poll = self._reader.poll
7979

8080
def put(self, obj, block=True, timeout=None):
81-
assert not self._closed, "Queue {0!r} has been closed".format(self)
81+
if self._closed:
82+
raise ValueError(f"Queue {self!r} is closed")
8283
if not self._sem.acquire(block, timeout):
8384
raise Full
8485

@@ -89,6 +90,8 @@ def put(self, obj, block=True, timeout=None):
8990
self._notempty.notify()
9091

9192
def get(self, block=True, timeout=None):
93+
if self._closed:
94+
raise ValueError(f"Queue {self!r} is closed")
9295
if block and timeout is None:
9396
with self._rlock:
9497
res = self._recv_bytes()
@@ -298,7 +301,8 @@ def __setstate__(self, state):
298301
self._cond, self._unfinished_tasks = state[-2:]
299302

300303
def put(self, obj, block=True, timeout=None):
301-
assert not self._closed, "Queue {0!r} is closed".format(self)
304+
if self._closed:
305+
raise ValueError(f"Queue {self!r} is closed")
302306
if not self._sem.acquire(block, timeout):
303307
raise Full
304308

Lib/test/_test_multiprocessing.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1116,6 +1116,13 @@ def _on_queue_feeder_error(e, obj):
11161116
# Assert that the serialization and the hook have been called correctly
11171117
self.assertTrue(not_serializable_obj.reduce_was_called)
11181118
self.assertTrue(not_serializable_obj.on_queue_feeder_error_was_called)
1119+
1120+
def test_closed_queue_put_get_exceptions(self):
1121+
for q in multiprocessing.Queue(), multiprocessing.JoinableQueue():
1122+
q.close()
1123+
for meth, args in (q.put, ('foo',)), (q.get, ()):
1124+
with self.assertRaisesRegex(ValueError, 'is closed'):
1125+
meth(*args)
11191126
#
11201127
#
11211128
#
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
When the queue is closed, :exc:`ValueError` is now raised by
2+
:meth:`multiprocessing.Queue.put` and :meth:`multiprocessing.Queue.get`
3+
instead of :exc:`AssertionError` and :exc:`OSError`, respectively.

0 commit comments

Comments
 (0)