Skip to content

Commit 0461704

Browse files
ZackerySpytzserhiy-storchaka
authored andcommitted
bpo-22872: multiprocessing.Queue's put() and get() now raise ValueError if the queue is closed. (GH-9010)
Previously, put() and get() would raise AssertionError and OSError, respectively.
1 parent e385d06 commit 0461704

File tree

4 files changed

+26
-2
lines changed

4 files changed

+26
-2
lines changed

Doc/library/multiprocessing.rst

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

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

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

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

806814
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: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,6 +1114,14 @@ def _on_queue_feeder_error(e, obj):
11141114
# Assert that the serialization and the hook have been called correctly
11151115
self.assertTrue(not_serializable_obj.reduce_was_called)
11161116
self.assertTrue(not_serializable_obj.on_queue_feeder_error_was_called)
1117+
1118+
def test_closed_queue_put_get_exceptions(self):
1119+
for q in multiprocessing.Queue(), multiprocessing.JoinableQueue():
1120+
q.close()
1121+
with self.assertRaisesRegex(ValueError, 'is closed'):
1122+
q.put('foo')
1123+
with self.assertRaisesRegex(ValueError, 'is closed'):
1124+
q.get()
11171125
#
11181126
#
11191127
#
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
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.
4+
Patch by Zackery Spytz.

0 commit comments

Comments
 (0)