Commit c2940a8
committed
fix(sessions): raise AlreadyExistsError on concurrent create_session races
The has_user_provided_id existence check in
DatabaseSessionService.create_session() is not atomic with the insert
that follows it: two concurrent callers can both pass the check and
then race the same INSERT on (app_name, user_id, session_id). The
loser saw a raw, unhandled IntegrityError instead of a clean error.
Wrap the insert flush in try/except IntegrityError and raise
AlreadyExistsError, mirroring the SAVEPOINT pattern
_get_or_create_state already uses for app_state/user_state races.
Verified against both sqlite+aiosqlite and postgres+asyncpg with a
concurrent create_session() reproduction: 5/5 trials on each backend
now raise a clean AlreadyExistsError instead of a raw
IntegrityError/UniqueViolationError.
This does not address the separate orphaned user_state row issue also
reported in #6823 -- _rollback_on_exception_session rolls back the
whole transaction on any exception (including the AlreadyExistsError
raised here), so this particular race shouldn't be able to leave a
row behind on its own. That deeper issue needs more data to root-cause.
Related: #68231 parent 775c1bd commit c2940a8
2 files changed
Lines changed: 72 additions & 1 deletion
File tree
- src/google/adk/sessions
- tests/unittests/sessions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
639 | 639 | | |
640 | 640 | | |
641 | 641 | | |
642 | | - | |
| 642 | + | |
| 643 | + | |
| 644 | + | |
| 645 | + | |
| 646 | + | |
| 647 | + | |
| 648 | + | |
| 649 | + | |
| 650 | + | |
| 651 | + | |
| 652 | + | |
| 653 | + | |
643 | 654 | | |
644 | 655 | | |
645 | 656 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1190 | 1190 | | |
1191 | 1191 | | |
1192 | 1192 | | |
| 1193 | + | |
| 1194 | + | |
| 1195 | + | |
| 1196 | + | |
| 1197 | + | |
| 1198 | + | |
| 1199 | + | |
| 1200 | + | |
| 1201 | + | |
| 1202 | + | |
| 1203 | + | |
| 1204 | + | |
| 1205 | + | |
| 1206 | + | |
| 1207 | + | |
| 1208 | + | |
| 1209 | + | |
| 1210 | + | |
| 1211 | + | |
| 1212 | + | |
| 1213 | + | |
| 1214 | + | |
| 1215 | + | |
| 1216 | + | |
| 1217 | + | |
| 1218 | + | |
| 1219 | + | |
| 1220 | + | |
| 1221 | + | |
| 1222 | + | |
| 1223 | + | |
| 1224 | + | |
| 1225 | + | |
| 1226 | + | |
| 1227 | + | |
| 1228 | + | |
| 1229 | + | |
| 1230 | + | |
| 1231 | + | |
| 1232 | + | |
| 1233 | + | |
| 1234 | + | |
| 1235 | + | |
| 1236 | + | |
| 1237 | + | |
| 1238 | + | |
| 1239 | + | |
| 1240 | + | |
| 1241 | + | |
| 1242 | + | |
| 1243 | + | |
| 1244 | + | |
| 1245 | + | |
| 1246 | + | |
| 1247 | + | |
| 1248 | + | |
| 1249 | + | |
| 1250 | + | |
| 1251 | + | |
| 1252 | + | |
1193 | 1253 | | |
1194 | 1254 | | |
1195 | 1255 | | |
| |||
0 commit comments