Skip to content

Commit f75de59

Browse files
ankursharmascopybara-github
authored andcommitted
fix: In memory session service to evaluate dictionary keys and value into an isolated snapshot sequence before starting loop
_list_sessions_impl intermittently would throw `RuntimeError: dictionary changed size during iteration` Co-authored-by: Ankur Sharma <ankusharma@google.com> PiperOrigin-RevId: 892454902
1 parent a27ce47 commit f75de59

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

src/google/adk/sessions/in_memory_session_service.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -242,15 +242,14 @@ def _list_sessions_impl(
242242
sessions_without_events = []
243243

244244
if user_id is None:
245-
for user_id in self.sessions[app_name]:
246-
for session_id in self.sessions[app_name][user_id]:
247-
session = self.sessions[app_name][user_id][session_id]
245+
for uid in list(self.sessions[app_name].keys()):
246+
for session in list(self.sessions[app_name][uid].values()):
248247
copied_session = copy.deepcopy(session)
249248
copied_session.events = []
250-
copied_session = self._merge_state(app_name, user_id, copied_session)
249+
copied_session = self._merge_state(app_name, uid, copied_session)
251250
sessions_without_events.append(copied_session)
252251
else:
253-
for session in self.sessions[app_name][user_id].values():
252+
for session in list(self.sessions[app_name][user_id].values()):
254253
copied_session = copy.deepcopy(session)
255254
copied_session.events = []
256255
copied_session = self._merge_state(app_name, user_id, copied_session)

0 commit comments

Comments
 (0)