fix: retry refresh token updates on serialization failures (SQL & ent)#4830
fix: retry refresh token updates on serialization failures (SQL & ent)#4830RonnanSouza wants to merge 2 commits into
Conversation
1f86e6a to
a016e56
Compare
|
I'd also consider making the hard-coded parameters inputs with defaults (i.e. the number of retries, backoff, jitter). Maybe the hard-coded / defaults are good for some users, but others may need to fine-tune... |
|
Hey Pimenta, thanks for the review.
I set this way because I saw a similar approach in the k8s retry mechanism. But I can make it customizable with the enable/disable config from the previous comment. |
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>
Signed-off-by: Ronan <ronanpalmeiras@gmail.com>
5799628 to
63763ac
Compare
matheuscscp
left a comment
There was a problem hiding this comment.
LGTM! 🚀
Thanks @RonnanSouza for addressing my concerns 🤗
For the opt-in to work as a canary we need to tell people about it, can you pls open a PR in the website repo for adding docs? 🙏 Thanks ❤️
Yey, thanks for the time reviewing it! Sure, I'll open a PR for the website and for the helm chart for it. |
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