pola-rs/polars · error · RuntimeError
Event loop stopped before Future completed.
Error message
Event loop stopped before Future completed.
What it means
The patched `run_until_complete` in polars' nest_asyncio runs the loop until the future is done OR the loop is stopping. If the loop was stopped (e.g. by an outer `stop()` or another coroutine stopping the loop) before the awaited future finished, the future is incomplete and RuntimeError is raised.
Solutions
- Run the coroutine on a fresh event loop instead of a shared/stopped one: `asyncio.run(coro)` in a clean context.
- Avoid nesting `run_until_complete` calls; restructure code to `await` instead.
- Ensure nothing calls `loop.stop()` before the future completes; check signal handlers and other libraries patching asyncio.
- Upgrade polars / remove nest_asyncio patching if running under Jupyter's native async support (Python 3.8+ IPython supports top-level await).
Example fix
// before loop.run_until_complete(coro) # RuntimeError inside notebook's running loop // after result = await coro # top-level await in Jupyter
Defensive patterns
Strategy: try-catch
Try / catch
try:
result = loop.run_until_complete(fut)
except RuntimeError as e:
if "Event loop stopped before Future completed" in str(e):
result = asyncio.run(coro) # retry on a fresh loop
else:
raise Prevention
- Never call `loop.stop()` while awaited futures are pending.
- Avoid nested `run_until_complete` calls; use `await`.
- In Jupyter, use top-level `await` instead of manually driving loops.
- Run blocking async helpers on a dedicated, single-owner event loop.
When it happens
Trigger: Calling a polars API that internally runs `loop.run_until_complete(fut)` inside an environment where something else stops the event loop — e.g. nesting `run_until_complete` inside an already-running loop, a signal handler calling `loop.stop()`, or cancelled tasks.
Common situations: Jupyter notebooks with competing event-loop managers; mixing polars' patched loop with other frameworks (tornado, ipykernel) that stop loops; calling async-blocking polars helpers from threads.
Understand the failure class
Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.
Related errors
- Can't patch loop of type
- asyncio is already patched by nest_asyncio
- cannot create String column without an offsets buffer
- could not execute mprotect for CPUID check
- {0}
AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18).
Data as JSON: /api/errors/9e76dddd7d7c6439.
Report an issue: GitHub.
Appendix: source
Thrown at py-polars/src/polars/_utils/nest_asyncio.py:283
def run_forever(self):
with manage_run(self), manage_asyncgens(self):
while True:
self._run_once()
if self._stopping:
break
self._stopping = False
def run_until_complete(self, future):
with manage_run(self):
f = asyncio.ensure_future(future, loop=self)
if f is not future:
f._log_destroy_pending = False
while not f.done():
self._run_once()
if self._stopping:
break
if not f.done():
raise RuntimeError("Event loop stopped before Future completed.")
return f.result()
def _run_once(self):
"""
Simplified re-implementation of asyncio's _run_once that
runs handles as they become ready.
"""
scheduled = self._scheduled
while scheduled and scheduled[0]._cancelled:
heappop(scheduled)
timeout = (
0
if self._ready or self._stopping
else min(max(scheduled[0]._when - self.time(), 0), 86400)
if scheduled
else None
)View on GitHub (pinned to fe841f959e)