Repository navigation
Make Redis per-provider upstream delete atomic - #7303
Conversation
rdimitrov
left a comment
There was a problem hiding this comment.
Approve: the single Lua script is the right fix for the delete/index race.
Design: The row delete and both index SREMs now run atomically in redis.go, the owner of row/index consistency, in the same style as the existing store and CAS scripts. The user-set key built inside the script matches redisSetKey exactly and shares the {ns:name} hash tag in the prefix, so Cluster slot consistency holds. Both non-test callers (refresher.go:269, callback.go:837) only log errors, so they are covered, and there is no API, CLI or persisted-format change. DeleteUpstreamTokens and DeleteUser keep the same race; the PR says it defers them, which is fine.
Non-blocking notes (not required for approval):
redis.go, WRONGTYPE precheck: if an index key (or the row key itself) holds the wrong type, the delete now fails and leaves the row. Before, the row was deleted and only theSREMwarned. In the refresher path (refresher.go:269) that row is the dead rotated refresh token, so a corrupted index plus a persist failure leaves it to be replayed and trip the IdP's reuse detection. This needs prior key corruption, so it is rare. Either state the fail-closed behaviour in the PR body, or skip theSREMon a wrong-type index key, stillDELthe row, and return a distinct status that Go logs.- The new tests run on miniredis only.
redis_integration_test.goalready runs real Redis, so one delete plus WRONGTYPE case there would pin the realTYPE/error_replybehaviour.
🤖 Reviewed with Claude Code
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #7303 +/- ##
==========================================
+ Coverage 81.95% 81.99% +0.04%
==========================================
Files 922 923 +1
Lines 93367 93575 +208
==========================================
+ Hits 76521 76730 +209
+ Misses 16841 16840 -1
Partials 5 5 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
DeleteUpstreamTokensForProvider ran GET, DEL and two SREMs as separate commands. A write to the same row landing between the DEL and the SREMs left a live row missing from the session index and the user set, so GetAllUpstreamTokens, GetLatestUpstreamTokensForUser and DeleteUser could not see it until the next write. Writes were already atomic Lua scripts; the delete was the odd one out. Replace the sequence with one Lua script that reads the stored user ID, deletes the row and removes it from both index sets. Rows with no usable user ID (placeholder, undecodable, empty or null user_id) are still deleted and removed from the session index. Any Redis error now fails the call instead of being logged. The script checks that both index keys are sets (or absent) before changing anything and fails with WRONGTYPE otherwise. Redis does not roll back a script that errors partway, and the row is the only record of which user set to clean up, so deleting it first would leave a stale user-set member that DeleteUser later trusts. Tests cover both index sets, the unusable-row cases, a wrong-type index key on each side, and a go-redis hook that stores the row under another user right after any standalone command on it. The hook fails on the old implementation and on one that reads the owner before the script, and never fires on the script itself. Co-authored-by: Mecatl <[email protected]>
dcf59e0 to
ad5f00d
Compare
|
@rdimitrov @ChrisJBurns the lint job on this PR was failing, so I amended the commit and force-pushed. The branch is now The failure was golangci-lint's
Because the commit changed, GitHub may have dismissed the earlier approval, so it may need a re-approval. |
Summary
RedisStorage.DeleteUpstreamTokensForProvidersent four separate commands: GET the row to learn its user ID, DEL the row, then SREM it from the session index and from the user's reverse-index set. If a write to the same row landed between the DEL and the SREMs, the result was a live row missing from both index sets.GetAllUpstreamTokensandGetLatestUpstreamTokensForUsercould not see it, andDeleteUserwould not delete it, until the next write of that row. With session-keyed storage a concurrent write to the same key is unlikely, but with user-keyed storage ("User-keyed upstream storage behind the embedder option" (#7263)) every client of a user writes the same key. The write paths were already atomic Lua scripts; the delete was the odd one out.storeUpstreamTokensScript.nullplaceholder, undecodable JSON, empty, missing or JSON-nulluser_id) are still deleted and removed from the session index; only the user-set removal is skipped, as before.WRONGTYPE. Redis does not roll back a script that fails partway, and the row is the only record of which user set to clean up, so deleting it first and then failing would leave a stale user-set member thatDeleteUserlater trusts.Type of change
Test plan
go test -race ./pkg/authserver/storage/)task test-e2e)task lint-fix)New tests in
redis_test.go(miniredis):Does this introduce a user-facing change?
No change visible to MCP clients. Both in-tree callers (the refresher after a failed persist of a rotated refresh token, and
cleanupUpstreamTokensin the callback handler) already log a delete error and carry on, so on a Redis fault operators may now see that caller's warning where before they saw a cleanup warning from storage, or nothing for a failed GET.Special notes for reviewers
user_idto be a non-empty string. The store and CAS scripts do neither; they are left unchanged here.{ns:name}-tagged key prefix rather than declared inKEYS, so it lands in the same cluster slot. Declaring it inKEYSwould be a separate change covering all three scripts.DeleteUpstreamTokensandDeleteUserare still multi-command and have the same class of race. They are a separate change.docs/redis-storage.mdnordocs/arch/11-auth-server-storage.mddescribes the delete steps.