Skip to content

Conversation

@masenf
Copy link
Collaborator

@masenf masenf commented Nov 14, 2025

It turns out that just rehydrating and not also re-running the on_load handlers can leave applications in a bad state; particularly when their background tasks exiting are predicated on the sid, that will change on reconnect, but without an on_load, no mechanism is there to restart the tasks.

It turns out that just rehydrating and not also re-running the on_load handlers
can leave applications in a bad state; particularly when their background tasks
exiting are predicated on the `sid`, that will change on reconnect, but without
an on_load, no mechanism is there to restart the tasks.
@codspeed-hq
Copy link

codspeed-hq bot commented Nov 14, 2025

CodSpeed Performance Report

Merging #5980 will not alter performance

Comparing masenf/full-rehydrate (f30cfbc) with main (3617ee4)

Summary

✅ 8 untouched

@greptile-apps
Copy link
Contributor

greptile-apps bot commented Nov 14, 2025

Greptile Overview

Greptile Summary

This PR reverts the reconnect optimization introduced in PR #5969 to fix issues with background tasks becoming stale after reconnection. Previously, reconnects only sent a hydrate event without running on_load handlers to avoid re-initializing state. However, this left applications in a bad state when background tasks depended on the session ID (sid), which changes on reconnect. Without on_load handlers re-running, there was no mechanism to restart these tasks with the new sid.

Key Changes:

  • Frontend now queues all initialEvents() on reconnect (including both hydrate and on_load events)
  • Removed is_reconnect flag and payload modification from JavaScript
  • Backend middleware now always resets client storage and marks state as not hydrated
  • Background tasks will properly restart with the new sid through on_load handlers

Confidence Score: 5/5

  • This PR is safe to merge with minimal risk
  • The changes are a clean revert of an optimization that caused issues. The code returns to a simpler, more reliable approach where reconnections trigger a full re-hydration cycle. The existing test suite (test_disconnect_reconnect in test_background_task.py) validates this behavior.
  • No files require special attention

Important Files Changed

File Analysis

Filename Score Overview
reflex/.templates/web/utils/state.js 5/5 Simplified reconnect logic to run full hydration including on_load events; removed is_reconnect flag
reflex/middleware/hydrate_middleware.py 5/5 Removed reconnect-specific logic, now always resets client storage and marks state as not hydrated

Sequence Diagram

sequenceDiagram
    participant Client as Frontend (state.js)
    participant Socket as WebSocket
    participant Server as Backend
    participant Middleware as HydrateMiddleware
    
    Note over Client,Server: Initial Connection
    Client->>Socket: connect()
    Socket->>Server: WebSocket connection established
    Server->>Client: "connect" event
    Client->>Client: queue initialEvents()
    Client->>Server: hydrate event
    Server->>Middleware: preprocess(hydrate)
    Middleware->>Middleware: reset client storage
    Middleware->>Middleware: set is_hydrated = false
    Middleware->>Server: return state delta
    Server->>Client: state update + on_load events
    
    Note over Client,Server: Reconnection (After this PR)
    Client->>Client: socket.rehydrate = true
    Socket--xServer: Disconnected
    Client->>Socket: reconnect()
    Socket->>Server: WebSocket reconnected
    Server->>Client: "connect" event
    Client->>Client: Check rehydrate flag = true
    Client->>Client: queue ALL initialEvents() (hydrate + on_load)
    Client->>Server: hydrate event
    Server->>Middleware: preprocess(hydrate)
    Middleware->>Middleware: reset client storage
    Middleware->>Middleware: set is_hydrated = false
    Middleware->>Server: return state delta
    Server->>Client: state update + on_load events
    Note over Client,Server: Background tasks restart with new sid
Loading

Copy link
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.

2 files reviewed, no comments

Edit Code Review Agent Settings | Greptile

@masenf masenf force-pushed the masenf/full-rehydrate branch from 02220b1 to ef179a4 Compare November 14, 2025 22:36
@adhami3310 adhami3310 merged commit 9dfbbc4 into main Nov 17, 2025
47 checks passed
@adhami3310 adhami3310 deleted the masenf/full-rehydrate branch November 17, 2025 18:57
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.

3 participants