Skip to content

fix(sessions): microsecond-safe DatabaseSessionService on SQL Server (mssql) - #6943

Open
anmolg1997 wants to merge 1 commit into
google:mainfrom
anmolg1997:fix-mssql-session-storage
Open

fix(sessions): microsecond-safe DatabaseSessionService on SQL Server (mssql)#6943
anmolg1997 wants to merge 1 commit into
google:mainfrom
anmolg1997:fix-mssql-session-storage

Conversation

@anmolg1997

Copy link
Copy Markdown
Contributor

Problem

DatabaseSessionService fails on SQL Server on the second append_event of a session — i.e. essentially every real conversation's first turn — with the optimistic-concurrency error ("session has been modified in storage since it was loaded"). Two independent defects combine:

  1. PreciseTimestamp maps to legacy DATETIME on mssql (the generic DateTime fallback). Legacy DATETIME has ~3.33 ms precision, so the microsecond update marker read back never equals the one written.
  2. mssql is missing from _NAIVE_DATETIME_DIALECTS, so create_session stores timezone-aware datetimes while DATETIME2 reads back naive — the marker comparison renders ...+00:00 vs ... for the same instant and raises the same false stale-write error.

Fix

  • Map PreciseTimestamp to DATETIME2(precision=6) on mssql (mirrors the existing mysql.DATETIME(fsp=6) branch).
  • Add mssql to _NAIVE_DATETIME_DIALECTS (Spanner stays excluded — its TIMESTAMP is timezone-aware).
  • Unit test for the dialect mapping.

Validation

Both fixes have been running for ~2 months in production as downstream monkeypatches against Azure SQL (mssql+aioodbc, per-env schemas, ~10^5 persisted events), on google-adk 1.36 → 2.6.3. tests/unittests/sessions/ passes with the change (the handful of vertex-session failures in my environment reproduce identically on unpatched main — unrelated).

Happy to extend test coverage if you'd like an mssql-dialect marker round-trip test as well.

…(mssql)

Two defects break DatabaseSessionService on SQL Server today, both hit on
the SECOND append_event of a session (i.e. essentially every real
conversation's first turn):

1. PreciseTimestamp falls through to the plain DateTime impl, which maps to
   SQL Server's legacy DATETIME (~3.33ms precision). The optimistic-
   concurrency update marker is microsecond-precision, so the value read
   back never equals the value written and the session's own next append is
   rejected as a stale write. Fix: map to DATETIME2(6), the SQL Server type
   with microsecond precision (mirrors the existing mysql DATETIME(fsp=6)
   branch).

2. mssql is missing from _NAIVE_DATETIME_DIALECTS, so create_session writes
   timezone-aware datetimes while DATETIME2 reads back naive; the marker
   comparison then renders '...+00:00' vs '...' for the same instant and
   raises the same false stale-write error. Fix: include mssql, so tzinfo
   is stripped before storage exactly as for sqlite/postgresql/mysql/
   mariadb (Spanner stays excluded - its TIMESTAMP is timezone-aware).

Both fixes have been running in production for two months as downstream
monkeypatches on Azure SQL (mssql+aioodbc, ~10^5 events); this upstreams
them. Added a unit test for the DATETIME2(6) dialect mapping.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants