python/cpython · error · RuntimeError

Cannot close a running event loop

Error message

Cannot close a running event loop

What it means

Error "Cannot close a running event loop" thrown in python/cpython.

Source

Thrown at Lib/asyncio/base_events.py:744

    def stop(self):
        """Stop running the event loop.

        Every callback already scheduled will still run.  This simply
        informs run_forever to stop looping after a complete iteration.
        """
        self._stopping = True

    def close(self):
        """Close the event loop.

        This clears the queues and shuts down the executor,
        but does not wait for the executor to finish.

        The event loop must not be running.
        """
        if self.is_running():
            raise RuntimeError("Cannot close a running event loop")
        if self._closed:
            return
        if self._debug:
            logger.debug("Close %r", self)
        self._closed = True
        self._ready.clear()
        self._scheduled.clear()
        self._executor_shutdown_called = True
        executor = self._default_executor
        if executor is not None:
            self._default_executor = None
            executor.shutdown(wait=False)

    def is_closed(self):
        """Returns True if the event loop was closed."""
        return self._closed

    def __del__(self, _warn=warnings.warn):

View on GitHub (pinned to bc6749cc3b)

Solutions

  1. Call loop.stop() first and wait for run_forever() to return before calling loop.close().
  2. Use asyncio.run() instead of manual loop lifecycle management.

When it happens

Trigger: Raised when loop.close() is called on a running event loop.

Common situations: See trigger scenarios.


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