fix: retry refresh token updates on serialization failures (SQL & ent)#4830
Open
RonnanSouza wants to merge 1 commit into
Open
fix: retry refresh token updates on serialization failures (SQL & ent)#4830RonnanSouza wants to merge 1 commit into
RonnanSouza wants to merge 1 commit into
Conversation
cf70e72 to
1f86e6a
Compare
UpdateRefreshToken runs a read-modify-write inside a SERIALIZABLE transaction. Under concurrent refresh-token rotation of the same token (e.g. multiple kube clients refreshing at once), Postgres aborts the conflicting transaction with SQLSTATE 40001 and the error surfaced to clients as HTTP 500. Add a bounded, jittered retry around the refresh-token update that re-runs the transaction on transient serialization/deadlock failures, detected per-driver (Postgres 40001/40P01, MySQL 1213/1205). Error wraps in the SQL storage now use %w so the driver error can be matched with errors.As. SQLite serializes writes and opts out (nil check). Enables the previously-disabled refresh-token concurrency conformance tests for Postgres and MySQL. Signed-off-by: Ronan <ronanpalmeiras@gmail.com> fix(storage/ent): retry refresh token update on serialization failures Mirror the SQL backend fix in the ent storage. UpdateRefreshToken runs in a SERIALIZABLE transaction and aborts with a serialization failure under concurrent rotation of the same token, surfacing as HTTP 500. Wrap the refresh-token update in a bounded, jittered retry that re-runs the transaction on transient serialization/deadlock failures, detected per-driver (Postgres 40001/40P01, MySQL 1213/1205) via errors.As over the ent-wrapped driver error. Enables the previously-disabled refresh-token concurrency conformance tests for Postgres, MySQL and MySQL 8. Signed-off-by: Ronan <ronanpalmeiras@gmail.com> refactor(storage): extract shared SQL retry policy into sqlretry refactor(storage): extract shared SQL retry policy into sqlretry Signed-off-by: Ronan <ronanpalmeiras@gmail.com>
1f86e6a to
a016e56
Compare
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.
Overview
When several clients call
UpdateRefreshToken— which performs a read-modify-write inside a SERIALIZABLE transaction — concurrent rotation of the same token makes the database abort one transaction with a serialization failure, e.g.:
time=2026-06-10T09:34:44.629Z level=ERROR msg="failed to update refresh token" err="update refresh token: pq: could not serialize access due to concurrent update" Neither SQL storage backend retried, so the abort surfaced to clients as a 500.
What this PR does / why we need it
Add a bounded, jittered retry around the refresh-token update in both SQL backends (
storage/sqlandstorage/ent):errors.As: Postgres40001/40P01, MySQL1213/1205.database/sqlnor ent retries transactions natively.Special notes for your reviewer