Skip to content

InMemorySessionService keeps empty user maps after sessions are deleted #7722

Description

@DisturbedCrow

🔴 Required Information

Describe the Bug:

InMemorySessionService.delete_session removes the session itself but leaves the empty per-user dictionary in service.sessions. Once all users are empty, the app dictionary is left behind too.

Repeated create/delete cycles with new user IDs therefore keep growing the internal structure even though there are no live sessions.

Steps to Reproduce:

  1. Create an InMemorySessionService.
  2. Create and delete one session for each of 10,000 distinct users.
  3. Inspect service.sessions.

Expected Behavior:

Deleting a user's last session should remove that user's empty session dictionary. If it was the app's last user, the empty app dictionary should be removed as well.

Observed Behavior:

All 10,000 empty user dictionaries remain reachable:

apps: 1
retained users: 10000
live sessions: 0

Environment Details:

  • ADK Library Version: current main at da35ca77d71f7d74c8b7d7550f6e1b591a6690e0
  • Desktop OS: Ubuntu 24.04.4 LTS
  • Python Version: 3.14.4

Model Information:

  • Are you using LiteLLM: No
  • Which model is being used: N/A

🟡 Optional Information

Regression:

Unknown.

Logs:

I also added a focused test that expected the empty app entry to be removed after deleting its only session. It fails on current main with:

AssertionError: assert 'my_app' not in {'my_app': {'user': {}}}

Screenshots / Video:

N/A

Additional Context:

The cleanup can stay local to _delete_session_impl: remove the session, then remove the user entry if its dictionary is empty, followed by the app entry if that dictionary is empty. User- and app-scoped state are stored separately, so this would only prune the session index.

Minimal Reproduction Code:

import asyncio

from google.adk.sessions.in_memory_session_service import InMemorySessionService


async def main():
  service = InMemorySessionService()

  for i in range(10_000):
    user_id = f'user-{i}'
    session = await service.create_session(app_name='app', user_id=user_id)
    await service.delete_session(
        app_name='app', user_id=user_id, session_id=session.id
    )

  print('apps:', len(service.sessions))
  print('retained users:', len(service.sessions['app']))
  print(
      'live sessions:',
      sum(len(sessions) for sessions in service.sessions['app'].values()),
  )


asyncio.run(main())

How often has this issue occurred?:

  • Always (100%)

Activity

  1. added 2 commits that reference this issue on Oct 26, 2026
    0f93887
    9d73464
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions