Skip to content

Conversation

@masenf
Copy link
Collaborator

@masenf masenf commented Nov 25, 2025

Save the originally requested state class as requested_state_cls so a subsequent loop variable that was also called state_cls doesn't interfere with returning the correct state at the end of the function.

Save the originally requested state class as `requested_state_cls` so a
subsequent loop variable that was also called `state_cls` doesn't interfere
with returning the correct state at the end of the function.
@codspeed-hq
Copy link

codspeed-hq bot commented Nov 25, 2025

CodSpeed Performance Report

Merging #6001 will not alter performance

Comparing masenf/redis-get-state (d15cdfa) with main (cb262b6)

Summary

✅ 8 untouched

@greptile-apps
Copy link
Contributor

greptile-apps bot commented Nov 25, 2025

Greptile Overview

Greptile Summary

Fixed a critical variable shadowing bug in StateManagerRedis.get_state where a loop variable named state_cls was overwriting the originally requested state class, causing the function to return the wrong state.

  • Renamed the original state_cls variable to requested_state_cls to preserve the requested state class throughout the function execution
  • The bug occurred because two loops (lines 300 and 303) used state_cls as their iteration variable, shadowing the original value
  • This caused line 340 to return the last iterated state class instead of the originally requested one
  • Added comprehensive regression test test_add_dependency_get_state_regression that reproduces the scenario where this bug would manifest
  • Also fixed similar variable shadowing in test_base_class_vars by renaming loop variable from field to field_name to avoid shadowing the field import

Confidence Score: 5/5

  • This PR is safe to merge with no risk - it fixes a critical bug with a clean solution and includes regression tests
  • The variable rename is a straightforward fix that eliminates variable shadowing without changing any logic. The bug fix is well-tested with a comprehensive regression test that would have caught this issue. The similar fix in the test file follows the same pattern and is equally safe.
  • No files require special attention

Important Files Changed

File Analysis

Filename Score Overview
reflex/istate/manager/redis.py 5/5 Fixed variable shadowing bug where loop variable state_cls overwrote the originally requested state class, causing wrong state to be returned
tests/units/test_state.py 5/5 Added regression test for the bug fix and renamed loop variable field to field_name to avoid shadowing the field import

Sequence Diagram

sequenceDiagram
    participant Client
    participant StateManagerRedis
    participant Redis
    participant StateTree
    
    Client->>StateManagerRedis: get_state(token)
    Note over StateManagerRedis: Split token to extract state_path
    StateManagerRedis->>StateManagerRedis: requested_state_cls = get_class_substate(state_path)
    Note over StateManagerRedis: Store requested class before loop
    
    StateManagerRedis->>StateManagerRedis: Get already populated states
    StateManagerRedis->>StateManagerRedis: Calculate required_state_classes
    
    loop For each required state class
        StateManagerRedis->>Redis: pipeline.get(_substate_key(token, state_cls))
    end
    
    Redis-->>StateManagerRedis: Return redis_state data
    
    loop For each state_cls in required_state_classes
        Note over StateManagerRedis: Loop variable state_cls does NOT overwrite requested_state_cls
        StateManagerRedis->>StateManagerRedis: Deserialize or create state instance
        StateManagerRedis->>StateTree: Add state to flat_state_tree
        StateManagerRedis->>StateTree: Link parent-child relationships
    end
    
    alt top_level requested
        StateManagerRedis-->>Client: Return top-level state
    else specific substate requested
        Note over StateManagerRedis: Return requested_state_cls, not last loop iteration
        StateManagerRedis-->>Client: Return flat_state_tree[requested_state_cls.get_full_name()]
    end
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

Ensure that fetching a dependency state causes dependent states to also be
fetched.
@masenf masenf merged commit a20f0bf into main Nov 25, 2025
47 checks passed
@masenf masenf deleted the masenf/redis-get-state branch November 25, 2025 20:03
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