Skip to content

Fix SET KEEPTTL losing the value of a logically expired key - #990

Open
0x5t4l1n wants to merge 1 commit into
Snapchat:mainfrom
0x5t4l1n:fix/set-keepttl-expired-key
Open

0x5t4l1n wants to merge 1 commit into
Snapchat:mainfrom
0x5t4l1n:fix/set-keepttl-expired-key

Conversation

@0x5t4l1n

@0x5t4l1n 0x5t4l1n commented Dec 3, 2026

Copy link
Copy Markdown

Fixes #983

What changed

SET key value KEEPTTL carries the existing expire over to the new value. If the key was already past its expire time but not reclaimed yet, the dead expire was moved onto the new value, the SET replied OK, and the next read expired the key and deleted the value. setGenericCommand now calls expireIfNeeded first when KEEPTTL is set, so the key is dropped and the new value is written without a stale expire. Live keys still keep their TTL, and a missing key behaves as before. expireIfNeeded is used directly so no keyspace hit/miss stats or LRU are touched.

Testing

Built from this branch (make MALLOC=libc NO_MOTD=yes BUILD_TLS=no). Reproduced on the unfixed binary: SET k old PXAT 1; SET k new KEEPTTL; GET k returned nil. With the fix it returns new, EXISTS k is 1 and TTL k is -1. A live key still keeps its TTL (SET live v EX 100; SET live v2 KEEPTTL; TTL live -> 100). Added a test in tests/unit/expire.tcl; ./runtest --single unit/expire --clients 1 passes with the fix and the new test fails without it. I ran only the expire suite, not the full test suite. I did not check replica or active-replica behaviour.

AI-assisted (Claude Code).

When the key was past its expire time but not yet reclaimed, SET ... KEEPTTL moved the dead expire onto the new value, so the next read expired the key and deleted the value that had just been acknowledged. Expire the key first on the KEEPTTL path, so the write starts from a clean key, as Redis does through its write lookup. Add a regression test.

Fixes Snapchat#983

Signed-off-by: Stalin <[email protected]>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] SET ... KEEPTTL on a logically expired key replies OK and then loses the value

1 participant