Whats the expected behavior of save_session & load_session? #34
Replies: 3 comments 1 reply
-
|
Hey @matthewdavis, good catch on the session persistence issue. You're right that something isn't working as expected. The ProblemThe "Session file corrupted (invalid JSON)" error suggests the session isn't being saved properly. This could be happening for a few reasons:
Quick Debug StepsTry this to see what's actually happening: import os
from monarchmoney import MonarchMoney
# Login and save with explicit path
mm = MonarchMoney()
await mm.login(email, password)
# Check if we actually have a token
print(f"Token exists: {mm.token is not None}")
print(f"Token length: {len(mm.token) if mm.token else 0}")
# Save with explicit path and check file
session_path = "debug_session.pickle"
mm.save_session(session_path)
# Check if file was created and its size
if os.path.exists(session_path):
size = os.path.getsize(session_path)
print(f"Session file created: {session_path} ({size} bytes)")
else:
print("Session file not created")Then in a new process: import os
from monarchmoney import MonarchMoney
session_path = "debug_session.pickle"
print(f"File exists: {os.path.exists(session_path)}")
mm = MonarchMoney()
try:
mm.load_session(session_path)
print(f"Session loaded, token exists: {mm.token is not None}")
except Exception as e:
print(f"Load failed: {e}")Potential WorkaroundIf the pickle sessions are problematic, you can manually handle the token: # After login, save token manually
token = mm.token
with open("token.txt", "w") as f:
f.write(token)
# In new process
with open("token.txt", "r") as f:
token = f.read().strip()
mm = MonarchMoney(token=token)
# Should work without loginLet me know what the debug output shows and we can figure out what's going wrong with the session persistence. |
Beta Was this translation helpful? Give feedback.
-
✅ Fixed in v0.10.1!Great investigation @matthewdavis! You identified the exact issue - the session persistence was completely broken across processes. I've just released v0.10.1 which resolves this. What Was WrongYou were 100% correct in your analysis:
The root causes were:
The FixStable Encryption: Default passwords now use the home directory instead of hostname/username, ensuring consistent keys across processes. Enhanced Session Data: Both methods now handle comprehensive session data including CSRF tokens. Testing the Fix# Process 1: Save session
mm1 = MonarchMoney(session_file="test.json", use_encryption=True)
# ... login ...
mm1.save_session()
# Process 2: Load session (this now works!)
mm2 = MonarchMoney(session_file="test.json", use_encryption=True)
mm2.load_session() # ✅ Works perfectly now
await mm2.get_accounts() # ✅ Ready to useUpgradepip install --upgrade monarchmoney-enhancedThe fix maintains full backward compatibility - your existing code will work exactly the same, just with reliable cross-process session persistence. Thanks again for the detailed bug report! Your analysis of the Fernet encryption vs JSON parsing mismatch was spot-on and made this fix much faster to implement. Release Notes: https://github.com/keithah/monarchmoney-enhanced/releases/tag/v0.10.1 |
Beta Was this translation helpful? Give feedback.
-
✅ Fixed in v0.10.1!Great investigation @matthewdavis! You identified the exact issue - the session persistence was completely broken across processes. I've just released v0.10.1 which resolves this. What Was WrongYou were 100% correct in your analysis:
The root causes were:
The FixStable Encryption: Default passwords now use the home directory instead of hostname/username, ensuring consistent keys across processes. Enhanced Session Data: Both methods now handle comprehensive session data including CSRF tokens. Testing the Fix# Process 1: Save session
mm1 = MonarchMoney(session_file="test.json", use_encryption=True)
# ... login ...
mm1.save_session()
# Process 2: Load session (this now works!)
mm2 = MonarchMoney(session_file="test.json", use_encryption=True)
mm2.load_session() # ✅ Works perfectly now
await mm2.get_accounts() # ✅ Ready to useUpgradepip install --upgrade monarchmoney-enhancedThe fix maintains full backward compatibility - your existing code will work exactly the same, just with reliable cross-process session persistence. Thanks again for the detailed bug report! Your analysis of the Fernet encryption vs JSON parsing mismatch was spot-on and made this fix much faster to implement. Release Notes: https://github.com/keithah/monarchmoney-enhanced/releases/tag/v0.10.1 |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
From my testing, the
save_session()andload_session()methods appear to work within the same Python process but fail completely when trying to persist sessions across separate processes or application restarts.Expected Behavior
Session save/load should enable authentication persistence across application restarts:
Actual Behavior
Within Same Process (Misleading Success)
Across Processes (Real Failure)
The error:
Environment
Beta Was this translation helpful? Give feedback.
All reactions