Fix: RedisSessionService.list_sessions() state loss bug#45
Fix: RedisSessionService.list_sessions() state loss bug#45BaskaranNM wants to merge 1 commit intogoogle:mainfrom
Conversation
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
## Problem
RedisSessionService.list_sessions() was clearing session state by setting
`session.state = {}` at line 178, causing loss of app-level and user-level
state data when listing sessions.
This differed from InMemorySessionService.list_sessions() which correctly
preserves state by calling `self._merge_state()`.
## Impact
- Session state (including custom fields like 'title', '_ag_ui_thread_id')
was lost when listing sessions via the API
- Frontend applications couldn't display session metadata in session lists
- State was only available when fetching individual sessions via get_session()
## Root Cause
The list_sessions() method was directly clearing state instead of merging
app/user state from Redis hashes, breaking the state management contract.
## Solution
Replace `session.state = {}` with `session = await self._merge_state(app_name, user_id, session)`
to match the behavior of InMemorySessionService.list_sessions()
## Changes
- Line 178: Changed from `session.state = {}` to merge_state() call
- Ensures consistent state handling across all session operations
- Maintains backward compatibility (events still cleared as intended)
## Testing
- Updated test_create_and_list_sessions to verify state is preserved
- Added test_list_sessions_preserves_app_user_state for explicit state verification
- All 16 tests pass
830bb56 to
dfb7f91
Compare
|
@gemini-cli /review |
|
🤖 Hi @DeanChensj, I've received your request, and I'm working on it now! You can track my progress in the logs for more details. |
There was a problem hiding this comment.
Thanks for the fix! It correctly addresses the bug where session state was being lost in list_sessions. However, the current implementation has a performance issue where it performs multiple Redis calls per session in a loop. I've suggested an optimization to fetch the state once before the loop.
Please consider refactoring the implementation to improve performance, especially for users with a large number of sessions.
| session = Session.model_validate(session_data) | ||
| session.events = [] | ||
| session.state = {} | ||
| session = await self._merge_state(app_name, user_id, session) |
There was a problem hiding this comment.
This change introduces a performance bottleneck. Calling _merge_state inside the loop will result in two Redis HGETALL calls for every session in the list (2N calls). Since the app_name and user_id are constant for all sessions in this method, we should fetch the app and user state once before the loop and merge them into each session manually to avoid unnecessary network roundtrips.
| session = Session.model_validate(session_data) | ||
| session.events = [] | ||
| session.state = {} | ||
| session = await self._merge_state(app_name, user_id, session) |
There was a problem hiding this comment.
Consider refactoring this to fetch the state once:
app_state_raw = await self.cache.hgetall(RedisKeys.app_state(app_name))
app_state = {State.APP_PREFIX + k.decode(): orjson.loads(v) for k, v in app_state_raw.items()}
user_state_raw = await self.cache.hgetall(RedisKeys.user_state(app_name, user_id))
user_state = {State.USER_PREFIX + k.decode(): orjson.loads(v) for k, v in user_state_raw.items()}
for session_data in sessions.values():
session = Session.model_validate(session_data)
session.events = []
session.state.update(app_state)
session.state.update(user_state)
sessions_without_events.append(session)This would reduce the number of Redis calls from 2N + 1 to 3.
Description
Fixes a bug in RedisSessionService.list_sessions() where session state was being cleared instead of preserved, causing loss of app-level and user-level state data.
Problem
RedisSessionService.list_sessions() was clearing session state by setting session.state = {} at line 178, which caused:
Root Cause
The method was directly clearing state instead of merging app/user state from Redis hashes, breaking the state management contract that other methods (get_session(), create_session()) correctly implement.
Solution
Replace session.state = {} with session = await self._merge_state(app_name, user_id, session) to match the behavior of:
Changes
Testing
Verified that: