python/cpython · error · ValueError

task_done() called too many times

Error message

task_done() called too many times

What it means

Error "task_done() called too many times" thrown in python/cpython.

Source

Thrown at Lib/asyncio/queues.py:235

        self._wakeup_next(self._putters)
        return item

    def task_done(self):
        """Indicate that a formerly enqueued task is complete.

        Used by queue consumers. For each get() used to fetch a task,
        a subsequent call to task_done() tells the queue that the processing
        on the task is complete.

        If a join() is currently blocking, it will resume when all items
        have been processed (meaning that a task_done() call was received
        for every item that had been put() into the queue).

        Raises ValueError if called more times than there were items placed
        in the queue.
        """
        if self._unfinished_tasks <= 0:
            raise ValueError('task_done() called too many times')
        self._unfinished_tasks -= 1
        if self._unfinished_tasks == 0:
            self._finished.set()

    async def join(self):
        """Block until all items in the queue have been gotten and processed.

        The count of unfinished tasks goes up whenever an item is added to
        the queue.  The count goes down whenever a consumer calls
        task_done() to indicate that the item was retrieved and all work on
        it is complete.  When the count of unfinished tasks drops to zero,
        join() unblocks.
        """
        if self._unfinished_tasks > 0:
            await self._finished.wait()

    def shutdown(self, immediate=False):
        """Shut-down the queue, making queue gets and puts raise QueueShutDown.

View on GitHub (pinned to bc6749cc3b)

Solutions

  1. Call task_done() exactly once per successful get(); keep a counter or use join() correctly so producers and consumers stay balanced.
  2. Do not call task_done() more times than items were put into the queue.

When it happens

Trigger: Raised when Queue.task_done() is called more times than items were put on the queue.

Common situations: See trigger scenarios.


AI-assisted analysis of python/cpython@bc6749cc3b (2026-08-14). Data as JSON: /api/errors/75ba080d850df23e. Report an issue: GitHub.