python/cpython · error · RuntimeError

Event loop stopped before Future completed.

Error message

Event loop stopped before Future completed.

What it means

Error "Event loop stopped before Future completed." thrown in python/cpython.

Source

Thrown at Lib/asyncio/base_events.py:723

        if new_task:
            # An exception is raised if the future didn't complete, so there
            # is no need to log the "destroy pending task" message
            future._log_destroy_pending = False

        future.add_done_callback(_run_until_complete_cb)
        try:
            self.run_forever()
        except:
            if new_task and future.done() and not future.cancelled():
                # The coroutine raised a BaseException. Consume the exception
                # to not log a warning, the caller doesn't have access to the
                # local task.
                future.exception()
            raise
        finally:
            future.remove_done_callback(_run_until_complete_cb)
        if not future.done():
            raise RuntimeError('Event loop stopped before Future completed.')

        return future.result()

    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.

View on GitHub (pinned to bc6749cc3b)

Solutions

  1. Ensure all tasks complete before stopping the loop: await asyncio.gather(*tasks) or use asyncio.run() which cancels and collects pending tasks.
  2. Avoid calling loop.stop() while futures are still pending; add done callbacks that stop the loop only after results arrive.

When it happens

Trigger: Raised when the event loop is stopped before the future passed to run_until_complete() finishes.

Common situations: See trigger scenarios.


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