Skip to content

Commit f81c78e

Browse files
Merge pull request #1092 from NilPuig/fix/memory-usage-nil-guard
Fix nil panic in memoryUsageCmd Lua script
2 parents d704b68 + 2fd155e commit f81c78e

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

internal/rdb/inspect.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,9 @@ for i=1,2 do
264264
if (table.getn(ids) > 0) then
265265
for _, id in ipairs(ids) do
266266
local bytes = redis.call("MEMORY", "USAGE", ARGV[1] .. id)
267-
sample_total = sample_total + bytes
267+
if bytes then
268+
sample_total = sample_total + bytes
269+
end
268270
end
269271
local n = redis.call("LLEN", KEYS[i])
270272
local avg = sample_total / table.getn(ids)
@@ -281,7 +283,9 @@ for i=3,6 do
281283
if (table.getn(ids) > 0) then
282284
for _, id in ipairs(ids) do
283285
local bytes = redis.call("MEMORY", "USAGE", ARGV[1] .. id)
284-
sample_total = sample_total + bytes
286+
if bytes then
287+
sample_total = sample_total + bytes
288+
end
285289
end
286290
local n = redis.call("ZCARD", KEYS[i])
287291
local avg = sample_total / table.getn(ids)
@@ -304,13 +308,17 @@ if table.getn(groups) > 0 then
304308
local ids = redis.call("ZRANGE", group_key, 0, sample_size - 1)
305309
for _, id in ipairs(ids) do
306310
local bytes = redis.call("MEMORY", "USAGE", ARGV[1] .. id)
307-
agg_task_sample_total = agg_task_sample_total + bytes
308-
agg_task_sample_size = agg_task_sample_size + 1
311+
if bytes then
312+
agg_task_sample_total = agg_task_sample_total + bytes
313+
agg_task_sample_size = agg_task_sample_size + 1
314+
end
309315
end
310316
end
311317
end
312-
local avg = agg_task_sample_total / agg_task_sample_size
313-
memusg = memusg + (avg * agg_task_count)
318+
if agg_task_sample_size > 0 then
319+
local avg = agg_task_sample_total / agg_task_sample_size
320+
memusg = memusg + (avg * agg_task_count)
321+
end
314322
end
315323
return memusg
316324
`)

0 commit comments

Comments
 (0)