#1671·adk-go

session/database: serialize AppendEvent per session, matching adk-python

Author: baptmontCreated Sep 29, 2026Updated Sep 29, 2026
Labelsenhancement

adk-python serializes append_event per session in its database service. adk-go's session/database does not, and that difference is behind a class of concurrency bugs here.

What adk-python does

  • _with_session_lock keeps one asyncio.Lock per (app_name, user_id, session_id). It is reference-counted and dropped when the last waiter leaves (L494-L524).
  • append_event holds that lock across the stale check, the commit and the last_update_time update (L918-L1011).
  • It locks the session row with FOR UPDATE where the dialect supports it (L931).
  • It applies the event to the in-memory session only after the commit (L1014).

What adk-go does

AppendEvent takes no per-session lock and no row lock, so two appends on one session run concurrently and the database sorts it out. With SQLite, one is aborted with a lock error.

It also appends to the in-memory session before persisting (service.go#L391-L398). That has two effects:

  • A failed append still leaves its event in Events().
  • Two overlapping appends can land in memory in a different order from storage.

And because appends aren't serialized, every shared field has to be reasoned about on its own. #1244 does that for updatedAt. With appends serialized, most of that reasoning goes away.

This is reachable today: tail-retention compaction appends its summary to the invocation's session while sub-agents are still producing events (internal/llminternal/compaction_processor.go).

Proposal

Match Python in the database backend:

  • a per-session mutex in databaseService, keyed by (appName, userID, sessionID) and held across AppendEvent
  • the in-memory append moved to after the commit
  • FOR UPDATE on the session fetch where supported

Keying the lock on the session identity rather than the localSession object also covers two handles to the same session, as Python's does.

Only the database backend needs this. Python's Vertex AI, in-memory and SQLite session services take no such lock.

Related

  • #1244: the narrow fix for the updatedAt data race. It stays scoped to that race.
  • #1256: refreshing stale handles, which touches the same AppendEvent code.