8000 gh-117531: Unblock getters after non-immediate queue shutdown (#117532) · python/cpython@6bc0b33 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6bc0b33

Browse files
authored
gh-117531: Unblock getters after non-immediate queue shutdown (#117532)
(This is a small tweak of the original gh-104750 which added shutdown.)
1 parent dfcae43 commit 6bc0b33

File tree

3 files changed

+26
-5
lines changed

3 files changed

+26
-5
lines changed

Doc/library/queue.rst

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,8 +245,10 @@ them down.
245245
queue is empty. Set *immediate* to true to make :meth:`~Queue.get` raise
246246
immediately instead.
247247

248-
All blocked callers of :meth:`~Queue.put` will be unblocked. If *immediate*
249-
is true, also unblock callers of :meth:`~Queue.get` and :meth:`~Queue.join`.
248+
All blocked callers of :meth:`~Queue.put` and :meth:`~Queue.get` will be
249+
unblocked. If *immediate* is true, a task will be marked as done for each
250+
remaining item in the queue, which may unblock callers of
251+
:meth:`~Queue.join`.
250252

251253
.. versionadded:: 3.13
252254

Lib/queue.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,9 @@ def shutdown(self, immediate=False):
239239
By default, gets will only raise once the queue is empty. Set
240240
'immediate' to True to make gets raise immediately instead.
241241
242-
All blocked callers of put() will be unblocked, and also get()
243-
and join() if 'immediate'.
242+
All blocked callers of put() and get() will be unblocked. If
243+
'immediate', a task is marked as done for each item remaining in
244+
the queue, which may unblock callers of join().
244245
'''
245246
with self.mutex:
246247
self.is_shutdown = True
@@ -249,9 +250,10 @@ def shutdown(self, immediate=False):
249250
self._get()
250251
if self.unfinished_tasks > 0:
251252
self.unfinished_tasks -= 1
252-
self.not_empty.notify_all()
253253
# release all blocked threads in `join()`
254254
self.all_tasks_done.notify_all()
255+
# All getters need to re-check queue-empty to raise ShutDown
256+
self.not_empty.notify_all()
255257
self.not_full.notify_all()
256258

257259
# Override these methods to implement other queue organizations

Lib/test/test_queue.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,23 @@ def test_shutdown_get_task_done_join(self):
636636

637637
self.assertEqual(results, [True]*len(thrds))
638638

639+
def test_shutdown_pending_get(self):
640+
def get():
641+
try:
642+
results.append(q.get())
643+
except Exception as e:
644+
results.append(e)
645+
646+
q = self.type2test()
647+
results = []
648+
get_thread = threading.Thread(target=get)
649+
get_thread.start()
650+
q.shutdown(immediate=False)
651+
get_thread.join(timeout=10.0)
652+
self.assertFalse(get_thread.is_alive())
653+
self.assertEqual(len(results), 1)
654+
self.assertIsInstance(results[0], self.queue.ShutDown)
655+
639656

640657
class QueueTest(BaseQueueTestMixin):
641658

0 commit comments

Comments
 (0)
0