Repository navigation
Conversation
SessionDB used a non-atomic check-then-insert (isSessionExist() then INSERT) to create the 'sessions' row on first write. Two concurrent requests carrying the same new session id could both pass the existence check and both INSERT, causing a PRIMARY KEY violation on PK_sessions (SQL Server error 2627, MySQL error 1062). Make the create idempotent via a new ensureSessionRow() helper: on a duplicate-key error it treats the row as already created and falls through to an update. Add isDuplicateKeyError() to recognise duplicate/PK violations across SQL Server, MySQL and SQLite. Apply the same tolerance to the session_kv_data per-key insert in writeSessionKey(). Add regression tests to MSSQLSessionStorageTest and MySQLSessionStorageTest that deterministically drive the create branch into a collision via a stale-read SessionDB double; they fail on the pre-fix code (2627 / 1062) and pass with the fix. Fixes #428
The 'sessions' table enforces a PRIMARY KEY on s-id for all engines, so the non-atomic check-then-insert race applies to SQLite too (UNIQUE constraint failed, error 19). SQLite's writer-serialising lock makes the natural race unlikely, but it is still possible via separate connections or busy-retry. Add a regression test mirroring the MSSQL/MySQL ones: a stale-read SessionDB double drives the create branch into a collision. It fails on the pre-fix code (error 19) and passes with the fix, exercising the SQLite branch of isDuplicateKeyError().
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #429 +/- ##
============================================
+ Coverage 81.96% 81.99% +0.03%
- Complexity 3293 3301 +8
============================================
Files 106 106
Lines 9735 9752 +17
============================================
+ Hits 7979 7996 +17
Misses 1756 1756
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Summary
Makes
DatabaseSessionStorage(viaSessionDB) create the session row race-safely, fixing a duplicate primary-key error that occurred under concurrency.Motivation
SessionDBcreated thesessionsrow on first write with a non-atomic check-then-insert (isSessionExist()thenINSERT). Two concurrent requests carrying the same brand-new session id (e.g. an SPA firing several API calls on first load) could both pass the existence check and bothINSERT, causing aPRIMARY KEYviolation onPK_sessions— SQL Server error 2627, MySQL error 1062. The losing request failed with aDatabaseExceptioneven though the session was ultimately created. Fixes #428.Changes
SessionDB::ensureSessionRow()— idempotent, race-safe session-row creation: on a duplicate-key error it treats the row as already created and falls through to alast-usedupdate instead of propagating the exception.SessionDB::isDuplicateKeyError()— recognises duplicate/PK violations across SQL Server (2627/2601), MySQL (1062) and SQLite (19/23000), with a SQLSTATE/message fallback.saveSession()andwriteSessionKey()withensureSessionRow().session_kv_dataper-key insert inwriteSessionKey()(a secondary race on the same path).MSSQLSessionStorageTestandMySQLSessionStorageTest.How to Test / Verify
Automated (PHPUnit) integration tests against real databases:
MSSQLSessionStorageTest::testConcurrentCreateIsRaceSafeandMySQLSessionStorageTest::testConcurrentCreateIsRaceSafedeterministically driveSessionDB's create branch into a collision using a stale-readSessionDBdouble (its firstisSessionExist()returns a stalefalse), reproducing the exact losing-request scenario.Breaking Changes and Migration Steps
None.
Checklist
composer fix-cs) — not run; changes follow existing file style and passphp -lRelated issues
Closes #428