performEvictions: mem_freed may be negative (#7908)

If 'delta' is negative 'mem_freed' may underflow and cause
the while loop to exit prematurely (And not evicting enough
memory)

mem_freed can be negative when:
1. We use lazy free (consuming memory by appending to a list)
2. Thread doing an allocation between the two calls to zmalloc_used_memory.
This commit is contained in:
guybe7 2020-10-13 18:50:57 +02:00 committed by GitHub
parent f559a57206
commit 37fd3d40ae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -499,7 +499,8 @@ int performEvictions(void) {
if (!isSafeToPerformEvictions()) return EVICT_OK;
int keys_freed = 0;
size_t mem_reported, mem_tofree, mem_freed;
size_t mem_reported, mem_tofree;
long long mem_freed; /* May be negative */
mstime_t latency, eviction_latency;
long long delta;
int slaves = listLength(server.slaves);
@ -520,7 +521,7 @@ int performEvictions(void) {
monotime evictionTimer;
elapsedStart(&evictionTimer);
while (mem_freed < mem_tofree) {
while (mem_freed < (long long)mem_tofree) {
int j, k, i;
static unsigned int next_db = 0;
sds bestkey = NULL;