Practical technical guides / Recent development: google/adk-python commit 6b2d455 (2026-11-05)

Concurrent ADK Sessions Could Erase Shared user: and app: State: The 5 November Fix and the Test to Keep

ADK Python commit 6b2d455 (5 November 2026) stops concurrent sessions dropping shared state keys. Measured before and after on SQLite, and what it does not cover.

By Sandeep Belgavi · Published 2026-11-05

ADK Python commit 6b2d455 (5 November 2026) stops concurrent sessions dropping shared state keys. Measured before and after on SQLite, and what it does not cover.

Two tabs, one preference lost

Jenya opens the Belgavi's Book Store help-desk assistant in two browser tabs. In one tab she sets her preferred reply language; in the other, a few hundred milliseconds later, she asks to be notified about a delayed order. Each tab is its own ADK session, and each turn writes a user: key, which ADK shares across every session of the same user. When she reopens the assistant the next day, one of the two preferences is gone. Nothing logged an error.

On 5 November 2026 at 00:58 UTC, google/adk-python merged commit 6b2d45523b, "fix: stop concurrent writes losing app: and user: state". The change came from pull request 7942, which shows as closed rather than merged on GitHub; the fix landed as a separate commit whose message references the pull request. This guide explains the bug, what the commit changes, what we measured before and after it, and what it does not cover. As of 5 November the fix was on main only; the latest tagged ADK Python release was 2.13.0 of 23 October.

Why scoped state is a read-modify-write

ADK session state has prefixes. A key with no prefix belongs to one session. A key starting with user: is shared by every session of one user in one app, and app: by every session of the app. A temp: key is never persisted.

DatabaseSessionService stores each shared scope as one JSON document in one row: one row per app in the app-state table, one row per app and user in the user-state table. To apply an event's state_delta, it reads the row, merges the delta in Python with dict.update, and writes the whole document back. That is the classic lost update. Two writers that both read the document before either writes will each write back a document missing the other's key.

The pull request describes how the old code ordered those writers. append_event relied on SELECT ... FOR UPDATE, "which SQLAlchemy renders on PostgreSQL and MySQL", and its in-process lock was per session, not per user or app. create_session merged its initial app: and user: state into rows it read without any lock, on every dialect. RedisSessionService.create_session had "the same unguarded GET, merge and SET".

What the commit changes

The commit message summarises it: DatabaseSessionService "now serializes, in process, writes to the shared app- and user-state rows from append_event and create_session, and create_session reads the rows its initial state merges into FOR UPDATE on PostgreSQL and MySQL. RedisSessionService.create_session and append_event merge app: and user: state in a watched transaction."

In the code, the per-session lock map became a general lock map keyed by ("session", app, user, session), ("user", app, user) or ("app", app). A write takes the session lock first, then the app lock, then the user lock, always in that order so two writers cannot wait on each other in a cycle. Events with no scoped delta take no extra lock. In create_session, a state row that already exists is locked with FOR UPDATE before the initial state is merged into it. The code comment explains why a missing row is not locked: "on MySQL a locking read of a missing key takes a gap lock, and two such inserts of the same key deadlock."

There are no schema or API changes.

Measured before and after

We installed ADK Python from Git at the commit's parent (245f5d5) and at 6b2d455, with SQLAlchemy 2.1.8 and aiosqlite 0.22.1, on CPython 3.13.5 under Windows 11. Both builds report version 2.13.0, because the version string had not changed on main. The probe creates four sessions for one user, then appends one event to each of them at once with asyncio.gather; each event writes its own user: key and its own app: key. A fresh session then reads the merged state. It repeats this 20 times.

Database URLBuildTrials keeping all 4 user: keysuser: keys kept of 80app: keys kept of 80
sqlite+aiosqlite:///:memory:parent of the fix0 of 202020
sqlite+aiosqlite:///:memory:6b2d45520 of 208080
sqlite+aiosqlite:///sessions.dbparent of the fix20 of 208080
sqlite+aiosqlite:///sessions.db6b2d45520 of 208080

With the in-memory database, the old code kept exactly one of the four keys in every trial, which matches what the pull request reported for four concurrent turns ("get_user_state returns 1 of the 4 keys"). The fix kept all four every time.

The file-backed database did not lose keys on either build, even when we injected a 50 ms pause after each state-row read to widen the race window. That is not luck. In the parent build, for a file-backed SQLite engine, ADK registers a hook that issues BEGIN IMMEDIATE at the start of every read-write transaction, so the database itself admits one writer at a time and the second writer reads only after the first commits. The in-memory engine uses SQLAlchemy's StaticPool, one shared connection that cannot nest transactions, and ADK skips that hook there. So, in this version, the loss on SQLite needed the in-memory configuration that many tests, notebooks and local demos use. The parent of 6b2d455 is the one we checked; we did not test older releases.

We did not run PostgreSQL, MySQL or Redis. On PostgreSQL and MySQL, the pull request's description says append_event was already ordered by FOR UPDATE, and that create_session(state=...) with scoped keys could still drop a concurrent writer's keys; those claims are the authors', not our measurement.

What it does not cover

The ADK digest for 5 November raised the open question, and the commit message answers it only partly. The new locks are asyncio.Lock objects inside one DatabaseSessionService instance. They order writers in one process. Across processes:

  • on PostgreSQL and MySQL, append_event and now create_session lock the existing row with FOR UPDATE, so writers in different processes are ordered by the database;
  • on SQLite, ordering across processes comes only from SQLite's own write lock, which for a file-backed database means the BEGIN IMMEDIATE hook described above;
  • two service instances in one process, for example two app objects each constructing their own service, do not share the lock map.

If you run several server processes against one database, the guarantee you can rely on is the database's row lock, not the in-process lock. Check which dialect you are on.

The pattern behind it

Any store that keeps a shared document and updates it by read, merge in application code, write back, has this bug unless something orders the writers. The four fixes available are the ones the ADK services already use:

  1. Lock the row for the duration of the read-merge-write (SELECT ... FOR UPDATE), the database fix.
  2. Serialize in process, which is enough only for a single process.
  3. Merge in the database in one statement, so there is no window. The pull request notes that SqliteSessionService already did this "with a single-statement SQL merge".
  4. Use optimistic concurrency: a version column or a watched key (Redis WATCH), and retry on conflict.
import json, sqlite3

def merge_user_state(conn: sqlite3.Connection, app: str, user: str, delta: dict) -> None:
    # One statement: SQLite's json_patch merges the delta into the stored document,
    # so there is no window between read and write for another writer to fall into.
    conn.execute(
        "UPDATE user_states SET state = json_patch(state, ?) WHERE app_name = ? AND user_id = ?",
        (json.dumps(delta), app, user),
    )

The single-statement merge is the most robust because it needs no lock in your code at all. Note that SQLite's json_patch follows JSON Merge Patch semantics, where a null value deletes a key, which differs from Python's dict.update; decide which semantics you want before switching.

Upgrade and test checklist

  • If you use DatabaseSessionService with sqlite+aiosqlite:///:memory: and serve concurrent requests, you were exposed; upgrade to a release that includes 6b2d455 once one is tagged.
  • On PostgreSQL or MySQL, the remaining exposure before the fix was create_session(state=...) with app: or user: keys.
  • Add a concurrency test like the one in the companion example: N sessions of one user, each writing a distinct user: key at once, then assert that all N keys are present. Run it against the database URL you deploy with, not only the in-memory one.
  • Treat app: keys as shared configuration and avoid writing them on every turn. Every write to a shared row is a point of contention, fixed or not.

Limits

Measurements are from one Windows machine with SQLite only, using builds from Git rather than a release. The injected delay is ours and exists only to widen the race window. The book store, its users and the keys are synthetic, and no model was called: the probe appends events directly through the session service.

Primary references and authorship

Sources dated on or before 5 November 2026. Original explanation and synthetic examples by Sandeep Belgavi. Companion code: Sandeep-BA/agent-decision-lab/examples/python/adk_scoped_state_race. Views are my own and do not represent any employer, past or present.