{"record":{"id":"2b9e920358773204","repo":"python/cpython","slug":"this-event-loop-is-already-running","errorCode":null,"errorMessage":"This event loop is already running","messagePattern":"This event loop is already running","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"Lib/asyncio/base_events.py","lineNumber":637,"sourceCode":"                          f\"its threads within {timeout} seconds.\",\n                          RuntimeWarning, stacklevel=2)\n            self._default_executor.shutdown(wait=False)\n        else:\n            thread.join()\n\n    def _do_shutdown(self, future):\n        try:\n            self._default_executor.shutdown(wait=True)\n            if not self.is_closed():\n                self.call_soon_threadsafe(futures._set_result_unless_cancelled,\n                                          future, None)\n        except Exception as ex:\n            if not self.is_closed() and not future.cancelled():\n                self.call_soon_threadsafe(future.set_exception, ex)\n\n    def _check_running(self):\n        if self.is_running():\n            raise RuntimeError('This event loop is already running')\n        if events._get_running_loop() is not None:\n            raise RuntimeError(\n                'Cannot run the event loop while another loop is running')\n\n    def _run_forever_setup(self):\n        \"\"\"Prepare the run loop to process events.\n\n        This method exists so that custom event loop subclasses (e.g., event loops\n        that integrate a GUI event loop with Python's event loop) have access to all the\n        loop setup logic.\n        \"\"\"\n        self._check_closed()\n        self._check_running()\n        self._set_coroutine_origin_tracking(self._debug)\n\n        self._old_agen_hooks = sys.get_asyncgen_hooks()\n        self._thread_id = threading.get_ident()\n        sys.set_asyncgen_hooks(","sourceCodeStart":619,"sourceCodeEnd":655,"githubUrl":"https://github.com/python/cpython/blob/bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6/Lib/asyncio/base_events.py#L619-L655","documentation":"Error \"This event loop is already running\" thrown in python/cpython.","triggerScenarios":"Raised when loop.run_forever()/run_until_complete() is called on a loop that is already running.","commonSituations":"See trigger scenarios.","solutions":["Do not call loop.run_forever() or run_until_complete() from within a running loop; use await or asyncio.create_task() instead.","Use asyncio.run() only at the top level of your program."],"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"}