python/cpython · error · RuntimeError

Cannot run the event loop while another loop is running

Error message

Cannot run the event loop while another loop is running

What it means

Error "Cannot run the event loop while another loop is running" thrown in python/cpython.

Source

Thrown at Lib/asyncio/base_events.py:639

            self._default_executor.shutdown(wait=False)
        else:
            thread.join()

    def _do_shutdown(self, future):
        try:
            self._default_executor.shutdown(wait=True)
            if not self.is_closed():
                self.call_soon_threadsafe(futures._set_result_unless_cancelled,
                                          future, None)
        except Exception as ex:
            if not self.is_closed() and not future.cancelled():
                self.call_soon_threadsafe(future.set_exception, ex)

    def _check_running(self):
        if self.is_running():
            raise RuntimeError('This event loop is already running')
        if events._get_running_loop() is not None:
            raise RuntimeError(
                'Cannot run the event loop while another loop is running')

    def _run_forever_setup(self):
        """Prepare the run loop to process events.

        This method exists so that custom event loop subclasses (e.g., event loops
        that integrate a GUI event loop with Python's event loop) have access to all the
        loop setup logic.
        """
        self._check_closed()
        self._check_running()
        self._set_coroutine_origin_tracking(self._debug)

        self._old_agen_hooks = sys.get_asyncgen_hooks()
        self._thread_id = threading.get_ident()
        sys.set_asyncgen_hooks(
            firstiter=self._asyncgen_firstiter_hook,
            finalizer=self._asyncgen_finalizer_hook

View on GitHub (pinned to bc6749cc3b)

Solutions

  1. Only one event loop may run per thread; run the second loop in a separate thread via threading.Thread and asyncio.new_event_loop().
  2. Refactor nested loop usage into await calls within a single loop.

When it happens

Trigger: Raised when trying to run an event loop while another loop is already running in the thread.

Common situations: See trigger scenarios.


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