From e103f07c92e99cf1cf6ff8b7c6d522f019c6252f Mon Sep 17 00:00:00 2001 From: Pradyum Mistry <39883432+altf4-games@users.noreply.github.com> Date: Wed, 11 Nov 2026 20:08:05 +0530 Subject: [PATCH] store: make valkey UpdateSessionLastActivity atomic Signed-off-by: Pradyum Mistry <39883432+altf4-games@users.noreply.github.com> --- pkg/store/store_redis.go | 10 ++++-- pkg/store/store_valkey.go | 25 ++++++------- pkg/store/store_valkey_test.go | 64 ++++++++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+), 15 deletions(-) diff --git a/pkg/store/store_redis.go b/pkg/store/store_redis.go index 8b6a0e8be..46feab255 100644 --- a/pkg/store/store_redis.go +++ b/pkg/store/store_redis.go @@ -297,19 +297,23 @@ func (rs *redisStore) Close() error { return rs.cli.Close() } -// updateSessionLastActivityScript atomically checks if the session key exists +// updateSessionLastActivityLua atomically checks if the session key exists // and updates its last activity time in the sorted-set index. // It uses EXISTS to verify presence rather than GET (which retrieved the // actual value in older implementations). This saves data transfer overhead, // if in future the value is required GET can be used instead. -var updateSessionLastActivityScript = redisv9.NewScript(` +// Both the redis and valkey stores run this script, so a session deleted +// between the check and the ZADD cannot be re-added to the index. +const updateSessionLastActivityLua = ` if redis.call("EXISTS", KEYS[1]) == 1 then redis.call("ZADD", KEYS[2], ARGV[2], ARGV[1]) return 1 else return 0 end -`) +` + +var updateSessionLastActivityScript = redisv9.NewScript(updateSessionLastActivityLua) // UpdateSessionLastActivity updates the last-activity index for the given session. func (rs *redisStore) UpdateSessionLastActivity(ctx context.Context, sessionID string, at time.Time) error { diff --git a/pkg/store/store_valkey.go b/pkg/store/store_valkey.go index eb2cf3207..e8d39bf57 100644 --- a/pkg/store/store_valkey.go +++ b/pkg/store/store_valkey.go @@ -301,7 +301,13 @@ func (vs *valkeyStore) Close() error { return nil } -// UpdateSessionLastActivity updates the last-activity index for the given session +// updateSessionLastActivityValkeyScript is the valkey client's handle for +// updateSessionLastActivityLua (defined in store_redis.go). +var updateSessionLastActivityValkeyScript = valkey.NewLuaScript(updateSessionLastActivityLua) + +// UpdateSessionLastActivity updates the last-activity index for the given session. +// The existence check and the ZADD run in one script, so a concurrent delete +// cannot leave an orphaned member in the last-activity index. func (vs *valkeyStore) UpdateSessionLastActivity(ctx context.Context, sessionID string, at time.Time) error { if sessionID == "" { return errors.New("UpdateSessionLastActivity: sessionID is empty") @@ -310,20 +316,15 @@ func (vs *valkeyStore) UpdateSessionLastActivity(ctx context.Context, sessionID at = time.Now() } sessionKey := vs.sessionKey(sessionID) - existsResult, err := vs.cli.Do(ctx, vs.cli.B().Exists().Key(sessionKey).Build()).AsInt64() + res, err := updateSessionLastActivityValkeyScript.Exec(ctx, vs.cli, + []string{sessionKey, vs.lastActivityIndexKey}, + []string{sessionID, strconv.FormatInt(at.Unix(), 10)}, + ).AsInt64() if err != nil { - return fmt.Errorf("UpdateSessionLastActivity: valkey Exists failed: %w", err) + return fmt.Errorf("UpdateSessionLastActivity: script execution failed for sessionID %s: %w", sessionID, err) } - if existsResult != 1 { - // Existing result must be 1 + if res == 0 { return ErrNotFound } - - zddCmd := vs.cli.B().Zadd().Key(vs.lastActivityIndexKey).ScoreMember(). - ScoreMember(float64(at.Unix()), sessionID).Build() - err = vs.cli.Do(ctx, zddCmd).Error() - if err != nil { - return fmt.Errorf("UpdateSessionLastActivity: ZADD failed: %w", err) - } return nil } diff --git a/pkg/store/store_valkey_test.go b/pkg/store/store_valkey_test.go index 122d91f6e..c30ed9339 100644 --- a/pkg/store/store_valkey_test.go +++ b/pkg/store/store_valkey_test.go @@ -20,6 +20,7 @@ import ( "context" "errors" "strings" + "sync" "testing" "time" @@ -360,3 +361,66 @@ func TestValkeyStore_UpdateSandboxLastActivity(t *testing.T) { assert.Error(t, err) assert.True(t, errors.Is(err, ErrNotFound)) } + +// afterFirstDoClient wraps a valkey.Client and runs hook once, right after +// the first Do call returns. It lets a test land a concurrent operation +// between two round trips of the code under test. +type afterFirstDoClient struct { + valkey.Client + once sync.Once + hook func() +} + +func (c *afterFirstDoClient) Do(ctx context.Context, cmd valkey.Completed) valkey.ValkeyResult { + resp := c.Client.Do(ctx, cmd) + c.once.Do(c.hook) + return resp +} + +// TestValkeyStore_UpdateSessionLastActivity_ConcurrentDelete checks that a +// session deleted while its last activity is being updated does not leave an +// orphaned member in the last-activity index. The delete lands right after the +// first round trip of UpdateSessionLastActivity, both before and after the +// script is cached on the server (EVAL fallback vs. EVALSHA). +func TestValkeyStore_UpdateSessionLastActivity_ConcurrentDelete(t *testing.T) { + for _, tc := range []struct { + name string + scriptCached bool + }{ + {name: "script not cached", scriptCached: false}, + {name: "script cached", scriptCached: true}, + } { + t.Run(tc.name, func(t *testing.T) { + ctx := context.Background() + c, mr := newValkeyTestClient(t) + + now := time.Now().UTC().Truncate(time.Second) + assert.NoError(t, c.StoreSandbox(ctx, newTestSandbox("sb-1", "sess-1", now.Add(30*time.Minute)))) + if tc.scriptCached { + assert.NoError(t, c.StoreSandbox(ctx, newTestSandbox("sb-0", "sess-0", now.Add(30*time.Minute)))) + assert.NoError(t, c.UpdateSessionLastActivity(ctx, "sess-0", now)) + } + + // The delete runs on the unwrapped store, as the GC or a DELETE request would. + wrapped := *c + wrapped.cli = &afterFirstDoClient{ + Client: c.cli, + hook: func() { + assert.NoError(t, c.DeleteSandboxBySessionID(ctx, "sess-1")) + }, + } + + err := wrapped.UpdateSessionLastActivity(ctx, "sess-1", now) + if err != nil { + assert.ErrorIs(t, err, ErrNotFound) + } + + assert.False(t, mr.Exists(c.sessionKey("sess-1")), "session key should be deleted") + members, err := mr.ZMembers(c.lastActivityIndexKey) + if err != nil { + assert.ErrorIs(t, err, miniredis.ErrKeyNotFound) + } + assert.NotContains(t, members, "sess-1", "deleted session must not be re-added to the last-activity index") + }) + } +}