Skip to content

fix(sessions): use INSERT ON CONFLICT DO NOTHING to prevent race in create_session#4955

Open
Lyt060814 wants to merge 2 commits intogoogle:mainfrom
Lyt060814:fix/create-session-race-condition
Open

fix(sessions): use INSERT ON CONFLICT DO NOTHING to prevent race in create_session#4955
Lyt060814 wants to merge 2 commits intogoogle:mainfrom
Lyt060814:fix/create-session-race-condition

Conversation

@Lyt060814
Copy link

Description

DatabaseSessionService.create_session contains a SELECT-then-INSERT pattern when initializing app_states and user_states rows for a new app_name/user_id. Under concurrent parallel calls (e.g. multiple sub-agents starting simultaneously), all callers see None from the SELECT and all attempt INSERT, causing a UniqueViolation on the primary key constraint.

Fix

Replace the check-then-insert pattern with dialect-specific INSERT ... ON CONFLICT DO NOTHING:

  • PostgreSQL: Uses sqlalchemy.dialects.postgresql.insert with on_conflict_do_nothing(index_elements=[...])
  • SQLite: Uses sqlalchemy.dialects.sqlite.insert with on_conflict_do_nothing()
  • Other dialects: Falls back to the original get-then-add pattern for backward compatibility

The upsert is followed by a regular SELECT to fetch the (now guaranteed to exist) state rows for subsequent state delta application.

Tests

Added 2 regression tests:

  • test_concurrent_create_session_no_unique_violation: 10 concurrent create_session calls with same app_name, different user_ids (tests the app_states race)
  • test_concurrent_create_session_same_user_no_unique_violation: 10 concurrent calls with same app_name AND user_id (tests the user_states race)

All 80 existing session tests pass.

Files Changed

  • src/google/adk/sessions/database_session_service.py: Added _ensure_state_rows() method, updated create_session() to use it
  • tests/unittests/sessions/test_session_service.py: Added 2 regression tests

Fixes #4954

@google-cla
Copy link

google-cla bot commented Mar 23, 2026

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@adk-bot adk-bot added the services [Component] This issue is related to runtime services, e.g. sessions, memory, artifacts, etc label Mar 23, 2026
@rohityan rohityan self-assigned this Mar 23, 2026
@rohityan rohityan added the request clarification [Status] The maintainer need clarification or more information from the author label Mar 23, 2026
@rohityan
Copy link
Collaborator

Hi @Lyt060814 , Thank you for your contribution! It appears you haven't yet signed the Contributor License Agreement (CLA). Please visit https://cla.developers.google.com/ to complete the signing process. Once the CLA is signed, we'll be able to proceed with the review of your PR. Thank you!

…reate_session

When multiple concurrent create_session calls target the same app_name
on a fresh database, the SELECT-then-INSERT pattern on app_states and
user_states tables causes UniqueViolation errors. All concurrent callers
see None from the SELECT and all attempt INSERT.

Replace the check-then-insert pattern with dialect-specific INSERT ...
ON CONFLICT DO NOTHING (PostgreSQL and SQLite). For unsupported dialects,
the original get-then-add fallback is preserved.

Added regression tests for concurrent session creation with:
- Same app_name, different user_ids (app_states race)
- Same app_name AND user_id (user_states race)

Fixes google#4954
@Lyt060814 Lyt060814 force-pushed the fix/create-session-race-condition branch from 042df3d to a99b903 Compare March 23, 2026 16:10
@Lyt060814
Copy link
Author

Hi @rohityan, thanks for the heads up! CLA is now signed and the check is passing ✅. I've also rebased onto the latest main to bring the branch up to date. Ready for review whenever you get a chance!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Race condition in create_session causes UniqueViolation on app_states under concurrent use

3 participants