You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
bug: InMemoryMemoryService shares session state across concurrent agent runs in same process #6167
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
importasynciofromgoogle.adk.sessionsimportInMemoryMemoryServicememory=InMemoryMemoryService()
asyncdefrun_agent_once(session_id, user_id):
# Create session, add events, read them backsession=awaitmemory.create_session(
app_name="test", user_id=user_id, session_id=session_id
)
awaitmemory.append_event(session, ...)
returnawaitmemory.get_session(...)
# Run two agents concurrentlyasyncdefmain():
results=awaitasyncio.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.
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?
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.
Activity
sanketpatil06 commentedon Jun 23, 2026
Hi @tcconnally , thanks for reporting this.
the
InMemoryMemoryServicesaves 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
InMemoryMemoryServiceto handle memories one-by-one using anasyncio.Lockfor each session, or just add the session ID to the end of theuser_id(likef"{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?
tcconnally commentedon Jun 26, 2026
Following up on my own report — I checked the current source on
mainand I don't think this is actually a bug:InMemoryMemoryServiceis already thread-safe: it guards all access with athreading.Lock(added in10197db0, "feat: Make InMemoryMemoryService thread-safe", July 2025).{app_name}/{user_id}→session.id→ events — so concurrent sessions for the same user don't intermix.InMemorySessionServicemethods (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.
fix: snapshot events under lock in InMemoryMemoryService.search_memory
fix: snapshot events under lock in InMemoryMemoryService.search_memory