{"record":{"id":"75ba080d850df23e","repo":"python/cpython","slug":"task-done-called-too-many-times","errorCode":null,"errorMessage":"task_done() called too many times","messagePattern":"task_done\\(\\) called too many times","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"Lib/asyncio/queues.py","lineNumber":235,"sourceCode":"        self._wakeup_next(self._putters)\n        return item\n\n    def task_done(self):\n        \"\"\"Indicate that a formerly enqueued task is complete.\n\n        Used by queue consumers. For each get() used to fetch a task,\n        a subsequent call to task_done() tells the queue that the processing\n        on the task is complete.\n\n        If a join() is currently blocking, it will resume when all items\n        have been processed (meaning that a task_done() call was received\n        for every item that had been put() into the queue).\n\n        Raises ValueError if called more times than there were items placed\n        in the queue.\n        \"\"\"\n        if self._unfinished_tasks <= 0:\n            raise ValueError('task_done() called too many times')\n        self._unfinished_tasks -= 1\n        if self._unfinished_tasks == 0:\n            self._finished.set()\n\n    async def join(self):\n        \"\"\"Block until all items in the queue have been gotten and processed.\n\n        The count of unfinished tasks goes up whenever an item is added to\n        the queue.  The count goes down whenever a consumer calls\n        task_done() to indicate that the item was retrieved and all work on\n        it is complete.  When the count of unfinished tasks drops to zero,\n        join() unblocks.\n        \"\"\"\n        if self._unfinished_tasks > 0:\n            await self._finished.wait()\n\n    def shutdown(self, immediate=False):\n        \"\"\"Shut-down the queue, making queue gets and puts raise QueueShutDown.","sourceCodeStart":217,"sourceCodeEnd":253,"githubUrl":"https://github.com/python/cpython/blob/bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6/Lib/asyncio/queues.py#L217-L253","documentation":"Error \"task_done() called too many times\" thrown in python/cpython.","triggerScenarios":"Raised when Queue.task_done() is called more times than items were put on the queue.","commonSituations":"See trigger scenarios.","solutions":["Call task_done() exactly once per successful get(); keep a counter or use join() correctly so producers and consumers stay balanced.","Do not call task_done() more times than items were put into the queue."],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6","analyzedAt":"2026-08-14T22:01:13.976Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}