python/cpython · error · RuntimeError

Event loop is closed

Error message

Event loop is closed

What it means

Error "Event loop is closed" thrown in python/cpython.

Source

Thrown at Lib/asyncio/base_events.py:556

        """Create subprocess transport."""
        raise NotImplementedError

    def _write_to_self(self):
        """Write a byte to self-pipe, to wake up the event loop.

        This may be called from a different thread.

        The subclass is responsible for implementing the self-pipe.
        """
        raise NotImplementedError

    def _process_events(self, event_list):
        """Process selector events."""
        raise NotImplementedError

    def _check_closed(self):
        if self._closed:
            raise RuntimeError('Event loop is closed')

    def _check_default_executor(self):
        if self._executor_shutdown_called:
            raise RuntimeError('Executor shutdown has been called')

    def _asyncgen_finalizer_hook(self, agen):
        self._asyncgens.discard(agen)
        if not self.is_closed():
            self.call_soon_threadsafe(self.create_task, agen.aclose())

    def _asyncgen_firstiter_hook(self, agen):
        if self._asyncgens_shutdown_called:
            warnings.warn(
                f"asynchronous generator {agen!r} was scheduled after "
                f"loop.shutdown_asyncgens() call",
                ResourceWarning, source=self)

        self._asyncgens.add(agen)

View on GitHub (pinned to bc6749cc3b)

Solutions

  1. Schedule all coroutines/callbacks before calling loop.close(); check loop.is_closed() first.
  2. Create a fresh event loop with asyncio.new_event_loop() if the previous one was closed.
  3. Prefer asyncio.run(), which manages loop creation and closing for you.

When it happens

Trigger: Raised when an operation is attempted on a closed event loop.

Common situations: See trigger scenarios.


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