reflex icon indicating copy to clipboard operation
reflex copied to clipboard

RuntimeError: asyncio.run() cannot be called from a running event loop in Python 3.12 with Reflex 0.8.4

Open SreehithCoupa opened this issue 1 year ago β€’ 1 comments

When using Reflex 0.8.4 with Python 3.12, the application fails to start with the following error:

RuntimeError: asyncio.run() cannot be called from a running event loop

This error does not occur with Reflex 0.8.3, suggesting a regression in how asyncio event loops are handled in version 0.8.4.

Steps to Reproduce Create a Reflex application Use Python 3.12 (e.g., in a Docker container with python:3.12-bookworm) Install Reflex 0.8.4 Run the application with Gunicorn/Uvicorn Error Stack Trace

[2025-08-01 05:39:42 +0000] [142] [ERROR] Exception in worker process
Traceback (most recent call last):
  File "/usr/local/lib/python3.12/site-packages/gunicorn/arbiter.py", line 608, in spawn_worker
    worker.init_process()
  File "/usr/local/lib/python3.12/site-packages/uvicorn/workers.py", line 75, in init_process
    super().init_process()
  File "/usr/local/lib/python3.12/site-packages/gunicorn/workers/base.py", line 143, in init_process
    self.run()
  File "/usr/local/lib/python3.12/site-packages/uvicorn/workers.py", line 107, in run
    return asyncio.run(self._serve())
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/asyncio/runners.py", line 195, in run
    return runner.run(main)
           ^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/asyncio/runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/asyncio/base_events.py", line 691, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/uvicorn/workers.py", line 102, in _serve
    await server.serve(sockets=self.sockets)
  File "/usr/local/lib/python3.12/site-packages/uvicorn/server.py", line 71, in serve
    await self._serve(sockets)
  File "/usr/local/lib/python3.12/site-packages/uvicorn/server.py", line 78, in _serve
    config.load()
  File "/usr/local/lib/python3.12/site-packages/uvicorn/config.py", line 442, in load
    self.loaded_app = self.loaded_app()
                      ^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/reflex/app.py", line 603, in __call__
    self._compile(prerender_routes=is_prod_mode())
  File "/usr/local/lib/python3.12/site-packages/reflex/app.py", line 1407, in _compile
    compiler.compile_contexts(self._state, self.theme),
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/reflex/compiler/compiler.py", line 549, in compile_contexts
    return output_path, _compile_contexts(state, theme)
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/reflex/compiler/compiler.py", line 134, in _compile_contexts
    initial_state=utils.compile_state(state),
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/reflex/compiler/utils.py", line 216, in compile_state
    return _sorted_keys(asyncio.run(_resolve_delta(initial_state)))
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/asyncio/runners.py", line 191, in run
    raise RuntimeError(
RuntimeError: asyncio.run() cannot be called from a running event loop

Environment Information Python version: 3.12 Reflex version: 0.8.4 Operating system: Linux (Docker container) Web server: Gunicorn with Uvicorn workers

Expected Behavior The application should start successfully without any asyncio-related errors, as it does with Reflex 0.8.3.

Actual Behavior The application fails to start with the RuntimeError about asyncio.run() being called from a running event loop.

Root Cause Analysis The error occurs in reflex/compiler/utils.py line 216:

return _sorted_keys(asyncio.run(_resolve_delta(initial_state)))

This is problematic because asyncio.run() is being called from within an already running event loop, which Python 3.12 strictly prohibits. Python 3.12 has stricter rules about nested event loops compared to earlier versions.

Workarounds The following workarounds have been confirmed to resolve the issue:

Downgrade to Reflex 0.8.3

SreehithCoupa avatar Aug 01 '25 08:08 SreehithCoupa