Skip to content

get the running loop to compile async state#5657

Merged
adhami3310 merged 3 commits into
mainfrom
khaleel/eng-7204-runtimeerror-asynciorun-cannot-be-called-from-a-running
Aug 1, 2025
Merged

get the running loop to compile async state#5657
adhami3310 merged 3 commits into
mainfrom
khaleel/eng-7204-runtimeerror-asynciorun-cannot-be-called-from-a-running

Conversation

@adhami3310
Copy link
Copy Markdown
Member

unsure about this solution, fixes #5656

@linear
Copy link
Copy Markdown

linear Bot commented Aug 1, 2025

Copy link
Copy Markdown
Contributor

@greptile-apps greptile-apps Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Greptile Summary

This PR fixes a critical Python 3.12 compatibility issue where asyncio.run() was being called from within an already running event loop, causing a RuntimeError. The change modifies the compile_state function in reflex/compiler/utils.py to detect if an event loop is already running using asyncio.get_running_loop(), and if so, uses loop.run_until_complete() instead of asyncio.run() to execute the coroutine.

The previous implementation used a complex threading workaround with ThreadPoolExecutor specifically designed for Playwright test environments where an event loop was already active. This new approach simplifies the logic by directly leveraging the existing event loop when available, which is the standard pattern for handling coroutines in async contexts.

This change integrates with Reflex's state compilation system, which is responsible for resolving async state deltas during the build process. The compile_state function is called during compilation to determine the initial state structure, and this fix ensures it works correctly in both scenarios: when no event loop exists (normal compilation) and when one is already running (testing or certain deployment scenarios).

Confidence score: 3/5

  • This PR addresses a real Python 3.12 compatibility issue but removes specialized test environment handling
  • Score reflects uncertainty about impact on Playwright tests and the removal of threading logic without clear replacement
  • Pay close attention to reflex/compiler/utils.py and ensure comprehensive testing across different deployment scenarios

1 file reviewed, no comments

Edit Code Review Bot Settings | Greptile

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented Aug 1, 2025

CodSpeed Performance Report

Merging #5657 will not alter performance

Comparing khaleel/eng-7204-runtimeerror-asynciorun-cannot-be-called-from-a-running (413d72d) with main (651539f)

Summary

✅ 8 untouched benchmarks

@adhami3310 adhami3310 merged commit a1432bc into main Aug 1, 2025
41 checks passed
@adhami3310 adhami3310 deleted the khaleel/eng-7204-runtimeerror-asynciorun-cannot-be-called-from-a-running branch August 1, 2025 19:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants