{"record":{"id":"99e31cd58106449b","repo":"python/cpython","slug":"cannot-run-the-event-loop-while-another-loop-is-ru","errorCode":null,"errorMessage":"Cannot run the event loop while another loop is running","messagePattern":"Cannot run the event loop while another loop is running","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"Lib/asyncio/base_events.py","lineNumber":639,"sourceCode":"            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(\n            firstiter=self._asyncgen_firstiter_hook,\n            finalizer=self._asyncgen_finalizer_hook","sourceCodeStart":621,"sourceCodeEnd":657,"githubUrl":"https://github.com/python/cpython/blob/bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6/Lib/asyncio/base_events.py#L621-L657","documentation":"Error \"Cannot run the event loop while another loop is running\" thrown in python/cpython.","triggerScenarios":"Raised when trying to run an event loop while another loop is already running in the thread.","commonSituations":"See trigger scenarios.","solutions":["Only one event loop may run per thread; run the second loop in a separate thread via threading.Thread and asyncio.new_event_loop().","Refactor nested loop usage into await calls within a single loop."],"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"}