Skip to content

bug: InMemoryMemoryService shares session state across concurrent agent runs in same process #6167

Closed

Description

@tcconnally

Bug Description

InMemoryMemoryService stores sessions in a process-global dictionary without isolating concurrent agent runs. When two agents run in parallel within the same Python process (e.g., during evals or parallel test suites), their session state intermixes, causing cross-contamination of conversation history.

Steps to Reproduce

import asyncio
from google.adk.sessions import InMemoryMemoryService

memory = InMemoryMemoryService()

async def run_agent_once(session_id, user_id):
    # Create session, add events, read them back
    session = await memory.create_session(
        app_name="test", user_id=user_id, session_id=session_id
    )
    await memory.append_event(session, ...)
    return await memory.get_session(...)

# Run two agents concurrently
async def main():
    results = await asyncio.gather(
        run_agent_once("session-1", "user-a"),
        run_agent_once("session-2", "user-b"),
    )

Expected Behavior

Each session should be fully isolated. Concurrent reads/writes to different session IDs should not interfere with each other.

Actual Behavior

Because InMemoryMemoryService uses a plain dict for storage (no locking or per-session copy-on-write), concurrent writes to different sessions can race on dict rehashing or key collisions. More critically, state inserted by one agent's session may be visible to another if they accidentally share a session ID or if the memory service doesn't enforce user-scoped isolation.

Impact

  • Eval suites running agents in parallel produce flaky, non-deterministic results
  • Session data leaks between users in multi-tenant deployments
  • Debugging is extremely difficult — the bug only manifests under concurrency

Proposed Fix

Add asyncio.Lock per session ID, or use an asyncio-safe store like a ContextVar-scoped dict. Consider a warning in the InMemoryMemoryService docstring that it is not safe for concurrent use.

Activity

  1. added
    services[Component] This issue is related to runtime services, e.g. sessions, memory, artifacts, etc
    on Jun 19, 2026
  2. added theissue type on Jun 19, 2026
  3. sanketpatil06 commented on Jun 23, 2026

    @sanketpatil06

    Hi @tcconnally , thanks for reporting this.
    the InMemoryMemoryService saves all memory under a single (app_name, user_id) key without any locks or barriers, so running multiple sessions for the same user at the same time will mix up their memory entries and cause dictionary crashes.

    A workaround worth trying locally: subclass InMemoryMemoryService to handle memories one-by-one using an asyncio.Lock for each session, or just add the session ID to the end of the user_id (like f"{user_id}_{session_id}") — this forces the dictionary to keep different sessions separate.

    Could you check if this stops the data leakage and crashes during your parallel runs, and verify that the memory behaves correctly under load?

  4. added
    request clarification[Status] The maintainer need clarification or more information from the author
    on Jun 26, 2026
  5. tcconnally commented on Jun 26, 2026

    @tcconnally
    Author

    Following up on my own report — I checked the current source on main and I don't think this is actually a bug:

    • InMemoryMemoryService is already thread-safe: it guards all access with a threading.Lock (added in 10197db0, "feat: Make InMemoryMemoryService thread-safe", July 2025).
    • Memory is already isolated per session — storage is keyed {app_name}/{user_id} → session.id → events — so concurrent sessions for the same user don't intermix.
    • My original reproduction also mistakenly used InMemorySessionService methods (create_session/append_event/get_session) against the memory service, which don't exist on it.

    Apologies for the noise — closing this. Thanks @sanketpatil06 for taking a look.

  6. added a commit that references this issue on Jul 13, 2026
    aeccd3b
  7. added a commit that references this issue on Aug 11, 2026
    c123d7a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

request clarification[Status] The maintainer need clarification or more information from the authorservices[Component] This issue is related to runtime services, e.g. sessions, memory, artifacts, etc

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions