[pkg-lua-devel] Bug#1099154: More Info about the bug lua-redis

Alejandro E BM alejandro.monedero at gmail.com
Fri Feb 28 22:03:36 GMT 2025


Sorry, I sent the bug report incomplete.

The problem is that the lua-redis package does not close the file
descriptors when calling redis quit or shutdown. Here in this report to the
upstream is the problem explained https://github.com/nrk/redis-lua/issues/71

I detected the problem when running the software kamailio-proxy. After
running some time I got this error

ERROR: app_lua [app_lua_api.c:719]: app_lua_run_ex(): error from Lua:
/usr/share/lua/5.1/redis.lua:813: could not connect to 127.0.0.1:6379 [Too
many open files]

When running a lsof there where a lot of this entries:

kamailio   2205          kamailio    4u     sock                0,8
 0t0      33760 protocol: TCP
kamailio   2205          kamailio   13u     sock                0,8
 0t0      33758 protocol: TCP
kamailio   2205          kamailio   17u     sock                0,8
 0t0     130221 protocol: TCP
kamailio   2205          kamailio   18u     sock                0,8
 0t0     130222 protocol: TCP
kamailio   2205          kamailio   19u     sock                0,8
 0t0     143025 protocol: TCP

To reproduce the problem I wrote a lua script, to use first

ulimit -m 1024
lua pe.lua
...
uccessfully connected and pinged Redis. Total connections: 1018
Successfully connected and pinged Redis. Total connections: 1019
Successfully connected and pinged Redis. Total connections: 1020
Successfully connected and pinged Redis. Total connections: 1021
Error connecting to Redis: /usr/share/lua/5.3/redis.lua:810: could not
connect to 127.0.0.1:6379 [Too many open files]
Reached the maximum open files limit. Sleeping for 10 hours...

The script is:

local redis = require "redis"  -- Ensure this library is installed
(lua-redis)
local socket = require "socket"  -- For sleeping

local redis_host = "127.0.0.1"
local redis_port = 6379

local connection_counter = 0  -- Initialize connection counter
local client_list = {}  -- Initialize an array to store all client
connections

-- Main loop
local attempt = 0
while true do
    -- Protected call to catch the error and handle it
    local success, client_or_error = pcall(function()
        local client, err = redis.connect(redis_host, redis_port)
        if err then
            return nil, err
        end

        -- Save the client to the client_list array
        table.insert(client_list, client)

        connection_counter = connection_counter + 1  -- Increment the
connection counter

        local res, err = client:ping()
        if err then
            return nil, err
        end

        client:quit()  -- This will not properly release the TCP socket,
causing lingering connections

        return client
    end)

    if not success then
        -- If the pcall failed (error occurred)
        print("Error connecting to Redis: " .. client_or_error)

        -- Check if the error is "Too many open files"
        if string.match(client_or_error, "Too many open files") then
            print("Reached the maximum open files limit. Sleeping for 10
hours...")
            socket.sleep(10 * 60 * 60)  -- Sleep for 10 hours
        else
            -- If it's another error, break out of the loop
            break
        end
    else
        -- If success, print the connection count
        print("Successfully connected and pinged Redis. Total connections:
" .. connection_counter)
    end
end

-- Print out the number of connections made
print("Connections made: " .. connection_counter)

-- Do your lsof here to check the connections
print("Do your lsof")

-- Sleep for 10 hours to allow you to inspect the system
socket.sleep(10 * 60 * 60)  -- Sleep for 10 hours


Instead

When editing  /usr/share/lua/5.3/redis.lua and adding

client_prototype.quit = function(client)
    request.multibulk(client, 'QUIT')
    client.network.socket:shutdown()
    client.network.socket:close() -- ADDED
    return true
end

client_prototype.shutdown = function(client)
    request.multibulk(client, 'SHUTDOWN')
    client.network.socket:shutdown()
    client.network.socket:close() -- ADDED
end

With this change the file descriptors are properly closed.

Cheers

Alejandro


More information about the pkg-lua-devel mailing list