get the running loop to compile async state#5657
Conversation
There was a problem hiding this comment.
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.pyand ensure comprehensive testing across different deployment scenarios
1 file reviewed, no comments
CodSpeed Performance ReportMerging #5657 will not alter performanceComparing Summary
|
unsure about this solution, fixes #5656