{"record":{"id":"d115f962bb857c0e","repo":"python/cpython","slug":"event-loop-stopped-before-future-completed","errorCode":null,"errorMessage":"Event loop stopped before Future completed.","messagePattern":"Event loop stopped before Future completed\\.","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"Lib/asyncio/base_events.py","lineNumber":723,"sourceCode":"        if new_task:\n            # An exception is raised if the future didn't complete, so there\n            # is no need to log the \"destroy pending task\" message\n            future._log_destroy_pending = False\n\n        future.add_done_callback(_run_until_complete_cb)\n        try:\n            self.run_forever()\n        except:\n            if new_task and future.done() and not future.cancelled():\n                # The coroutine raised a BaseException. Consume the exception\n                # to not log a warning, the caller doesn't have access to the\n                # local task.\n                future.exception()\n            raise\n        finally:\n            future.remove_done_callback(_run_until_complete_cb)\n        if not future.done():\n            raise RuntimeError('Event loop stopped before Future completed.')\n\n        return future.result()\n\n    def stop(self):\n        \"\"\"Stop running the event loop.\n\n        Every callback already scheduled will still run.  This simply\n        informs run_forever to stop looping after a complete iteration.\n        \"\"\"\n        self._stopping = True\n\n    def close(self):\n        \"\"\"Close the event loop.\n\n        This clears the queues and shuts down the executor,\n        but does not wait for the executor to finish.\n\n        The event loop must not be running.","sourceCodeStart":705,"sourceCodeEnd":741,"githubUrl":"https://github.com/python/cpython/blob/bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6/Lib/asyncio/base_events.py#L705-L741","documentation":"Error \"Event loop stopped before Future completed.\" thrown in python/cpython.","triggerScenarios":"Raised when the event loop is stopped before the future passed to run_until_complete() finishes.","commonSituations":"See trigger scenarios.","solutions":["Ensure all tasks complete before stopping the loop: await asyncio.gather(*tasks) or use asyncio.run() which cancels and collects pending tasks.","Avoid calling loop.stop() while futures are still pending; add done callbacks that stop the loop only after results arrive."],"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-15T17:31:12.345Z"}