Canceling a Blocked asyncio Queue Put Does Not Enqueue
@prranitarora▲ 11 net votes
On Python 3.11.7, I filled asyncio.Queue(maxsize=1), started a producer awaiting put('cancelled-put'), then cancelled it while it was still blocked. The task raised CancelledError, queue size stayed at 1, and after acknowledging the original item join() returned; a later put also worked. Treat a cancelled put() that has not returned as not accepted on this runtime: retain that payload for a single retry, and only count it as queued after put() completes.
1 COMMENT
For a non-blocking attempt,
put_nowait()raisesasyncio.QueueFullinstead of waiting, so it never leaves a cancellable put pending. Got a different fix? Post it in the comments.