Repository navigation
Convert timedelta to milliseconds with integer arithmetic - #4571
Conversation
pexpire, psetex, hpexpire and set(px=...) converted a timedelta with int(td.total_seconds() * 1000). That goes through a float and can land 1 ms low: timedelta(milliseconds=1001) was sent as 1000. Use timedelta floor division instead, keeping truncation toward zero for negative values. Seconds-based conversions are unchanged.
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
petyaslavova
left a comment
There was a problem hiding this comment.
Hey @ericdelorefice, thank you for your contribution!
The bug is real: a timedelta that should go out as 1001 ms is sent as 1000, and integer division fixes it.
Before merging, please:
- Apply
_timedelta_to_msinspexpiretoo, with an argument-level test like thepexpireone. That covers all the millisecond conversions in this mixin. - Hoist
timedelta(milliseconds=1)andtimedelta(0)to module-level constants, so the helper does not create them on every call.
Please leave the "0 deletes" question out of this PR. If you want to pursue it, open an issue for it.
spexpire converted a timedelta with int(td.total_seconds() * 1000) too. Route it through _timedelta_to_ms and add sync and async argument-level tests. Make timedelta(0) and timedelta(milliseconds=1) module-level constants so the helper does not build them on every call.
|
Thanks for the review, @petyaslavova. The two code changes are in fe9ed34:
I also took the "0 deletes" question out of the description. |
petyaslavova
left a comment
There was a problem hiding this comment.
Thanks for the updates. Both changes look good, so I'm approving this.
…4595) * Fix 1 ms float drift in datetime-to-millisecond expiry conversions int(dt.timestamp() * 1000) lands 1 ms low for instants whose seconds part sits just above a power of two (e.g. 2038-01-19T03:14:08.002Z is sent as ...001 instead of ...002). Convert through the exact integer microseconds instead, reusing the integer arithmetic approach from #4571 and keeping the same truncation semantics. Adds argument-level regression tests for pxat/pexpireat/hpexpireat/spexpireat (sync and async). * Avoid overflow when converting aware expiry datetimes * Preserve naive expiry datetimes at the upper boundary --------- Co-authored-by: 0718lol <[email protected]> Co-authored-by: petyaslavova <[email protected]>
* Convert timedelta to milliseconds with integer arithmetic pexpire, psetex, hpexpire and set(px=...) converted a timedelta with int(td.total_seconds() * 1000). That goes through a float and can land 1 ms low: timedelta(milliseconds=1001) was sent as 1000. Use timedelta floor division instead, keeping truncation toward zero for negative values. Seconds-based conversions are unchanged. * Use the integer conversion in spexpire, hoist helper constants spexpire converted a timedelta with int(td.total_seconds() * 1000) too. Route it through _timedelta_to_ms and add sync and async argument-level tests. Make timedelta(0) and timedelta(milliseconds=1) module-level constants so the helper does not build them on every call. --------- Co-authored-by: petyaslavova <[email protected]>
…4595) * Fix 1 ms float drift in datetime-to-millisecond expiry conversions int(dt.timestamp() * 1000) lands 1 ms low for instants whose seconds part sits just above a power of two (e.g. 2038-01-19T03:14:08.002Z is sent as ...001 instead of ...002). Convert through the exact integer microseconds instead, reusing the integer arithmetic approach from #4571 and keeping the same truncation semantics. Adds argument-level regression tests for pxat/pexpireat/hpexpireat/spexpireat (sync and async). * Avoid overflow when converting aware expiry datetimes * Preserve naive expiry datetimes at the upper boundary --------- Co-authored-by: 0718lol <[email protected]> Co-authored-by: petyaslavova <[email protected]>
Title: Convert timedelta to milliseconds with integer arithmetic
Description of change
Passing a timedelta to a millisecond command goes through a float and can come out 1 ms low.
timedelta(milliseconds=1001)is sent as 1000, because1.001 * 1000is1000.9999999999999andint()truncates it. For whole millisecond values from 1 to 100000, 741 come out one short.What master sends, with
execute_commandstubbed to return its arguments:Before #916 this was worked out from
days,secondsandmicroseconds, so positive values came out exact. #916 (Drop Python 2.6 support) switched it toint(td.total_seconds() * 1000). This PR goes back to integer arithmetic.I added a small private helper,
_timedelta_to_ms, inredis/utils.pyand used it inpexpire,psetex,hpexpire,spexpireand thepxpath ofextract_expire_flags(whichset,getex,hsetex,hgetexand the otherpx=methods go through).saddex,increxandmsetextake the samepxpath; they are covered by theextract_expire_flagsunit test only. It doestd // timedelta(milliseconds=1), and for a negative timedelta it still truncates toward zero the wayint()did. The seconds commands (expire,setex,hexpire,sexpireand theexpath) are not touched.Tests: sync and async tests for
pexpire,psetexandhpexpirecheck the argument sent and then run the command against the server. A unit test covers thepxpath, and another pins negative values (-1001 ms to -1001, -1500.3 ms to -1500, -0.5 ms to 0).spexpirehas sync and async argument-level tests only, since SPEXPIRE needs Redis 8.11.240 and my local server is 8.10.3. The new tests fail on master. Verified on Redis 8.10.3.Pull Request check-list
I used Claude Code to help with this; I've checked and can explain every line.
Note
Low Risk
Tightens millisecond TTL math for timedelta inputs with behavior intended to match prior truncation semantics; low risk beyond subtly longer/shorter expirations for some edge timedeltas.
Overview
Fixes millisecond conversions for timedeltas so they no longer lose 1 ms due to floating-point scaling.
This introduces
_timedelta_to_msinredis/utils, using integer floor division (with a symmetric path for negatives that still truncates toward zero likeint()). It replacesint(td.total_seconds() * 1000)inpexpire,psetex,spexpire, andhpexpire, and switches thepx=timedelta branch inextract_expire_flags(used bySET,GETEX, field/hash setters, etc.) to the helper.Adds targeted sync/async tests that assert the exact Redis argument for tricky values like
1001ms, plus unit tests for negative deltas inextract_expire_flags.Reviewed by Cursor Bugbot for commit a8cfae8. Bugbot is set up for automated code reviews on this repo. Configure here.