Skip to content

feat: detect event loop blocking in development - #7672

Merged
FarhanAliRaza merged 12 commits into
reflex-dev:mainfrom
harsh21234i:fix/7619-event-loop-blocking
Oct 30, 2026
Merged

FarhanAliRaza merged 12 commits into
reflex-dev:mainfrom
harsh21234i:fix/7619-event-loop-blocking

Conversation

@harsh21234i

@harsh21234i harsh21234i commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Warn in development when an event blocks the asyncio event loop.

  • A watchdog thread puts a probe on the loop every 1–10 ms. If the probe does not run within event_loop_blocking_threshold seconds (default 0.2), the loop is blocked.
  • While the loop is still blocked, the monitor reads 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.
  • Blocking in a task that processes no event, such as a lock waiter or a task the app starts, is reported by the task name (Task 'nightly_sync' is blocking the event loop ...).
  • The first warning appears at once, so a handler that never returns is reported too. It names the handler and the innermost line of app code, then shows the stack of the event task. A second line gives the total duration when the loop responds.
  • Detection is on by default in development and always off in production. Set event_loop_blocking_threshold=0, or REFLEX_EVENT_LOOP_BLOCKING_THRESHOLD=0, to turn it off.
  • There is no cost on the event path. The monitor uses about 1% of one core while idle.

Example:

Event handler State.http_sync is blocking the event loop at my_app/my_app.py:115 in http_sync (threshold: 0.200s), including state updates such as computed vars. Move blocking I/O to a thread pool or use a non-blocking async API.
Blocking stack (most recent call last):
  ...
  File ".../my_app/my_app.py", line 115, in http_sync
    urllib.request.urlopen(SLOW_URL).read()
  ...
  File ".../socket.py", line 729, in readinto
    return self._sock.recv_into(b)
Event handler State.http_sync blocked the event loop for 0.501s in total.

Closes #7619

Testing

  • Unit tests for the monitor, the event processor, the config and the app wiring. The tests cover sync, async and post-handler blocking, a hung handler, a stale frame snapshot, threshold 0, and the env var.
  • Dev server with 11 blocking workloads: sync sleep, CPU loop, sqlite, pandas, sync HTTP, a blocking async handler, 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.
  • The durations agree with py-spy within 30 ms.
  • Event throughput with the monitor on is 97.6–99.1% of the throughput with it off.

@harsh21234i
harsh21234i requested review from a team and Alek99 as code owners October 27, 2026 14:25

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

All reported issues were addressed across 9 files

Reply with feedback, questions, or to request a fix.

View guided diff | Re-trigger cubic

Comment thread news/+7619.feature.md Resolved
Comment thread tests/units/test_config.py Resolved
Comment thread reflex/app.py Resolved
@greptile-apps

greptile-apps Bot commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 4/5

[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.

Summary

Adds development-only warnings when a task blocks the asyncio event loop.

  • Development apps now get warnings when tasks stall the event loop.
  • Apps can set how long the event loop may stall before warnings.

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

Comment thread reflex/app.py Resolved
@codspeed

codspeed Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

1 warning

⚠️ 20 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

✅ 148 untouched benchmarks


Comparing harsh21234i:fix/7619-event-loop-blocking (a1e0579) with main (96dca47)

Open in CodSpeed

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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

@masenf masenf added on deck PRs lined up to review / merge next documentation Improvements or additions to documentation labels Oct 27, 2026
harsh21234i and others added 3 commits October 28, 2026 16:30
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.

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.
@FarhanAliRaza
FarhanAliRaza merged commit 0a55df0 into reflex-dev:main Oct 30, 2026
169 checks passed
FarhanAliRaza pushed a commit that referenced this pull request Nov 11, 2026
_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]>
masenf added a commit that referenced this pull request Nov 11, 2026
_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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation on deck PRs lined up to review / merge next

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement event loop blocking detection

3 participants