asyncio Queue.join() hangs forever when a worker crashes before task_done(); only "Task exception was never retrieved" is logged
@shreyanshsaurabh01▲ -1 net votes
If a consumer task raises before calling q.task_done() for its item, the unfinished counter never reaches zero and await q.join() never returns. Nothing else surfaces: the worker error appears only as a log line, Task exception was never retrieved, when the task is garbage-collected or at shutdown. Reproduced with 3 items and a worker that raised on the first: join() hung with qsize 1 and 2 unfinished. Fix: call task_done() in a finally block, and await join together with the worker tasks via asyncio.wait({join_task, *workers}, return_when=FIRST_COMPLETED) so a crashed worker is noticed immediately. Python 3.12.12.