From bb23eb0b01bdc485ee6634e4323bc251df6d78ba Mon Sep 17 00:00:00 2001 From: "debing.sun" Date: Tue, 27 May 2025 17:14:06 +0800 Subject: [PATCH] Fix incorrect server.cronloops update in defragWhileBlocked() causing timer to run twice as fast (#14081) This bug was introduced in [#13814](https://github.com/redis/redis/issues/13814), and was found by @guybe7. It incorrectly moved the update of `server.cronloops` from `whileBlockedCron()` to `activeDefragTimeProc()`, causing the cron-based timers to effectively run twice as fast when active defrag is enabled. As a result, memory statistics are not updated during blocked operations. The repair parts from https://github.com/redis/redis/pull/13995, because it needs to be backport, so use a separate pr repair it. --- src/defrag.c | 6 ------ src/server.c | 6 ++++++ 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/defrag.c b/src/defrag.c index 21e729046..6cc100aef 100644 --- a/src/defrag.c +++ b/src/defrag.c @@ -1554,12 +1554,6 @@ static int activeDefragTimeProc(struct aeEventLoop *eventLoop, long long id, voi monotime endtime = starttime + dutyCycleUs; int haveMoreWork = 1; - /* Increment server.cronloops so that run_with_period works. */ - long hz_ms = 1000 / server.hz; - int cronloops = (server.mstime - server.blocked_last_cron + (hz_ms - 1)) / hz_ms; /* rounding up */ - server.blocked_last_cron += cronloops * hz_ms; - server.cronloops += cronloops; - mstime_t latency; latencyStartMonitor(latency); diff --git a/src/server.c b/src/server.c index 3b343901e..78efab5bb 100644 --- a/src/server.c +++ b/src/server.c @@ -1703,6 +1703,12 @@ void whileBlockedCron(void) { if (server.blocked_last_cron >= server.mstime) return; + /* Increment server.cronloops so that run_with_period works. */ + long hz_ms = 1000 / server.hz; + int cronloops = (server.mstime - server.blocked_last_cron + (hz_ms - 1)) / hz_ms; /* rounding up */ + server.blocked_last_cron += cronloops * hz_ms; + server.cronloops += cronloops; + mstime_t latency; latencyStartMonitor(latency);