Repository navigation
feat: detect event loop blocking in development - #7672
Conversation
There was a problem hiding this comment.
All reported issues were addressed across 9 files
Reply with feedback, questions, or to request a fix.
View guided diff | Re-trigger cubic
[Medium impact] The new warnings affect development event processing, not production. The existing docstring requirement must be satisfied before merging; no new blocking defect was found. SummaryAdds development-only warnings when a task blocks the asyncio event loop.
No new actionable issues were found in the changes since the previous review. Reviews (12) · Last reviewed commit: "feat: report blocking in any task, not o..." · Reviewed by Greptile |
Merging this PR will not alter performance1 warning
Comparing |
There was a problem hiding this comment.
All reported issues were addressed across 3 files (changes from recent commits).
Reply with feedback, questions, or to request a fix.
View guided diff | Re-trigger cubic
Name the event task running on the blocked loop instead of tracking handler frames per event, so blocking in computed vars after the handler is reported. Log after the loop responds with the real duration. Remove the per-event hooks and the duplicated otel branch, and simplify the threshold validation.
There was a problem hiding this comment.
All reported issues were addressed across 7 files (changes from recent commits).
Reply with feedback, questions, or to request a fix.
View guided diff | Re-trigger cubic
Capture the loop thread's frames from the event task's entry to the blocking line when the threshold is crossed, so the warning points at the code that blocked, including computed vars and C-extension calls.
Log the handler and its stack as soon as the threshold is reached, so a handler that never returns is reported, then log the total duration once the loop responds. Report nothing when the frame snapshot no longer contains the event task's entry frame.
Use 0 instead of None to turn detection off, so REFLEX_EVENT_LOOP_BLOCKING_THRESHOLD=0 works: the env parser cannot produce None for optional fields. Name the innermost frame of app code (under the app root, outside the stdlib and installed packages) in the first warning line, so library frames do not hide the line to fix.
A blocking task that processes no event, such as the lock waiter or a task the app starts, is now reported by its task name. Event handler warnings keep their wording.
_transform_event_arg ran `from reflex.model import Model` for every dict payload with a class annotation (dict, dataclass, TypedDict, Pydantic model). The first such event therefore imported reflex.model, and with the db extra SQLAlchemy/SQLModel/alembic (0.22-0.56 s, ~143 modules), inside the event task, and the dev event-loop blocking monitor (#7672) reported the user's trivial handler as blocking the loop. A class can only subclass rx.Model after reflex.model has run, so look the module up in sys.modules instead of importing it, as telemetry_accounting._get_db_model_count already does. Apps that never use rx.Model never load SQLAlchemy, at startup or per event, and apps that do already loaded it when their models were defined. Claude-Session: https://claude.ai/code/session_01Dxjadq3qkhKnqZR2b9WBFi Co-authored-by: Claude <[email protected]>
_transform_event_arg ran `from reflex.model import Model` for every dict payload with a class annotation (dict, dataclass, TypedDict, Pydantic model). The first such event therefore imported reflex.model, and with the db extra SQLAlchemy/SQLModel/alembic (0.22-0.56 s, ~143 modules), inside the event task, and the dev event-loop blocking monitor (#7672) reported the user's trivial handler as blocking the loop. A class can only subclass rx.Model after reflex.model has run, so look the module up in sys.modules instead of importing it, as telemetry_accounting._get_db_model_count already does. Apps that never use rx.Model never load SQLAlchemy, at startup or per event, and apps that do already loaded it when their models were defined. Claude-Session: https://claude.ai/code/session_01Dxjadq3qkhKnqZR2b9WBFi Co-authored-by: Claude <[email protected]> (cherry picked from commit 68b522c)
Summary
Warn in development when an event blocks the asyncio event loop.
event_loop_blocking_thresholdseconds (default0.2), the loop is blocked.asyncio.current_task(loop)and gets the handler from the event task's name (reflex_event|<event>|…). This covers the whole event, including computed vars recalculated after the handler.Task 'nightly_sync' is blocking the event loop ...).event_loop_blocking_threshold=0, orREFLEX_EVENT_LOOP_BLOCKING_THRESHOLD=0, to turn it off.Example:
Closes #7619
Testing
0, and the env var.asynchandler, a background event, a helper function and a slow computed var. All 11 are detected with the correct handler and app line, on Python 3.11 and 3.14, with asyncio, uvloop and rloop. There were no warnings for awaiting handlers or blocks shorter than the threshold.