Repository: reflex-dev/reflex

Issue:
reset() does not clear private (i.e. _ ) attributes
**Describe the bug**
reset() clears the state of public attributes but the private attributes continue to keep their values. 

This happens when calling the reset of all substates from a single state using  
"await self.get_state" and then calling the reset on the result. 

Print statements before and after the reset show the public attributes are cleared but the private ones are not. 

**Expected behavior**
both private and public state attributes should be cleared when reset is called 

**Specifics (please complete the following information):**
 - Python Version: 3.12.3
 - Reflex Version:  0.6.2a1
 - OS: Ubuntu
 - Browser (Optional):
 


Discussion:
Interested in submitting a PR? This is a good first issue
Have you tried on 0.5.10? Is this a regression? Does it only apply to `get_state`, or do you also see this when calling `self.reset()`
Currently it's buried in my project code so I'll create a separate testcase. 

Is there a good way to do that so it can be included as a regression test for you or should I just make a github repo for it?

I can also try doing a PR...
To answer your questions:
1. It fails in both 0.5.10 and 0.6.1
2. It does not require get_state to fail. It fails in the simplest possible scenario.

Here is a quick test case that illustrates the issue:
https://github.com/bertbarabas/reflex_4065

we have `tests/units/test_state.py::test_reset`, but i don't think it tests backend vars, so that could probably be updated to include resetting at least one backend var

```diff
diff --git a/tests/units/test_state.py b/tests/units/test_state.py
index 2bb690247..f6dd04659 100644
--- a/tests/units/test_state.py
+++ b/tests/units/test_state.py
@@ -105,6 +105,7 @@ class TestState(BaseState):
     complex: Dict[int, Object] = {1: Object(), 2: Object()}
     fig: Figure = Figure()
     dt: datetime.datetime = datetime.datetime.fromisoformat("1989-11-09T18:53:00+01:00")
+    _backend: int = 0
 
     @ComputedVar
     def sum(self) -> float:
@@ -706,6 +707,7 @@ def test_reset(test_state, child_state):
     # Set some values.
     test_state.num1 = 1
     test_state.num2 = 2
+    test_state._backend = 3
     child_state.value = "test"
 
     # Reset the state.
@@ -714,6 +716,7 @@ def test_reset(test_state, child_state):
     # The values should be reset.
     assert test_state.num1 == 0
     assert test_state.num2 == 3.14
+    assert test_state._backend == 0
     assert child_state.value == ""
 
     expected_dirty_vars = {
```

This seems to reproduce the issue on my end.



Patch:
diff --git a/reflex/state.py b/reflex/state.py
index ef07278d9f5..3b3c98f0dfb 100644
--- a/reflex/state.py
+++ b/reflex/state.py
@@ -1238,6 +1238,10 @@ def reset(self):
                 default = copy.deepcopy(field.default)
             setattr(self, prop_name, default)
 
+        # Reset the backend vars.
+        for prop_name, value in self.backend_vars.items():
+            setattr(self, prop_name, copy.deepcopy(value))
+
         # Recursively reset the substates.
         for substate in self.substates.values():
             substate.reset()


Test patch:
diff --git a/tests/units/test_state.py b/tests/units/test_state.py
index 830cfbafba6..afc775fa06c 100644
--- a/tests/units/test_state.py
+++ b/tests/units/test_state.py
@@ -105,6 +105,7 @@ class TestState(BaseState):
     complex: Dict[int, Object] = {1: Object(), 2: Object()}
     fig: Figure = Figure()
     dt: datetime.datetime = datetime.datetime.fromisoformat("1989-11-09T18:53:00+01:00")
+    _backend: int = 0
 
     @ComputedVar
     def sum(self) -> float:
@@ -706,6 +707,7 @@ def test_reset(test_state, child_state):
     # Set some values.
     test_state.num1 = 1
     test_state.num2 = 2
+    test_state._backend = 3
     child_state.value = "test"
 
     # Reset the state.
@@ -714,6 +716,7 @@ def test_reset(test_state, child_state):
     # The values should be reset.
     assert test_state.num1 == 0
     assert test_state.num2 == 3.14
+    assert test_state._backend == 0
     assert child_state.value == ""
 
     expected_dirty_vars = {
@@ -729,6 +732,7 @@ def test_reset(test_state, child_state):
         "map_key",
         "mapping",
         "dt",
+        "_backend",
     }
 
     # The dirty vars should be reset.