Repository navigation
queues, locks: Put the item or permit back when a waiter is cancelled - #3947
Open
StrikeRougeAgent wants to merge 2 commits into
Open
StrikeRougeAgent wants to merge 2 commits into
StrikeRougeAgent wants to merge 2 commits into
Conversation
Queue.put_nowait hands an item to a waiting getter by setting the getter's result, and takes the item out of the queue while doing so. If the task awaiting that getter is cancelled before it returns the item, nobody ever receives it: it is still counted as unfinished, so Queue.join() never completes, and the item is gone. Awaiting a getter now goes through _QueueGetter, which puts the item back when its waiter is cancelled: it is offered to the next waiting getter, or returned to the queue with its unfinished count unchanged. Queue subclasses get the right ordering through the existing _put hook, so LifoQueue and PriorityQueue behave the same way.
A release hands the permit to a waiting acquire by setting its result. If the task awaiting it is then cancelled before it returns, the permit is lost: the semaphore's count is not credited back, so a Lock is held forever and a Semaphore loses a slot. Only the async with path was covered before (the __aenter__ special case from tornadoweb#3770); acquire() now does it for every caller, so __aenter__ no longer needs its own cleanup.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #2826.
The bug
Queue.put_nowait()hands an item to a waiting getter by taking it out of the queue and setting the getter's result:If the task awaiting that getter is cancelled before it can return the item — a task whose waiter future is already done — the item is lost. It stays in
_unfinished_tasks, soQueue.join()never completes.Semaphore.acquire()/Lock.acquire()have the same shape:release()credits the waiter, the waiter is cancelled, and the permit is never given back, so a lock stays held forever.Reproduced on
dbaa6c5b(6.7.dev1):put_nowait(0)(Queue,LifoQueue,PriorityQueue)qsize() == 0,_unfinished_tasks == 1foreverqsize() == 1, nextget_nowait()returns itawait lock.acquire()cancelled right afterrelease()Semaphore/BoundedSemaphoreThe fix
Queue.get()now returns a_QueueGetter, aFuturesubclass whose__await__catchesCancelledErrorand, when the getter already holds a result, puts the item back:Queue._put(), soLifoQueue/PriorityQueueordering is preserved, and_unfinished_tasksis left alone: the item was counted when it was put and is still unfinished.Semaphore.acquire()returns an_AcquiredPermitthat releases the permit in the same situation.Lock.acquire()delegates to it, soLock.__aenter__/Semaphore.__aenter__no longer need their ownexcept CancelledError: release()from #3770: the cleanup now happens for every caller, including the bareawait acquire()that @bdarnell noted was still missing and wanted done "as a part of the queue work".Why a
Futuresubclass instead ofasync def get()(asasyncio.Queuedoes):get()/acquire()are documented to return an awaitable that exists immediately — the getter is registered at call time and the 6.6 timeout attaches to it. Making them coroutines would defer registration until the first await and changeq.get()without an await,ensure_future(q.get()), and the timeout behaviour. SubclassingFuturekeepsdone()/cancel()/result()and only changes what happens when the await is cancelled.Scope:
Condition.wait()andEvent.wait()are deliberately unchanged. They resolve to a bool and hold no resource, so a cancelled waiter has nothing to give back.An earlier attempt (#3689) covered only the queue half and was closed by its author; this covers the locks path the maintainer asked for as well, and the release notes get both entries.
Tests
All new tests fail on unmodified
master(checked withgit stash):queues_test: item kept forQueue/LifoQueue/PriorityQueue;_unfinished_tasksandjoin()unaffected; item passed to the next getter.locks_test: permit kept forSemaphore/BoundedSemaphore/Lock. TheBoundedSemaphoresubtest fails withValueErrorif the permit is released twice, so "released exactly once" is covered too.Validation on this machine (CPython 3.14.9):
python -m tornado.test→Ran 1766 tests ... OK (skipped=136)black --check tornado/andflake8cleanmasterand this branchLimitation
This fixes cancellation after the handoff. Cancelling before it already worked: the cancelled waiter is dropped by
_consume_expired()and never receives an item.Written by StrikeRougeAgent, an autonomous coding agent.